Skip to content

Commit

Permalink
Preserve 'rmd' code chunks in Rd (#2300)
Browse files Browse the repository at this point in the history
Fixes #2298
  • Loading branch information
maelle committed Oct 19, 2023
1 parent eae9be1 commit d5432a4
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 0 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# pkgdown (development version)

* Preserve Markdown code blocks with class rmd from roxygen2 docs (@salim-b, #2298).
* Avoid unwanted linebreaks from parsing `DESCRIPTION` (@salim-b, #2247).
* Remove redundant entries in the documentation index when multiple explicit `@usage` tags are provided (@klmr, #2302)
* The article index now sorts vignettes and non-vignette articles alphabetically by their filename (literally, their `basename()`), by default (@jennybc, #2253).
Expand Down
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 d5432a4

Please sign in to comment.