diff --git a/R/check-built.R b/R/check-built.R index 3f78e2a30..6c55eac1a 100644 --- a/R/check-built.R +++ b/R/check-built.R @@ -14,7 +14,7 @@ check_missing_images <- function(pkg, src_path, dst_path) { img <- xml2::xml_find_all(html, ".//img") src <- xml2::xml_attr(img, "src") - rel_src <- src[xml2::url_parse(src)$scheme == ""] + rel_src <- xml2::url_unescape(src[xml2::url_parse(src)$scheme == ""]) rel_path <- path_norm(path(path_dir(dst_path), rel_src)) exists <- file_exists(path(pkg$dst_path, rel_path)) diff --git a/R/tweak-page.R b/R/tweak-page.R index 3349dc469..b5e1cf74c 100644 --- a/R/tweak-page.R +++ b/R/tweak-page.R @@ -47,10 +47,16 @@ tweak_rmarkdown_html <- function(html, input_path, pkg = list(bs_version = 3)) { src <- xml2::xml_attr(img, "src") abs_src <- is_absolute_path(src) if (any(abs_src)) { + img_target_nodes <- img[abs_src] + 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) + purrr::walk2( - img[abs_src], - path_rel(path_real(src[abs_src]), path_real(input_path)), - xml2::xml_set_attr, + .x = img_target_nodes, + .y = img_rel_paths, + .f = xml2::xml_set_attr, attr = "src" ) } diff --git a/tests/testthat/test-build-article.R b/tests/testthat/test-build-article.R index e9b92d906..5429d9251 100644 --- a/tests/testthat/test-build-article.R +++ b/tests/testthat/test-build-article.R @@ -81,7 +81,7 @@ test_that("BS5 article gets correctly activated navbar", { html <- xml2::read_html(article_path) navbar <- xml2::xml_find_first(html, ".//div[contains(@class, 'navbar')]") - + expect_equal( xpath_text(navbar,".//li[contains(@class, 'active')]//button"), "Articles" @@ -104,7 +104,7 @@ test_that("output is reproducible by default, i.e. 'seed' is respected", { r_code_block("runif(5L)") )) suppressMessages(path <- build_article("test", pkg)) - + html <- xml2::read_html(path) output <- xpath_text(html, "//main//pre")[[2]] expect_snapshot(cat(output)) @@ -133,7 +133,7 @@ test_that("can control math mode", { suppressMessages(path <- build_article("math", pkg)) html <- xml2::read_html(path) expect_equal(xpath_length(html, ".//span[contains(@class, 'math')]"), 1) - + pkg$meta$template$`math-rendering` <- "katex" suppressMessages(init_site(pkg)) suppressMessages(path <- build_article("math", pkg)) @@ -276,6 +276,21 @@ test_that("warns about missing images", { expect_snapshot(build_article("kitten", pkg)) }) +test_that("spaces in sorce paths do work", { + # create simulated package + pkg0 <- local_pkgdown_site() + pkg0 <- pkg_add_file(pkg0, "vignettes/kitten.Rmd", "![Kitten](kitten.jpg)") + pkg0 <- pkg_add_kitten(pkg0, "vignettes") + + # copy simulated pkg to path that contains spaces + pkg1 <- fs::dir_copy(pkg0$src_path, fs::file_temp(pattern = "beware of spaces-")) + + # check that pkgdown site builds anyways + expect_no_error(suppressMessages( + build_article("kitten", as_pkgdown(pkg1)) + )) +}) + test_that("warns about missing alt-text", { pkg <- local_pkgdown_site() pkg <- pkg_add_file(pkg, "vignettes/kitten.Rmd", "![](kitten.jpg)")