Skip to content
Open
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
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

* 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).

* `build_site()` now checks vignettes for missing images and alt-text, and also flags empty `alt=""` as missing (#2985).

* do not autolink code that is in a link (href) in Rd files (#2972)

# pkgdown 2.2.0
Expand Down
17 changes: 15 additions & 2 deletions R/check-built.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,23 @@ check_built_site <- function(pkg = ".") {
if (!is.null(index_path)) {
check_missing_images(pkg, index_path, "index.html")
}

if (NROW(pkg$vignettes) > 0) {
for (i in seq_len(nrow(pkg$vignettes))) {
vig <- pkg$vignettes[i, ]
check_missing_images(pkg, vig$file_in, vig$file_out)
}
}
}

check_missing_images <- function(pkg, src_path, dst_path) {
html <- xml2::read_html(path(pkg$dst_path, dst_path), encoding = "UTF-8")
img <- xml2::xml_find_all(html, ".//img")

# Exclude hex logo (class="logo"), which intentionally has no alt-text
is_hex_logo <- grepl("\\blogo\\b", xml2::xml_attr(img, "class"), perl = TRUE)
img <- img[!is_hex_logo]

src <- xml2::xml_attr(img, "src")

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

alt <- xml2::xml_attr(img, "alt")
if (anyNA(alt)) {
problems <- src[is.na(alt)]
missing_alt <- is.na(alt) | alt == ""
if (any(missing_alt)) {
problems <- src[missing_alt]
problems[grepl("^data:image", problems)] <- "<base64 encoded image>"
cli::cli_inform(c(
x = "Missing alt-text in {.file {path_rel(src_path, pkg$src_path)}}",
Expand Down
Loading