diff --git a/NEWS.md b/NEWS.md index f7e645adc..64063745a 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,6 @@ # pkgdown (development version) +* `` tags now have their `srcref` attributes tweaked in the same way that the `src` attributes of `` tags are (#2402). * New translation for "Search site", the label applied to the search box for screenreaders. This was previously incorrectly labelled as "Toggle navigation" (#2320). * You can now choose where the search box is placed with the "search" navbar component. This has been documented for a very long time, but as far as I can tell, never worked (#2320). If you have made your own template with a custom `navbar`, you will need to remove the `
` with `role="search"` to avoid getting two search boxes. * The mobile version of pkgdown sites no longer has a scrollburglar (a small amount of horizontal scroll) (#2179, @netique). diff --git a/R/tweak-tags.R b/R/tweak-tags.R index 7c9b8cb60..b61698ecd 100644 --- a/R/tweak-tags.R +++ b/R/tweak-tags.R @@ -83,11 +83,19 @@ tweak_link_external <- function(html, pkg = list()) { # Fix relative image links tweak_img_src <- function(html) { + fix_path <- function(x) { + x <- gsub("(^|/)vignettes/", "\\1articles/", x, perl = TRUE) + x <- gsub("(^|/)man/figures/", "\\1reference/figures/", x, perl = TRUE) + x + } + imgs <- xml2::xml_find_all(html, ".//img[not(starts-with(@src, 'http'))]") - urls <- xml2::xml_attr(imgs, "src") - new_urls <- gsub("(^|/)vignettes/", "\\1articles/", urls, perl = TRUE) - new_urls <- gsub("(^|/)man/figures/", "\\1reference/figures/", new_urls, perl = TRUE) - purrr::map2(imgs, new_urls, ~ xml2::xml_set_attr(.x, "src", .y)) + urls <- fix_path(xml2::xml_attr(imgs, "src")) + purrr::map2(imgs, urls, ~ xml2::xml_set_attr(.x, "src", .y)) + + imgs <- xml2::xml_find_all(html, ".//source[not(starts-with(@srcset, 'http'))]") + urls <- fix_path(xml2::xml_attr(imgs, "srcset")) + purrr::map2(imgs, urls, ~ xml2::xml_set_attr(.x, "srcset", .y)) invisible() } diff --git a/tests/testthat/test-tweak-tags.R b/tests/testthat/test-tweak-tags.R index 5a08e9891..7101c960f 100644 --- a/tests/testthat/test-tweak-tags.R +++ b/tests/testthat/test-tweak-tags.R @@ -198,6 +198,18 @@ test_that("tweak_link_r6() correctly modifies link to inherited R6 classes", { ) }) + +test_that("tweak_img_src() updates img and source tags", { + html <- xml2::read_html(' + + + ') + + tweak_img_src(html) + expect_equal(xpath_attr(html, ".//img", "src"), "reference/figures/bar.png") + expect_equal(xpath_attr(html, ".//source", "srcset"), "reference/figures/foo.png") +}) + test_that("tweak_img_src() doesn't modify absolute links", { html <- xml2::read_html(' @@ -212,7 +224,6 @@ test_that("tweak_img_src() doesn't modify absolute links", { ) }) - # stripped divs etc ------------------------------------------------------- test_that("selectively remove hide- divs", {