From 7ad55dbce6e8f5c6498768bea030b66a0581f590 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A1bor=20Cs=C3=A1rdi?= Date: Tue, 2 Mar 2021 10:28:27 +0100 Subject: [PATCH] Escape md link targets for '%' Closes #1209. --- NEWS.md | 3 +++ R/markdown.R | 5 ++++- tests/testthat/test-markdown-link.R | 16 ++++++++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index c3da7917b..d95847f4a 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,8 @@ # roxygen2 (development version) +* Percent signs in markdown link targets, e.g. `[text](https://foo/ba%20r)` + are now handled correctly (#1209). + * The new `@exmaplesIf` tag can be used to create conditional examples. These examples only run if a specified condition holds (#962). diff --git a/R/markdown.R b/R/markdown.R index b682b1eec..b7c657fcc 100644 --- a/R/markdown.R +++ b/R/markdown.R @@ -333,7 +333,10 @@ mdxml_link <- function(xml, state) { } else if (dest == "" || dest == xml_text(xml)) { paste0("\\url{", escape_comment(xml_text(xml)), "}") } else { - paste0("\\href{", dest, "}{", mdxml_link_text(contents, state), "}") + paste0( + "\\href{", escape_comment(dest), "}", + "{", mdxml_link_text(contents, state), "}" + ) } } diff --git a/tests/testthat/test-markdown-link.R b/tests/testthat/test-markdown-link.R index e7c07cfdd..0db803049 100644 --- a/tests/testthat/test-markdown-link.R +++ b/tests/testthat/test-markdown-link.R @@ -446,3 +446,19 @@ test_that("linking to self is unqualified", { "foo \\code{\\link[=fun]{fun()}} and \\link{obj} bar" ) }) + +test_that("percents are escaped in link targets", { + out1 <- roc_proc_text(rd_roclet(), " + #' Title + #' + #' [link % text](https://foo.bar/link%20target) + #' @md + foo <- function() {}")[[1]] + out2 <- roc_proc_text(rd_roclet(), " + #' Title + #' + #' \\href{https://foo.bar/link%20target}{link % text} + #' @md + foo <- function() {}")[[1]] + expect_equivalent_rd(out1, out2) +})