From debc084926ee81ab2bf67783f62f58739acd66a3 Mon Sep 17 00:00:00 2001 From: Hadley Wickham Date: Mon, 11 Jul 2022 10:49:36 +1200 Subject: [PATCH] Don't propagate NA language (#1395) Fixes #1251 --- NEWS.md | 2 ++ R/markdown.R | 7 ++++--- tests/testthat/test-markdown-code.R | 13 +++++++++++++ 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/NEWS.md b/NEWS.md index ef1d1249..c66a4b70 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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). diff --git a/R/markdown.R b/R/markdown.R index 020e4a6c..97363b61 100644 --- a/R/markdown.R +++ b/R/markdown.R @@ -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) } @@ -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{