Jupyter-book: HTML comments ahead of raw HTML tag cause garbled HTML output

Created on 13 Nov 2020  路  4Comments  路  Source: executablebooks/jupyter-book

I am attempting to include in my book a Markdown file (.md) that has been generated from a RMarkdown file (.Rmd) by the R knitr package. I am using the R gt package to create formatted tables, and it inserts a HTML comment on the same line as it's HTML <style> tag, i.e. something like this:

<!--html_preserve--><style>
    .myclass {
        font-weight: bold;
        color: red
    }
</style>

<span class="myclass">Hello world</span><!--/html_preserve-->

The leading comment, <!--html_preserve-->, causes the final HTML file outputted by Jupyter Book to include HTML tags within the <style> section which cause it to no longer work. In this case you would get:

<!--html_preserve--><style>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>.myclass {
    font-weight: bold;
    color: red
}
</pre></div>
</div>
</style>

However, the rmarkdown::render() command in R will process this correctly. The comment looks like it is probably generated by the htmltools::htmlPreserve function:

htmlPreserve <- function(x) {
  x <- paste(x, collapse = "\n")
  if (nzchar(x))
    sprintf("<!--html_preserve-->%s<!--/html_preserve-->", x)
  else
    x
}

Since this package (hmltools) is potentially used by many other R packages the issue is likely going to be widespread when dealing with inputs generated by R.

Environment

  • Python Version: 3.7.5
  • Package versions or output of jupyter-book --version: 0.8.3
  • Operating System: MacOS
bug

Most helpful comment

Thanks @mmcky, if I understand right what you are suggesting is to insert the {raw} block around the original HTML in the .md file generated by knitr. If so, probably the easier thing is just open the file and do a find and replace for those HTML comments, replacing them with an empty string and writing out the new file. Sphinx seems to have no issue parsing the resulting file.

Hi @choldgraf, to clarify, I believe this behavior is a function of the R htmltools package, not RStudio. This package is imported into a wide range of other R packages, such that many other R packages could potentially generate similar issues.

Note: I've only raised an issue with the htmltools project here so they are aware of this.

All 4 comments

Thanks for opening your first issue here! Engagement like this is essential for open source projects! :hugs:

If you haven't done so already, check out EBP's Code of Conduct. Also, please try to follow the issue template as it helps other community members to contribute more effectively.

If your issue is a feature request, others may react to it, to raise its prominence (see Feature Voting).

Welcome to the EBP community! :tada:

hi @abielr maybe you could try using the raw directive to pass this html through to the HTML writer.

```{raw} html <html> ```

to ensure the html is unaltered as it is passed through to html.

more generally, I wonder whether the markdown behavior that @abielr mentions at the top is CommonMark-compliant or if it something specific to RStudio. If it's the former, then we should find a way to support it, but if it isn't then I think @mmcky's suggestion is the way to go

Thanks @mmcky, if I understand right what you are suggesting is to insert the {raw} block around the original HTML in the .md file generated by knitr. If so, probably the easier thing is just open the file and do a find and replace for those HTML comments, replacing them with an empty string and writing out the new file. Sphinx seems to have no issue parsing the resulting file.

Hi @choldgraf, to clarify, I believe this behavior is a function of the R htmltools package, not RStudio. This package is imported into a wide range of other R packages, such that many other R packages could potentially generate similar issues.

Note: I've only raised an issue with the htmltools project here so they are aware of this.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

choldgraf picture choldgraf  路  4Comments

choldgraf picture choldgraf  路  5Comments

mmcky picture mmcky  路  5Comments

spring-haru picture spring-haru  路  5Comments

sidneymbell picture sidneymbell  路  5Comments