From b510631799485b89f643bc44f7d3ee5de8380a70 Mon Sep 17 00:00:00 2001 From: Hadley Wickham Date: Thu, 18 Apr 2024 11:51:47 -0500 Subject: [PATCH] Also tweak urls with fragment Fixes #2313 --- NEWS.md | 1 + R/tweak-tags.R | 6 ++++-- tests/testthat/test-tweak-tags.R | 4 +++- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/NEWS.md b/NEWS.md index 5cdb677ef..ee2d0060b 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,6 @@ # pkgdown (development version) +* `build_readme()` now correctly tweaks links to markdown files that use an anchor, e.g. `foo.md#heading-name` (#2313). * `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). diff --git a/R/tweak-tags.R b/R/tweak-tags.R index dc16ede88..7c9b8cb60 100644 --- a/R/tweak-tags.R +++ b/R/tweak-tags.R @@ -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 } diff --git a/tests/testthat/test-tweak-tags.R b/tests/testthat/test-tweak-tags.R index e4849df4a..5a08e9891 100644 --- a/tests/testthat/test-tweak-tags.R +++ b/tests/testthat/test-tweak-tags.R @@ -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(' + ') 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('