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

Tweak source srcrefs #2474

Merged
merged 2 commits into from
Apr 19, 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
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# pkgdown (development version)

* `<source>` tags now have their `srcref` attributes tweaked in the same way that the `src` attributes of `<img>` 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 `<form>` 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).
Expand Down
16 changes: 12 additions & 4 deletions R/tweak-tags.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
13 changes: 12 additions & 1 deletion tests/testthat/test-tweak-tags.R
Original file line number Diff line number Diff line change
Expand Up @@ -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('<body>
<source srcset="man/figures/foo.png" />
<img src="man/figures/bar.png" />
</body>')

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('<body>
<img src="https://raw.githubusercontent.com/OWNER/REPO/main/vignettes/foo" />
Expand All @@ -212,7 +224,6 @@ test_that("tweak_img_src() doesn't modify absolute links", {
)
})


# stripped divs etc -------------------------------------------------------

test_that("selectively remove hide- divs", {
Expand Down
Loading