Skip to content

Commit c3648de

Browse files
committed
report missing alt text in vignettes
1 parent a6abe43 commit c3648de

2 files changed

Lines changed: 17 additions & 2 deletions

File tree

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
* When previewing a site, it is now served via a local http server. This enables dynamic features such as search to work correctly (@shikokuchuo, #2975).
44

5+
* `build_site()` now checks vignettes for missing images and alt-text, and also flags empty `alt=""` as missing (#2985).
6+
57
* do not autolink code that is in a link (href) in Rd files (#2972)
68

79
# pkgdown 2.2.0

R/check-built.R

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,23 @@ check_built_site <- function(pkg = ".") {
66
if (!is.null(index_path)) {
77
check_missing_images(pkg, index_path, "index.html")
88
}
9+
10+
if (NROW(pkg$vignettes) > 0) {
11+
for (i in seq_len(nrow(pkg$vignettes))) {
12+
vig <- pkg$vignettes[i, ]
13+
check_missing_images(pkg, vig$file_in, vig$file_out)
14+
}
15+
}
916
}
1017

1118
check_missing_images <- function(pkg, src_path, dst_path) {
1219
html <- xml2::read_html(path(pkg$dst_path, dst_path), encoding = "UTF-8")
1320
img <- xml2::xml_find_all(html, ".//img")
21+
22+
# Exclude hex logo (class="logo"), which intentionally has no alt-text
23+
is_hex_logo <- grepl("\\blogo\\b", xml2::xml_attr(img, "class"), perl = TRUE)
24+
img <- img[!is_hex_logo]
25+
1426
src <- xml2::xml_attr(img, "src")
1527

1628
rel_src <- xml2::url_unescape(src[xml2::url_parse(src)$scheme == ""])
@@ -26,8 +38,9 @@ check_missing_images <- function(pkg, src_path, dst_path) {
2638
}
2739

2840
alt <- xml2::xml_attr(img, "alt")
29-
if (anyNA(alt)) {
30-
problems <- src[is.na(alt)]
41+
missing_alt <- is.na(alt) | alt == ""
42+
if (any(missing_alt)) {
43+
problems <- src[missing_alt]
3144
problems[grepl("^data:image", problems)] <- "<base64 encoded image>"
3245
cli::cli_inform(c(
3346
x = "Missing alt-text in {.file {path_rel(src_path, pkg$src_path)}}",

0 commit comments

Comments
 (0)