Skip to content

Commit

Permalink
Link issues/users at line starts (take two) (r-lib#2374)
Browse files Browse the repository at this point in the history
Fixes r-lib#2030 (not creating links at line starts) while not breaking r-lib#2122, for most common use case.
  • Loading branch information
pearsonca authored and SebKrantz committed Jun 1, 2024
1 parent 1b6d2b2 commit f0a0a72
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# pkgdown (development version)

* Fix parsing of github profiles and issues into links when present at the beginning of list items (@pearsonca, #2122)
* Correct parse usage for S3 methods with non-syntactic class names (#2384).

* Deprecated `build_favicon()` was removed (`build_favicons()` remains).
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("(p>|\\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("(p>|\\(|\\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: 9 additions & 0 deletions tests/testthat/test-repo.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,21 @@ test_that("authors are automatically linked", {
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, "(@y)"), "(<a href='TEST/y'>@y</a>)")

expect_equal(repo_auto_link(pkg, "<p>@y some other text.</p>"), "<p><a href='TEST/y'>@y</a> some other text.</p>")
})

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, "<p>#123 some other text.</p>"), "<p><a href='TEST/123'>#123</a> some other text.</p>")
expect_equal(repo_auto_link(pkg, "<p><a href='TEST/123/'>#123</a></p>"), "<p><a href='TEST/123/'>#123</a></p>")
})

test_that("already linked issues aren't re-linked", {
pkg <- list(repo = repo_meta(issue = "TEST/"))
expect_equal(repo_auto_link(pkg, "<p><a href='NOT/ABC/'>#123</a></p>"), "<p><a href='NOT/ABC/'>#123</a></p>")
})

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

0 comments on commit f0a0a72

Please sign in to comment.