Skip to content

Conversation

@etiennebacher
Copy link

@etiennebacher etiennebacher commented Sep 20, 2022

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
Copy link
Author

Test failures are unrelated to this PR.


text <- xml2::xml_text(nodes)
replacement <- map_chr(text, fun, ...)
replacement <- unlist(lapply(seq_along(nodes), function(x) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you could simplify this code by adding pre_class as an explicit argument to tweak_children() — then you don't need to go through gymnastics to figure out whether or not it's set.

Copy link
Author

@etiennebacher etiennebacher Sep 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand. If I add an explicit argument pre_class in tweak_children(), then I will have to check whether this argument is NULL (or FALSE) before applying strsplit(), so I don't think this would result in a simpler code. But maybe I misunderstood you?

@sgvignali
Copy link

Do you have any updates?

@etiennebacher
Copy link
Author

etiennebacher commented May 5, 2023

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Autolinking Quarto html remove the "copy-code" button downlit does not keep code source option set with knitr

3 participants