Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: preserve 'rmd' code chunks in Rd #2300

Merged
merged 2 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading