Skip to content

Commit

Permalink
Also tweak urls with fragment (r-lib#2469)
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley authored and SebKrantz committed Jun 1, 2024
1 parent 0889870 commit 15ed800
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
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

0 comments on commit 15ed800

Please sign in to comment.