diff --git a/NEWS.md b/NEWS.md index a1c6cc2aa..55f12573a 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,6 @@ # pkgdown (development version) +* `build_articles()` now reports if you are missing alt-text for any images (#2357). * `build_reference()` now supports `\Sexpr[results=verbatim]` (@bastistician, #2510). * `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). * `build_home()` now includes the contents of `inst/AUTHORS` on the authors page (#2506). diff --git a/R/check.R b/R/check.R index 4a5b57bfa..67b829d8d 100644 --- a/R/check.R +++ b/R/check.R @@ -34,17 +34,27 @@ check_built_site <- function(pkg = ".") { check_missing_images <- function(pkg, src_path, dst_path) { html <- xml2::read_html(path(pkg$dst_path, dst_path), encoding = "UTF-8") - src <- xml2::xml_attr(xml2::xml_find_all(html, ".//img"), "src") + img <- xml2::xml_find_all(html, ".//img") + src <- xml2::xml_attr(img, "src") rel_src <- src[xml2::url_parse(src)$scheme == ""] rel_path <- fs::path_norm(path(fs::path_dir(dst_path), rel_src)) exists <- fs::file_exists(path(pkg$dst_path, rel_path)) - if (any(!exists)) { paths <- rel_src[!exists] - cli::cli_warn(c( - "Missing images in {.file {path_rel(src_path, pkg$src_path)}}: {.file {paths}}", + cli::cli_inform(c( + x = "Missing images in {.file {path_rel(src_path, pkg$src_path)}}: {.file {paths}}", i = "pkgdown can only use images in {.file man/figures} and {.file vignettes}" )) } + + alt <- xml2::xml_attr(img, "alt") + if (any(is.na(alt))) { + problems <- src[is.na(alt)] + problems[grepl("^data:image", problems)] <- " Some words, and then an image like this: ```{r, echo = FALSE} +#| fig-alt: The pkgdown logo knitr::include_graphics("open-graph/logo.png") ``` diff --git a/tests/testthat/test-build-articles.R b/tests/testthat/test-build-articles.R index 043a7109a..475544ad2 100644 --- a/tests/testthat/test-build-articles.R +++ b/tests/testthat/test-build-articles.R @@ -41,6 +41,11 @@ test_that("warns about missing images", { expect_snapshot(build_articles(pkg)) }) +test_that("warns about missing alt-text", { + pkg <- local_pkgdown_site(test_path("assets/missing-alt")) + expect_snapshot(build_article("missing-images", pkg)) +}) + test_that("articles don't include header-attrs.js script", { pkg <- as_pkgdown(test_path("assets/articles")) withr::defer(clean_site(pkg, quiet = TRUE))