Skip to content

Commit

Permalink
Improve md link parsing
Browse files Browse the repository at this point in the history
By prohibiting [ inside of links; fixes #555.
  • Loading branch information
hadley committed Jun 28, 2018
1 parent bb9eb2e commit ddd8f71
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 10 additions & 4 deletions R/markdown.R
Original file line number Diff line number Diff line change
Expand Up @@ -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]]
Expand All @@ -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.
Expand Down
7 changes: 7 additions & 0 deletions tests/testthat/test-rd-markdown-links.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()]"),
"<p><code>[[</code>. <a href=\"R:subset()\">subset()</a></p>\n"
)
})

test_that("commonmark picks up the various link references", {
cases <- list(
c("foo [func()] bar",
Expand Down

0 comments on commit ddd8f71

Please sign in to comment.