# Validates a Quarto series registry and post front matter. # Checks that series IDs, post slugs, link types, and series ordering # resolve without broken references or gaps. # # Run the downloaded file from the directory where you saved it: # Rscript check-registry.R read_registry <- function(path) { if (!file.exists(path)) { stop(sprintf("Registry file '%s' does not exist.", path), call. = FALSE) } data <- yaml::read_yaml(path) if (is.list(data) && !is.null(data$series)) { data$series } else if (is.list(data)) { data } else { list() } } read_front_matter <- function(path) { if (!file.exists(path)) { return(list()) } lines <- readLines(path, warn = FALSE) fences <- which(lines == "---") if (length(fences) < 2) { return(list()) } yaml_text <- paste(lines[(fences[1] + 1):(fences[2] - 1)], collapse = "\n") parsed <- yaml::yaml.load(yaml_text) if (is.list(parsed)) parsed else list() } scan_posts <- function(dir) { if (!dir.exists(dir)) { return(list()) } slugs <- list.files(dir) slugs <- slugs[dir.exists(file.path(dir, slugs))] post_files <- file.path(dir, slugs, "index.qmd") valid <- file.exists(post_files) slugs <- slugs[valid] post_files <- post_files[valid] stats::setNames( lapply(post_files, read_front_matter), slugs ) } check_registry <- function( registry, posts, allowed_types = c("read-first", "read-next", "builds-on", "see-also") ) { problems <- list() record <- function(scope, item, problem) { problems[[length(problems) + 1L]] <<- list( scope = scope, item = item, problem = problem ) } series_ids <- vapply( registry, function(s) if (!is.null(s$id)) as.character(s$id) else "", character(1) ) # Check for duplicate series IDs dup_ids <- series_ids[nzchar(series_ids) & duplicated(series_ids)] for (dup in unique(dup_ids)) { record("registry", dup, sprintf("Duplicate series id '%s'", dup)) } # Validate each series in registry for (s in registry) { sid <- s$id %||% "" if (!is.null(s$status) && !s$status %in% c("live", "planned")) { record( "registry", sid, sprintf("Invalid status '%s' in series '%s'", s$status, sid) ) } if (identical(s$status, "planned")) { if (is.null(s$title) || !nzchar(as.character(s$title))) { record( "registry", sid, sprintf("Planned series '%s' is missing a title", sid) ) } if (is.null(s$summary) || !nzchar(as.character(s$summary))) { record( "registry", sid, sprintf("Planned series '%s' is missing a summary", sid) ) } } for (edge in s$related) { target <- edge$to %||% "" if (!target %in% series_ids) { record( "registry", sid, sprintf("Unknown series link: '%s' references '%s'", sid, target) ) } if (identical(target, s$id)) { record( "registry", sid, sprintf("Series '%s' has a self-link", sid) ) } link_type <- edge$type %||% "" if (!link_type %in% allowed_types) { record( "registry", sid, sprintf("Invalid link type '%s' in series '%s'", link_type, sid) ) } } } # Live series IDs live_series <- vapply( Filter(function(s) identical(s$status, "live"), registry), function(s) as.character(s$id), character(1) ) post_slugs <- names(posts) # Validate each post for (slug in post_slugs) { p <- posts[[slug]] if (!is.null(p$series)) { if (!p$series %in% live_series) { record( "post", slug, sprintf( "Unknown series link: post '%s' references unknown or non-live series '%s'", slug, p$series ) ) } if (is.null(p$order) || !is.numeric(p$order)) { record( "post", slug, sprintf("Post '%s' has missing or non-numeric order", slug) ) } } if (length(p$related) > 3L) { record( "post", slug, sprintf( "Post '%s' has %d related links (maximum is 3)", slug, length(p$related) ) ) } for (r in p$related) { target_post <- r$post %||% "" if (!target_post %in% post_slugs) { record( "post", slug, sprintf( "Broken slug: post '%s' references nonexistent post '%s'", slug, target_post ) ) } if (identical(target_post, slug)) { record("post", slug, sprintf("Post '%s' has a self-link", slug)) } r_type <- r$type %||% "" if (!r_type %in% allowed_types) { record( "post", slug, sprintf("Invalid link type '%s' in post '%s'", r_type, slug) ) } } } # Series order: 1..n without gaps or duplicates in_series <- Filter( function(p) !is.null(p$series) && !is.null(p$order), posts ) if (length(in_series) > 0) { series_groups <- split( vapply(in_series, function(p) as.integer(p$order), integer(1)), vapply(in_series, function(p) as.character(p$series), character(1)) ) for (sid in names(series_groups)) { ords <- series_groups[[sid]] if (anyDuplicated(ords) > 0) { record( "post", sid, sprintf( "Duplicate order in series '%s': parts %s", sid, paste(ords[duplicated(ords)], collapse = ", ") ) ) } sorted_ords <- sort(unname(ords)) if (!identical(sorted_ords, seq_along(ords))) { record( "post", sid, sprintf( "Order gap in series '%s': expected 1..%d, got %s", sid, length(ords), paste(sorted_ords, collapse = ", ") ) ) } } } if (length(problems) == 0) { return(data.frame( scope = character(), item = character(), problem = character(), stringsAsFactors = FALSE )) } do.call(rbind, lapply(problems, as.data.frame, stringsAsFactors = FALSE)) } # Run demo when executed directly if (sys.nframe() == 0L) { demo_dir <- file.path(tempdir(), "check-registry-demo") dir.create( file.path(demo_dir, "_data"), recursive = TRUE, showWarnings = FALSE ) dir.create( file.path(demo_dir, "posts"), recursive = TRUE, showWarnings = FALSE ) on.exit(unlink(demo_dir, recursive = TRUE), add = TRUE) # Sample registry cat( "series:\n", " - id: basics\n", " status: live\n", " track: engineering\n", " related:\n", " - {to: testing, type: read-next}\n", " - id: testing\n", " status: planned\n", " track: engineering\n", " title: 'Testing in R'\n", " summary: 'Unit and snapshot testing'\n", file = file.path(demo_dir, "_data", "series.yml"), sep = "" ) # Sample posts make_post <- function(slug, yaml_content) { pdir <- file.path(demo_dir, "posts", slug) dir.create(pdir, recursive = TRUE, showWarnings = FALSE) cat( "---\n", yaml_content, "---\n\nPost body content.\n", file = file.path(pdir, "index.qmd"), sep = "" ) } make_post( "intro", paste0( "title: 'Introduction'\n", "series: basics\n", "order: 1\n", "related:\n", " - {post: advanced, type: read-next}\n" ) ) # Planted bug: broken slug reference to 'nonexistent-post' make_post( "advanced", paste0( "title: 'Advanced Concepts'\n", "series: basics\n", "order: 2\n", "related:\n", " - {post: nonexistent-post, type: builds-on}\n" ) ) make_post( "orphan-summary", paste0( "title: 'Orphan Summary'\n", "related:\n", " - {post: intro, type: see-also}\n" ) ) registry <- read_registry(file.path(demo_dir, "_data", "series.yml")) posts <- scan_posts(file.path(demo_dir, "posts")) issues <- check_registry(registry, posts) cat("Validated", length(registry), "series and", length(posts), "posts.\n") if (nrow(issues) == 0) { cat("✓ No registry issues detected.\n") } else { cat(sprintf("Found %d issue(s):\n", nrow(issues))) for (i in seq_len(nrow(issues))) { cat(sprintf( " [%s] %s: %s\n", issues$scope[i], issues$item[i], issues$problem[i] )) } } }