diff --git a/NEWS.md b/NEWS.md
index 12e4bccd5..f4440a37f 100644
--- a/NEWS.md
+++ b/NEWS.md
@@ -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).
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('