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

Allow source url to omit trailing slash #2481

Merged
merged 3 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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)

* The `repo.source.url` field no longer requires a trailing slash (#2017).
* `build_article()` no longer generates the wrong source link when you build your site outside of the root directory (#2172).
* `build_reference()` matches usage for S3 and S4 methods to the style used by R 4.0.0 and later (#2187).
* `<source>` tags now have their `srcref` attributes tweaked in the same way that the `src` attributes of `<img>` tags are (#2402).
Expand Down
4 changes: 3 additions & 1 deletion R/repo.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ repo_source <- function(pkg, paths) {
return()
}

needs_slash <- !grepl("/$", url$source) & !grepl("^/", paths)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

give a name to !grepl("^/", paths) to explauin why it's needed? not_relative_links?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also should it be need_slashes or need_slash given it's applied to several things at once 💅

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmmm, it seems clear to me already because I'm thinking that you need to add a slash if there isn't one on either the left of the right.


links <- a(
paste0("<code>", escape_html(paths), "</code>"),
paste0(url$source, paths)
paste0(url$source, ifelse(needs_slash, "/", ""), paths)
)

n <- length(links)
Expand Down
11 changes: 11 additions & 0 deletions tests/testthat/test-repo.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,17 @@ test_that("repo_source() truncates automatically", {
})
})

test_that("repo_source() is robust to trailing slash", {
pkg <- list(repo = repo_meta_gh_like("https://github.com/r-lib/pkgdown"))

dummy_meta <- function(x) {
hadley marked this conversation as resolved.
Show resolved Hide resolved
list(repo = list(url = list(source = x)))
}
source <- "Source: <a href='http://example.com/a'><code>a</code></a>"
expect_equal(repo_source(dummy_meta("http://example.com"), "a"), source)
hadley marked this conversation as resolved.
Show resolved Hide resolved
expect_equal(repo_source(dummy_meta("http://example.com/"), "a"), source)
hadley marked this conversation as resolved.
Show resolved Hide resolved
})

# repo_source with alternate default branch -------------------------------

test_that("repo_source() uses the branch setting in meta", {
Expand Down
Loading