Debian Codegraph @

tests/testthat/test-html_document_base.R

raw
1 test_that("Absolute path for image from output directory are made relative to output directory", {
2 skip_if_not_pandoc("2.0")
3 skip_if_not_installed("xml2")
4 skip_if_not_installed("withr")
5 rmd <- local_rmd_file(c(
6 "---",
7 " title: test",
8 "---",
9 "",
10 "```{r}",
11 "#| label: veryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryverylongfilename",
12 "plot(mtcars)",
13 "```"
14 ))
15 # Forcing absolute path for input and output to trigger absolute path for images
16 out <- rmarkdown::render(rmd, "html_document", output_options = list(self_contained = FALSE), output_dir = dirname(rmd), quiet = TRUE)
17 html <- xml2::read_html(out)
18 img <- xml2::xml_find_first(html, ".//img")
19 # is the path correctly made relative to _files ?
20 expect_match(xml2::xml_attr(img, "src"), sprintf("^%s", knitr_files_dir(basename(rmd))))
21 })
22
23
24 test_that("Absolute path from output_dir from img tag created through pandoc are made relative to output_dir", {
25 skip_if_not_pandoc("2.0")
26 skip_if_not_installed("xml2")
27 skip_if_not_installed("withr")
28 # from https://github.com/r-lib/pkgdown/issues/2341
29 # Issue happens because
30 # - knitr::hook_plot_md_base() will use markdown syntax when no width and height are provided (+ other contextt). Pandoc will convert and add a linebreak
31 # due to its wrapping config --wrap=auto from pandoc 2.17 which could break our processing
32 # - This won't be the case for .png file for example, as a out.width is computed by knitr internally which trigger <img src=> tag use
33 # - This is why we recreate using gif below
34 # 1. Take the gif from package
35 # 2. Copy it to the fig_path() which will be in output directory
36 # 3. Use include_graphics to include it in knitr, keeping absolute path
37 rmd <- local_rmd_file(c(
38 "---",
39 " title: test",
40 "---",
41 "",
42 "```{r}",
43 "#| label: veryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryverylongfilename",
44 "gif <- system.file('rmd/h/rmarkdown/rmd_loader.gif', package = 'rmarkdown')",
45 "stopifnot(file.exists(gif))",
46 "file <- knitr::fig_path('.gif')",
47 "dir.create(dirname(file), showWarnings = FALSE, recursive = TRUE)",
48 "file.copy(gif, file)",
49 "knitr::include_graphics(file, rel_path = FALSE)",
50 "```"
51 ))
52 out <- rmarkdown::render(rmd, "html_document", output_options = list(self_contained = FALSE, keep_md = TRUE), output_dir = dirname(rmd), quiet = TRUE)
53 html <- xml2::read_html(out)
54 img <- xml2::xml_find_first(html, ".//img")
55 # # is the path correctly made relative to _files ?
56 expect_match(xml2::xml_attr(img, "src"), sprintf("^%s", knitr_files_dir(basename(rmd))))
57 })