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

Also tweak urls with fragment #2469

Merged
merged 2 commits into from
Apr 18, 2024
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,6 +1,8 @@
# pkgdown (development version)

* `build_readme()` now correctly tweaks links to markdown files that use an anchor, e.g. `foo.md#heading-name` (#2313).
* `build_home()` no longer errors when you have an empty `.md` file (#2309).

* `data_template()` gives a more informative error if you've misspecified the navbar (#2312).
* The skip link now becomes visible when focussed (#2138). Thanks to @glin for the styles!
* `build_reference_index()` gives more informative errors if your `contents` field is malformed (#2323).
Expand Down
6 changes: 4 additions & 2 deletions R/tweak-tags.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,12 @@ tweak_link_md <- function(html) {
return()

hrefs <- xml2::xml_attr(links, "href")
needs_tweak <- grepl("\\.md$", hrefs) & xml2::url_parse(hrefs)$scheme == ""

urls <- xml2::url_parse(hrefs)
needs_tweak <- urls$scheme == "" & grepl("\\.md$", urls$path)

fix_links <- function(x) {
x <- gsub("\\.md$", ".html", x)
x <- gsub("\\.md\\b", ".html", x)
x <- gsub("\\.github/", "", x)
x
}
Expand Down
4 changes: 3 additions & 1 deletion tests/testthat/test-tweak-tags.R
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,18 @@ test_that("docs with no headings are left unchanged", {
test_that("local md links are replaced with html", {
html <- xml2::read_html('
<a href="local.md"></a>
<a href="local.md#fragment"></a>
<a href="http://remote.com/remote.md"></a>
')
tweak_link_md(html)

expect_equal(
xpath_attr(html, "//a", "href"),
c("local.html", "http://remote.com/remote.md")
c("local.html", "local.html#fragment", "http://remote.com/remote.md")
)
})


test_that("tweak_link_external() add the external-link class if needed", {
html <- xml2::read_html('
<a href="#anchor"></a>
Expand Down
Loading