From 9bcefb6bca6013e9577b821c4b65c39c7a570027 Mon Sep 17 00:00:00 2001 From: hadley Date: Thu, 28 Jun 2018 16:06:17 -0500 Subject: [PATCH] Respect escapes when passing links Fixes #720 --- NEWS.md | 3 ++- R/markdown.R | 2 +- tests/testthat/test-rd-markdown-links.R | 12 ++++++++++++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/NEWS.md b/NEWS.md index 88c10d5fb..ed92c91d2 100644 --- a/NEWS.md +++ b/NEWS.md @@ -110,7 +110,8 @@ * `%` in inline code blocks is now automatically escaped (#640). * Parsing of markdown links has been tweaked to reduce false positives - (#555) + (#555). If you still get a false positive, you can now put `\\` in front + of the `[` to avoid it being converted to a link (#720). # roxygen2 6.0.1 diff --git a/R/markdown.R b/R/markdown.R index 01e24d392..7333b7010 100644 --- a/R/markdown.R +++ b/R/markdown.R @@ -278,7 +278,7 @@ add_linkrefs_to_md <- function(text) { regex( comments = TRUE, " - (?<=[^\\]]|^) # must not be preceded by ] + (?<=[^\\]\\\\]|^) # must not be preceded by ] or \ \\[([^\\]\\[]+)\\] # match anything inside of [] (?:\\[([^\\]\\[]+)\\])? # match optional second pair of [] (?=[^\\[]|$) # must not be followed by [ diff --git a/tests/testthat/test-rd-markdown-links.R b/tests/testthat/test-rd-markdown-links.R index c95c70d96..07f0e67b0 100644 --- a/tests/testthat/test-rd-markdown-links.R +++ b/tests/testthat/test-rd-markdown-links.R @@ -31,6 +31,18 @@ test_that("can not have [ inside of link", { ) }) +test_that("can escape [ to avoid spurious links", { + expect_equal( + md_link_html("\\[test\\]"), + "

[test]

\n" + ) + + expect_equal( + md_link_html("\\[ [test] \\]"), + "

[ test ]

\n", + ) +}) + test_that("commonmark picks up the various link references", { cases <- list( c("foo [func()] bar",