From ddd8f71dcc9439ee2ad1c6a18f95f2b884e34789 Mon Sep 17 00:00:00 2001 From: hadley Date: Thu, 28 Jun 2018 15:59:39 -0500 Subject: [PATCH] Improve md link parsing By prohibiting [ inside of links; fixes #555. --- NEWS.md | 3 +++ R/markdown.R | 14 ++++++++++---- tests/testthat/test-rd-markdown-links.R | 7 +++++++ 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/NEWS.md b/NEWS.md index e6059af24..88c10d5fb 100644 --- a/NEWS.md +++ b/NEWS.md @@ -108,6 +108,9 @@ space (#628, @egnha). * `%` in inline code blocks is now automatically escaped (#640). + + * Parsing of markdown links has been tweaked to reduce false positives + (#555) # roxygen2 6.0.1 diff --git a/R/markdown.R b/R/markdown.R index 72f227283..01e24d392 100644 --- a/R/markdown.R +++ b/R/markdown.R @@ -278,10 +278,10 @@ add_linkrefs_to_md <- function(text) { regex( comments = TRUE, " - (?<=[^\\]]|^) # must not be preceded by ] - \\[([^\\]]+)\\] # match anything inside of [] - (?:\\[([^\\]]+)\\])? # match optional second pair of [] - (?=[^\\[]|$) # must not be followed by [ + (?<=[^\\]]|^) # must not be preceded by ] + \\[([^\\]\\[]+)\\] # match anything inside of [] + (?:\\[([^\\]\\[]+)\\])? # match optional second pair of [] + (?=[^\\[]|$) # must not be followed by [ " ) )[[1]] @@ -303,6 +303,12 @@ add_linkrefs_to_md <- function(text) { ) } +# Helper designed primarily for testing + +md_link_html <- function(x) { + commonmark::markdown_html(add_linkrefs_to_md(x)) +} + #' Parse a MarkDown link, to see if we should create an Rd link #' #' See the table above. diff --git a/tests/testthat/test-rd-markdown-links.R b/tests/testthat/test-rd-markdown-links.R index 9e019d742..c95c70d96 100644 --- a/tests/testthat/test-rd-markdown-links.R +++ b/tests/testthat/test-rd-markdown-links.R @@ -24,6 +24,13 @@ test_that("proper link references are added", { } }) +test_that("can not have [ inside of link", { + expect_equal( + md_link_html("`[[`. [subset()]"), + "

[[. subset()

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