rmarkdown::render()の中でJavascriptライブラリが差し込まれるまでの流れ

自分用メモ。

rmarkdown::render()

基本的にはこのソース。中でknitr::knit()が呼ばれるのでそっちもチェック。

rmarkdown/render.R at master · rstudio/rmarkdown · GitHub

knitr/output.R at master · yihui/knitr · GitHub

knitr::knit()する前

いろいろメタデータを取ってきている。hookの設定をしてるので、Javascriptのminifyを仕込むならこの辺?と思ったけど、外部JS/CSSファイルはknitrの範疇じゃないのでむりっぽい。

https://github.com/rstudio/rmarkdown/blob/047126d9333d5890cf049205d9c9341a047f69ba/R/render.R#L26-L252

knitrのhookについてはここ参照:

短い例だとこんな感じらしい:

knit_hooks$set(output = function(x, options) paste("<pre>", x, "</pre>", sep = ""))

(出典:other examples for output hooks · Issue #147 · yihui/knitr · GitHub)

knitr::knit()

チェックしようと思ったけどよく分からないので深入りしない。

https://github.com/rstudio/rmarkdown/blob/047126d9333d5890cf049205d9c9341a047f69ba/R/render.R#L254-L263

knitr/output.R at master · yihui/knitr · GitHub

~.knit.mdwidget用のJSファイルができている。「widget用のJSファイル」というのはRパッケージ用に書いたコードだけで、YAMLに書いたライブラリのファイルはまだ。

pre-processor

https://github.com/rstudio/rmarkdown/blob/047126d9333d5890cf049205d9c9341a047f69ba/R/render.R#L299-L312

output_format

このへんでつくられるやつらしい。

https://github.com/rstudio/rmarkdown/blob/047126d9333d5890cf049205d9c9341a047f69ba/R/render.R#L156-L157

https://github.com/rstudio/rmarkdown/blob/b3a11cd4e1d586cf8f4104aa2d2462c592fc39f7/R/output_format.R#L403-L424

output_format$pandoc$args

これがpandocを実行するときに使われる。こんな感じだった。

Browse[2]> output_format$pandoc$args
 [1] "--smart"                                                                                   
 [2] "--email-obfuscation"                                                                       
 [3] "none"                                                                                      
 [4] "--self-contained"                                                                          
 [5] "--standalone"                                                                              
 [6] "--section-divs"                                                                            
 [7] "--template"                                                                                
 [8] "/path/to/R/x86_64-pc-linux-gnu-library/3.1/rmarkdown/rmd/h/default.html"              
 [9] "--variable"                                                                                
[10] "theme:bootstrap"                                                                           
[11] "--include-in-header"                                                                       
[12] "/tmp/RtmpbW6iCA/rmarkdown-str5bfa6473a3a1.html"                                            
[13] "--mathjax"                                                                                 
[14] "--variable"                                                                                
[15] "mathjax-url:https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"
[16] "--no-highlight"                                                                            
[17] "--variable"                                                                                
[18] "highlightjs=/path/to/R/x86_64-pc-linux-gnu-library/3.1/rmarkdown/rmd/h/highlight"  

--include-in-headerにJSライブラリ、CSSを含むヘッダ部分のHTMLが指定されている。JSライブラリ、CSSをminifyするならこれより前でないといけない。具体的にはどこかというと、

pandoc_html_extras_args

ここより前。

https://github.com/rstudio/rmarkdown/blob/369462f68936eab9b13e3bc266c738599c4cb8b8/R/html_document_base.R#L77-L94