Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve repo_auto_link() regexp #2104

Merged
merged 1 commit into from
Jun 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# pkgdown (development version)

* Links to `@username` in NEWS.md work in more places (#2030).

* Footnotes work with more contents, including code (@banfai, #2042).

* Manual pages `\figure{file}{alternative text}` tags with multines alternative text can now be parsed. (#2080)
Expand Down
4 changes: 2 additions & 2 deletions R/repo.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ repo_auto_link <- function(pkg, text) {

if (!is.null(url$user)) {
user_link <- paste0("\\1<a href='", url$user, "\\2'>@\\2</a>")
text <- gsub("(\\s|^|\\()@([-\\w]+)", user_link, text, perl = TRUE)
text <- gsub("(\\s|[(>])@([-\\w]+)", user_link, text, perl = TRUE)
}

if (!is.null(url$issue)) {
issue_link <- paste0("<a href='", url$issue, "\\2'>#\\2</a>")
text <- gsub("(\\(|\\s)#(\\d+)", paste0("\\1", issue_link), text, perl = TRUE)
text <- gsub("(\\s|[(>])#(\\d+)", paste0("\\1", issue_link), text, perl = TRUE)

if (!is.null(pkg$repo$jira_projects)) {
issue_link <- paste0("<a href='", url$issue, "\\1\\2'>\\1\\2</a>")
Expand Down
9 changes: 5 additions & 4 deletions tests/testthat/test-repo.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ test_that("authors are automatically linked", {
# email addresses shouldn't get linked
expect_equal(repo_auto_link(pkg, "x@y.com"), "x@y.com")

# must have leading whitespace or open parens
expect_equal(repo_auto_link(pkg, "@y"), "<a href='TEST/y'>@y</a>")
# must have leading whitespace or open parens or end of HTML tag
expect_equal(repo_auto_link(pkg, " @y"), " <a href='TEST/y'>@y</a>")
expect_equal(repo_auto_link(pkg, "(@y)"), "(<a href='TEST/y'>@y</a>)")
expect_equal(repo_auto_link(pkg, "<p>@y"), "<p><a href='TEST/y'>@y</a>")
})

test_that("issues are automatically linked", {
pkg <- list(repo = repo_meta(issue = "TEST/"))
expect_equal(repo_auto_link(pkg, "(#123"), "(<a href='TEST/123'>#123</a>")
expect_equal(repo_auto_link(pkg, "in #123"), "in <a href='TEST/123'>#123</a>")
expect_equal(repo_auto_link(pkg, " #123"), " <a href='TEST/123'>#123</a>")
expect_equal(repo_auto_link(pkg, "(#123)"), "(<a href='TEST/123'>#123</a>)")
expect_equal(repo_auto_link(pkg, "<p>#123"), "<p><a href='TEST/123'>#123</a>")
})

test_that("URLs with hash (#) are preserved", {
Expand Down