r-lib / downlit

Syntax Highlighting and Automatic Linking
https://downlit.r-lib.org
Other
90 stars 22 forks source link

Fix: `downlit_html_*()` keeps all pre classes #160

Open etiennebacher opened 2 years ago

etiennebacher commented 2 years ago

Close #149, close #156 (and v2 of #155).

downlit_html() now keeps all pre classes, which allows for customization of code blocks in pkgdown using chunk options class.source and attr.source.

Current behavior (.my-class and .another-class are dropped):

library(downlit)

html <- xml2::read_xml("
<div>
  <div class='sourceCode'>
    <pre class='sourceCode r my-class'>
      <code class='sourceCode r'>
        <span>Hello</span>
      </code>
    </pre>
  </div>
  <div class='sourceCode'>
    <pre class='sourceCode r another-class'>
      <code class='sourceCode r'>
        <span>Hello</span>
      </code>
    </pre>
  </div>
</div>")
downlit_html_node(html)
html
#> {xml_document}
#> <div>
#> [1] <div class="sourceCode">\n  <pre class="downlit sourceCode r">\n<code cla ...
#> [2] <div class="sourceCode">\n  <pre class="downlit sourceCode r">\n<code cla ...

New behavior:

library(downlit)

html <- xml2::read_xml("
<div>
  <div class='sourceCode'>
    <pre class='sourceCode r my-class'>
      <code class='sourceCode r'>
        <span>Hello</span>
      </code>
    </pre>
  </div>
  <div class='sourceCode'>
    <pre class='sourceCode r another-class'>
      <code class='sourceCode r'>
        <span>Hello</span>
      </code>
    </pre>
  </div>
</div>")
downlit_html_node(html)
html
#> {xml_document}
#> <div>
#> [1] <div class="sourceCode">\n  <pre class="downlit sourceCode r my-class">\n ...
#> [2] <div class="sourceCode">\n  <pre class="downlit sourceCode r another-clas ...
etiennebacher commented 2 years ago

Test failures are unrelated to this PR.

sgvignali commented 1 year ago

Do you have any updates?

etiennebacher commented 1 year ago

@sgvignali I don't think there's anything more I can do on my side, so I'm just waiting for someone from r-lib to review this