Skip to content

Commit

Permalink
Retain forward slashes in HTML img src paths
Browse files Browse the repository at this point in the history
Fixes #2807
  • Loading branch information
Andrew0Hill authored Nov 8, 2024
1 parent ffe60d5 commit 3bee460
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion R/tweak-page.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ tweak_rmarkdown_html <- function(html, input_path, pkg = list(bs_version = 3)) {
img_src_real <- path_real(xml2::url_unescape(src[abs_src]))
input_path_real <- path_real(xml2::url_unescape(input_path))
img_rel_paths <- path_rel(path = img_src_real, start = input_path_real)
img_rel_paths <- xml2::url_escape(img_rel_paths)
img_rel_paths <- xml2::url_escape(img_rel_paths, reserved="/")

purrr::walk2(
.x = img_target_nodes,
Expand Down
33 changes: 33 additions & 0 deletions tests/testthat/test-tweak-page.R
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,36 @@ test_that("h1 section headings adjusted to h2 (and so on)", {
c("page-header", "section level2", "section level3", "section level2")
)
})

test_that("slashes not URL encoded during relative path conversion", {

# Since the URL conversion process in tweak_markdown_html() makes calls to
# fs::path_real() (which requires paths to exist), we create a
# temporary pkgdown site directory and populate it with an image file, which
# we can then reference in our test HTML file.

# Create a site
pkg <- local_pkgdown_site()

# Add the referenced image in a subdirectory of vignettes.
pkg <- pkg_add_kitten(pkg, "vignettes/img/")

# Get the full path to the image.
sim_path <- path_real(path(pkg$src_path, "vignettes/img/kitten.jpg"))

# Simulate an output HTML file referencing the absolute path.
html <- xml2::read_html(
sprintf('
<body>
<img src="%s" />
</body>
', sim_path)
)

# Function should update the absolute path to a relative path.
tweak_rmarkdown_html(html, path(pkg$src_path, "vignettes"))

# Check that the relative path has a non-encoded slash.
expect_equal(xpath_attr(html, ".//img", "src"), "img/kitten.jpg")

})

0 comments on commit 3bee460

Please sign in to comment.