Skip to content

Commit

Permalink
fix: preserve 'rmd' code chunks in Rd
Browse files Browse the repository at this point in the history
  • Loading branch information
maelle committed Oct 19, 2023
1 parent eae9be1 commit 3c4247b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
2 changes: 2 additions & 0 deletions R/tweak-reference.R
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ tweak_highlight_other <- function(div) {
lang <- sub("sourceCode ", "", xml2::xml_attr(div, "class"))
# since roxygen 7.2.0 generic code blocks have sourceCode with no lang
if (!is.na(lang) && lang == "sourceCode") lang <- "r"
# Pandoc does not recognize rmd as a language :-)
if (tolower(lang) %in% c("rmd", "qmd")) lang <- "markdown"
# many backticks to account for possible nested code blocks
# like a Markdown code block with code chunks inside
md <- paste0("``````", lang, "\n", xml2::xml_text(code), "\n``````")
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/test-tweak-reference.R
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,14 @@ test_that("can highlight other languages", {
expect_equal(xpath_text(html, "//code//span[not(span)]")[[1]], "field")
})

test_that("can highlight 'rmd'", {
skip_if_no_pandoc("2.16")
html <- xml2::read_xml('<div class="rmd"><pre><code>field: value</code></pre></div>')
tweak_highlight_other(html)

expect_equal(xpath_attr(html, "//code//span[not(span)]", "class")[[1]], "an")
})

test_that("fails cleanly", {
html <- xml2::read_xml('<div><pre><code></code></pre></div>')
tweak_highlight_other(html)
Expand Down

0 comments on commit 3c4247b

Please sign in to comment.