Skip to content

Commit

Permalink
Don't propagate NA language (#1395)
Browse files Browse the repository at this point in the history
Fixes #1251
  • Loading branch information
hadley authored Jul 10, 2022
1 parent 1157142 commit debc084
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# roxygen2 (development version)

* Generated HTML for code blocks never includes "NA" for language (#1251).

* Using a level 1 heading in the wrong tag now gives a more useful warning
(#1374).

Expand Down
7 changes: 4 additions & 3 deletions R/markdown.R
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,9 @@ eval_code_node <- function(node, env) {
# write knitr markup for inline code
text <- paste0("`", xml_text(node), "`")
} else {
lang <- xml_attr(node, "info")
# write knitr markup for fenced code
text <- paste0("```", xml_attr(node, "info"), "\n", xml_text(node), "```\n")
text <- paste0("```", if (!is.na(lang)) lang, "\n", xml_text(node), "```\n")
}
roxy_knit(text, env, knitr_chunk_defaults)
}
Expand Down Expand Up @@ -315,8 +316,8 @@ special <- c(
)

mdxml_code_block <- function(xml, state) {
info <- xml_attr(xml, "info")[1]
if (is.na(info) || nchar(info[1]) == 0) info <- NA_character_
info <- xml_attr(xml, "info", default = "")[1]
if (nchar(info[1]) == 0) info <- NA_character_
paste0(
"\n\n",
"\\if{html}{\\out{<div class=\"sourceCode",
Expand Down
13 changes: 13 additions & 0 deletions tests/testthat/test-markdown-code.R
Original file line number Diff line number Diff line change
Expand Up @@ -230,3 +230,16 @@ test_that("workaround for cmark sourcepos bug (#1353) works", {
expect_equal(out$get_section("description")$value, "line1\npre 1 2 3 post")
expect_equal(out$get_section("details")$value, "no workaround needed here")
})


test_that("doesn't generate NA language", {
out <- roc_proc_text(rd_roclet(), "
#' Title
#'
#' ```
#' r <- 1:10
#' ```
#' @md
foo <- function() {}")[[1]]
expect_false(grepl("NA", out$get_section("description")$value))
})

0 comments on commit debc084

Please sign in to comment.