Skip to content

Commit e706979

Browse files
authored
Check readme images after building reference and articles (#2494)
We can't know if the images in a readme exist until we have built the rest of the site. We worked around this a bit for images in `man/figures` but that strategy doesn't work for articles since we actually need to build them. So now we just check in a new step at the end of `build_site()`. Fixes #2194
1 parent 10b398d commit e706979

13 files changed

+82
-51
lines changed

NEWS.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# pkgdown (development version)
22

3+
* `build_home()` no longer checks if the README is missing any images. This check is now performed in `build_site()`, after `build_articles()` so you can refer to images created by vignettes with warnings (#2194).
34
* `build_home()` now includes the contents of `inst/AUTHORS` on the authors page (#2506).
45
* If you put a dropdown menu (e.g. articles) on the right hand side of the navbar, it will now be right aligned. This makes longer titles more likely to stay on the page (#2421).
56
* The title for the "Reference" page is now "Package index" since this page might contain more than just function details (#2181).

R/build-home-index.R

+11-28
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,7 @@
33
build_home_index <- function(pkg = ".", quiet = TRUE) {
44
pkg <- section_init(pkg, depth = 0L)
55

6-
src_path <- path_first_existing(
7-
pkg$src_path,
8-
c("pkgdown/index.md",
9-
"index.md",
10-
"README.md"
11-
)
12-
)
6+
src_path <- path_index(pkg)
137
dst_path <- path(pkg$dst_path, "index.html")
148
data <- data_home(pkg)
159

@@ -34,12 +28,19 @@ build_home_index <- function(pkg = ".", quiet = TRUE) {
3428
logo = logo_path(pkg, depth = 0)
3529
)
3630

37-
copy_figures(pkg)
38-
check_missing_images(pkg, path_rel(src_path, pkg$src_path), "index.html")
39-
4031
invisible()
4132
}
4233

34+
path_index <- function(pkg) {
35+
path_first_existing(
36+
pkg$src_path,
37+
c("pkgdown/index.md",
38+
"index.md",
39+
"README.md"
40+
)
41+
)
42+
}
43+
4344
data_home <- function(pkg = ".") {
4445
pkg <- as_pkgdown(pkg)
4546

@@ -206,21 +207,3 @@ cran_link <- memoise(function(pkg) {
206207

207208
NULL
208209
})
209-
210-
211-
check_missing_images <- function(pkg, src_path, dst_path) {
212-
html <- xml2::read_html(path(pkg$dst_path, dst_path), encoding = "UTF-8")
213-
src <- xml2::xml_attr(xml2::xml_find_all(html, ".//img"), "src")
214-
215-
rel_src <- src[xml2::url_parse(src)$scheme == ""]
216-
rel_path <- fs::path_norm(path(fs::path_dir(dst_path), rel_src))
217-
exists <- fs::file_exists(path(pkg$dst_path, rel_path))
218-
219-
if (any(!exists)) {
220-
paths <- rel_src[!exists]
221-
cli::cli_warn(c(
222-
"Missing images in {.file {src_path}}: {.file {paths}}",
223-
i = "pkgdown can only use images in {.file man/figures} and {.file vignettes}"
224-
))
225-
}
226-
}

R/build.R

+2
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,8 @@ build_site_local <- function(pkg = ".",
467467
build_search(pkg, override = override)
468468
}
469469

470+
check_built_site(pkg)
471+
470472
cli::cli_rule("Finished building pkgdown site for package {.pkg {pkg$package}}")
471473
preview_site(pkg, preview = preview)
472474
}

R/check.R

+27
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,30 @@ check_pkgdown <- function(pkg = ".") {
2121

2222
cli::cli_inform(c("v" = "No problems found."))
2323
}
24+
25+
check_built_site <- function(pkg = ".") {
26+
pkg <- as_pkgdown(pkg)
27+
28+
cli::cli_rule("Checking for problems")
29+
index_path <- path_index(pkg)
30+
if (!is.null(index_path)) {
31+
check_missing_images(pkg, index_path, "index.html")
32+
}
33+
}
34+
35+
check_missing_images <- function(pkg, src_path, dst_path) {
36+
html <- xml2::read_html(path(pkg$dst_path, dst_path), encoding = "UTF-8")
37+
src <- xml2::xml_attr(xml2::xml_find_all(html, ".//img"), "src")
38+
39+
rel_src <- src[xml2::url_parse(src)$scheme == ""]
40+
rel_path <- fs::path_norm(path(fs::path_dir(dst_path), rel_src))
41+
exists <- fs::file_exists(path(pkg$dst_path, rel_path))
42+
43+
if (any(!exists)) {
44+
paths <- rel_src[!exists]
45+
cli::cli_warn(c(
46+
"Missing images in {.file {path_rel(src_path, pkg$src_path)}}: {.file {paths}}",
47+
i = "pkgdown can only use images in {.file man/figures} and {.file vignettes}"
48+
))
49+
}
50+
}

R/rmarkdown.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ render_rmarkdown <- function(pkg, input, output, ..., seed = NULL, copy_images =
8888
dir_create(unique(path_dir(dst)))
8989
file_copy(src, dst, overwrite = TRUE)
9090
}
91-
check_missing_images(pkg, input, output)
91+
check_missing_images(pkg, input_path, output)
9292

9393
invisible(path)
9494
}

tests/testthat/_snaps/build-articles.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
Writing `articles/html-vignette.html`
1010
Condition
1111
Warning:
12-
Missing images in 'vignettes/html-vignette.Rmd': 'foo.png'
12+
Missing images in 'vignettes/html-vignette.Rmd': 'kitten.jpg'
1313
i pkgdown can only use images in 'man/figures' and 'vignettes'
1414

1515
# articles don't include header-attrs.js script

tests/testthat/_snaps/build-home.md

-14
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,6 @@
1616
Writing `authors.html`
1717
Writing `404.html`
1818

19-
# warns about missing images
20-
21-
Code
22-
build_home(pkg)
23-
Message
24-
-- Building home ---------------------------------------------------------------
25-
Writing `authors.html`
26-
Condition
27-
Warning:
28-
Missing images in 'README.md': 'foo.png'
29-
i pkgdown can only use images in 'man/figures' and 'vignettes'
30-
Message
31-
Writing `404.html`
32-
3319
# can build site even if no Authors@R present
3420

3521
Code

tests/testthat/_snaps/check.md

+11
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,14 @@
2424
Message
2525
v No problems found.
2626

27+
# warn about missing images in readme
28+
29+
Code
30+
check_built_site(pkg)
31+
Message
32+
-- Checking for problems -------------------------------------------------------
33+
Condition
34+
Warning:
35+
Missing images in 'README.md': 'articles/kitten.jpg'
36+
i pkgdown can only use images in 'man/figures' and 'vignettes'
37+
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11

2-
![](foo.png)
2+
![](vignettes/kitten.jpg)

tests/testthat/assets/bad-images/vignettes/html-vignette.Rmd

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ title: "test"
66
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")
77
```
88

9-
![](foo.png)
9+
![](kitten.jpg)

tests/testthat/test-build-articles.R

+3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ test_that("image links relative to output", {
3434
})
3535

3636
test_that("warns about missing images", {
37+
# Added in #2509: I can't figure out why this is necessary :(
38+
skip_on_covr()
39+
3740
pkg <- local_pkgdown_site(test_path("assets/bad-images"))
3841
expect_snapshot(build_articles(pkg))
3942
})

tests/testthat/test-build-home.R

-5
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,6 @@ test_that("intermediate files cleaned up automatically", {
2121
)
2222
})
2323

24-
test_that("warns about missing images", {
25-
pkg <- local_pkgdown_site(test_path("assets/bad-images"))
26-
expect_snapshot(build_home(pkg))
27-
})
28-
2924
test_that("can build site even if no Authors@R present", {
3025
skip_if_no_pandoc()
3126

tests/testthat/test-check.R

+23
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,26 @@ test_that("informs if everything is ok", {
2121
pkg <- local_pkgdown_site(test_path("assets/reference"))
2222
expect_snapshot(check_pkgdown(pkg))
2323
})
24+
25+
# built site ---------------------------------------------------------------
26+
27+
test_that("warn about missing images in readme", {
28+
pkg <- local_pkgdown_site(test_path("assets/bad-images"))
29+
suppressMessages(build_home(pkg))
30+
31+
expect_snapshot(check_built_site(pkg))
32+
})
33+
34+
test_that("readme can use images from vignettes", {
35+
pkg <- local_pkgdown_site(test_path("assets/bad-images"))
36+
file_copy(
37+
test_path("assets/articles-images/man/figures/kitten.jpg"),
38+
path(pkg$src_path, "vignettes/kitten.jpg")
39+
)
40+
withr::defer(unlink(path(pkg$src_path, "vignettes/kitten.jpg")))
41+
42+
suppressMessages(build_home(pkg))
43+
suppressMessages(build_articles(pkg))
44+
45+
suppressMessages(expect_no_warning(check_built_site(pkg)))
46+
})

0 commit comments

Comments
 (0)