diff --git a/NEWS.md b/NEWS.md index 0a1ea665d..f353ab075 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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). diff --git a/R/tweak-reference.R b/R/tweak-reference.R index 5acac5866..803db0aa8 100644 --- a/R/tweak-reference.R +++ b/R/tweak-reference.R @@ -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``````") diff --git a/tests/testthat/test-tweak-reference.R b/tests/testthat/test-tweak-reference.R index 71ab71e32..0f7ca3bf8 100644 --- a/tests/testthat/test-tweak-reference.R +++ b/tests/testthat/test-tweak-reference.R @@ -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('
field: value
') + tweak_highlight_other(html) + + expect_equal(xpath_attr(html, "//code//span[not(span)]", "class")[[1]], "an") +}) + test_that("fails cleanly", { html <- xml2::read_xml('
') tweak_highlight_other(html)