# Offline internal link checker for rendered Quarto, pkgdown, and static HTML sites. # Scans HTML files, extracts href and src attributes, resolves root-relative and # relative links, and flags missing files or directory links without index.html. # # Usage: # Rscript check-links.R _site # # Or run without arguments to execute the built-in demonstration: # Rscript check-links.R #' Simplify path by resolving '.' and '..' segments #' #' @param path Character vector of paths to simplify. #' @return Simplified path string. #' @keywords internal simplify_path <- function(path) { is_abs <- startsWith(path, "/") parts <- strsplit(path, "/+")[[1L]] stack <- character() for (part in parts) { if (part == "" || part == ".") { next } if (part == "..") { if (length(stack) > 0L && stack[length(stack)] != "..") { stack <- stack[-length(stack)] } else if (!is_abs) { stack <- c(stack, part) } } else { stack <- c(stack, part) } } res <- paste(stack, collapse = "/") if (is_abs) paste0("/", res) else res } #' Scan rendered HTML files for broken internal links #' #' @param site_dir Root directory of the rendered static site. #' @return A list containing `broken` (a data frame of broken links with columns #' `page`, `link`, and `resolved_path`) and `checked` (the total count of #' internal links checked). #' @export check_links <- function(site_dir = "_site") { if (!dir.exists(site_dir)) { stop( sprintf("Site directory '%s' does not exist.", site_dir), call. = FALSE ) } norm_site_dir <- normalizePath(site_dir, winslash = "/", mustWork = TRUE) html_files <- list.files( norm_site_dir, pattern = "\\.html$", recursive = TRUE, full.names = TRUE ) bad_pages <- character() bad_links <- character() bad_paths <- character() n_checked <- 0L # Skip external protocols, anchor fragments, data URIs, and scripts skip_pattern <- "^(https?:|mailto:|#|data:|javascript:|tel:)" for (html_file in html_files) { norm_html <- normalizePath(html_file, winslash = "/", mustWork = TRUE) rel_page <- substring(norm_html, nchar(norm_site_dir) + 2L) html_dir <- dirname(norm_html) doc <- tryCatch( xml2::read_html(norm_html, encoding = "UTF-8"), error = function(e) NULL ) if (is.null(doc)) { next } href_nodes <- xml2::xml_find_all(doc, "//*[@href]") hrefs <- xml2::xml_attr(href_nodes, "href") src_nodes <- xml2::xml_find_all(doc, "//*[@src]") srcs <- xml2::xml_attr(src_nodes, "src") raw_links <- c(hrefs, srcs) raw_links <- raw_links[!is.na(raw_links) & nzchar(trimws(raw_links))] for (u in raw_links) { u_trim <- trimws(u) if (grepl(skip_pattern, u_trim, ignore.case = TRUE)) { next } # Drop #fragment and ?query clean_u <- sub("[#?].*$", "", u_trim) p <- tryCatch( utils::URLdecode(clean_u), error = function(e) clean_u ) if (!nzchar(p)) { next } n_checked <- n_checked + 1L # Resolve root-relative vs relative path if (startsWith(p, "/")) { rel_p <- sub("^/+", "", p) target_path <- file.path(norm_site_dir, rel_p) } else { target_path <- file.path(html_dir, p) } norm_target <- simplify_path(target_path) # Check target existence: a folder link must contain index.html is_valid <- if (dir.exists(norm_target)) { idx <- file.path(norm_target, "index.html") file.exists(idx) && !dir.exists(idx) } else { file.exists(norm_target) } if (!is_valid) { bad_pages <- c(bad_pages, rel_page) bad_links <- c(bad_links, u) bad_paths <- c(bad_paths, norm_target) } } } broken_df <- data.frame( page = bad_pages, link = bad_links, resolved_path = bad_paths, stringsAsFactors = FALSE ) attr(broken_df, "checked") <- n_checked list( broken = broken_df, checked = n_checked ) } #' Run demonstration on a temporary mock site #' @return A list containing `broken` and `checked`. #' @export run_link_demo <- function() { demo_dir <- file.path(tempdir(), paste0("link_demo_", Sys.getpid())) dir.create(demo_dir, recursive = TRUE, showWarnings = FALSE) on.exit(unlink(demo_dir, recursive = TRUE), add = TRUE) # 1. Target with encoded space (valid target) writeLines( "
Target with space", file.path(demo_dir, "my file.html") ) # 2. Folder without index.html (broken directory target) templates_dir <- file.path(demo_dir, "templates") dir.create(templates_dir, recursive = TRUE, showWarnings = FALSE) writeLines("plain text", file.path(templates_dir, "README.txt")) # 3. Main HTML page with: # - one broken link ("missing.html") # - one folder link without index.html ("templates/") # - one encoded space ("my%20file.html") writeLines( c( "", "", "