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

Fix up knitr paths relative to the output directory #2502

Merged
merged 11 commits into from
Apr 30, 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
5 changes: 4 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Imports:
purrr (>= 1.0.0),
ragg,
rlang (>= 1.1.0),
rmarkdown (>= 1.1.9007),
rmarkdown (>= 2.26.2),
tibble,
whisker,
withr (>= 2.4.3),
Expand All @@ -50,6 +50,7 @@ Suggests:
htmlwidgets,
knitr,
lifecycle,
magick,
methods,
openssl,
pkgload (>= 1.0.2),
Expand All @@ -71,3 +72,5 @@ Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.1
SystemRequirements: pandoc
Remotes:
rstudio/rmarkdown
5 changes: 4 additions & 1 deletion R/rmarkdown.R
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,10 @@ rmarkdown_render_with_seed <- function(..., seed = NULL) {
# envir$.Random.seed <- .GlobalEnv$.Random.seed
# }
}

# Ensure paths from output are not made relative to input
# https://github.com/yihui/knitr/issues/2171
options(knitr.graphics.rel_path = FALSE)

rmarkdown::render(envir = globalenv(), ...)
}

Expand Down
17 changes: 0 additions & 17 deletions tests/testthat/_snaps/build-articles.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,3 @@
# links to man/figures are automatically relocated

Code
copy_figures(pkg)
Message
Copying man/figures/kitten.jpg to reference/figures/kitten.jpg

---

Code
build_articles(pkg, lazy = FALSE)
Message
-- Building articles -----------------------------------------------------------
Writing `articles/index.html`
Reading vignettes/kitten.Rmd
Writing `articles/kitten.html`

# warns about missing images

Code
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,24 @@ knitr::opts_chunk$set(
knitr::include_graphics("../man/figures/kitten.jpg")
```

``` {r}
knitr::include_graphics("another-kitten.jpg")
```

## rmarkdown

![](../man/figures/kitten.jpg)

## Another kitten

![](another-kitten.jpg)

## External package

```{r magick}
magick::image_read("another-kitten.jpg")
```

## Plot

```{r plot}
plot(1:3)
```
17 changes: 12 additions & 5 deletions tests/testthat/test-build-articles.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,28 @@ test_that("can recognise intro variants", {
expect_true(article_is_intro("articles/pack-age", "pack.age"))
})

test_that("links to man/figures are automatically relocated", {
test_that("image links relative to output", {
# weird path differences that I don't have the energy to dig into
skip_on_cran()
pkg <- local_pkgdown_site(test_path("assets/man-figures"))
pkg <- local_pkgdown_site(test_path("assets/articles-images"))

expect_snapshot(copy_figures(pkg))
expect_snapshot(build_articles(pkg, lazy = FALSE))
suppressMessages(copy_figures(pkg))
suppressMessages(build_article("kitten", pkg))

html <- xml2::read_html(path(pkg$dst_path, "articles", "kitten.html"))
src <- xpath_attr(html, "//img", "src")

expect_equal(src, c(
# knitr::include_graphics()
"../reference/figures/kitten.jpg",
"another-kitten.jpg",
# rmarkdown image
"../reference/figures/kitten.jpg",
"another-kitten.jpg"
"another-kitten.jpg",
# magick::image_read()
"kitten_files/figure-html/magick-1.png",
# figure
"kitten_files/figure-html/plot-1.jpg"
))

# And files aren't copied
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ test_that("package_vignettes() sorts articles alphabetically by file name", {
})

test_that("override works correctly for as_pkgdown", {
pkgdown <- as_pkgdown("assets/man-figures")
pkgdown <- as_pkgdown(test_path("assets/articles-images"))
expected_list <- list(figures = list(dev = "jpeg", fig.ext = "jpg", fig.width = 3, fig.asp = 1))
expect_equal(pkgdown$meta, expected_list)
modified_pkgdown <- as_pkgdown(pkgdown, override = list(figures = list(dev = "png")))
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-preview.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
test_that("checks its inputs", {
pkg <- local_pkgdown_site(test_path("assets/man-figures"))
pkg <- local_pkgdown_site(test_path("assets/articles-images"))

expect_snapshot(error = TRUE, {
preview_site(pkg, path = 1)
Expand Down
Loading