rstudio / htmltools

Tools for HTML generation and output
https://rstudio.github.io/htmltools/
215 stars 68 forks source link

HTML comments break parsing by recommonmark #193

Open abielr opened 3 years ago

abielr commented 3 years ago

I'm creating .Rmd files and translating them to .md with knitr, then later combining them with other Markdown files to generate a book using the Jupyter Book project. My understanding is that this tool uses Sphinx to parse Markdown, and that Sphinx in turn relies on the recommonmark package to parse Markdown files. In any case, the html_preserve comments inserted by htmltools seem to break recommonmark's parsing, as discussed here. The problem goes away, however, if the HTML comments are on their own line.

Raising this issue here as I don't know if the fix should be in htmltools or in recommonmark. Obviously it would be straightforward to change htmltools to have the comments on their own line, but not sure if this would break other downstream things in the R ecosystem.

jcheng5 commented 3 years ago

The .md files generated by knitr are Pandoc markdown, not commonmark markdown. Does it help if you insert this into the middle of your workflow?

pandoc -f markdown -t commonmark <filename>

I've been thinking of converting <!--html_preserve--> to use raw blocks, but these are different in pandoc ({=html}) versus commonmark ({raw} html, apparently?) so I think that might make your problem worse, not better.

Edit: Oh, is {raw} html a Jupyter/Jupyter Book thing, perhaps?

abielr commented 3 years ago

Using the recommended pandoc command solves one issue but causes another: while <!--html_preserve--> gets put on a new line, it also adds a lot of whitespace to the HTML which causes it to change how it is rendered in the browser. For example, something like

<td class="gt_row gt_right">4</td>

becomes

<td class="gt_row gt_right">

4

</td>

However, if in htmltools/R/tags.R::htmlPreserve() you were to change

sprintf("<!--html_preserve-->%s<!--/html_preserve-->", x)

to

sprintf("<!--html_preserve-->\n%s\n<!--/html_preserve-->", x)

than it appears to work fine with commonmark.