Skip to content

Instantly share code, notes, and snippets.

@matt-dray
Last active August 21, 2023 07:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matt-dray/cdc0a49bee2a9ee516972e542bd5e730 to your computer and use it in GitHub Desktop.
Save matt-dray/cdc0a49bee2a9ee516972e542bd5e730 to your computer and use it in GitHub Desktop.
Get all the categories from Quarto blog posts (between 'categories: ' and second instance of '---')
posts <-
list.files("posts", pattern = ".qmd", recursive = TRUE, full.names = TRUE)
get_categories <- function(post_path, ignore_rx = "resources") {
post_lines <- readLines(post_path, warn = FALSE)
cats_start <- which(post_lines == "categories:") + 1
cats_end <- which(post_lines == "---")[2] - 1
cats <- NULL
if (length(cats_start) == 1 & length(cats_end) == 1) {
cats <- gsub(" - ", "", post_lines[cats_start:cats_end])
cats <- cats[!grepl(ignore_rx, cats)]
}
return(cats)
}
cats_list <- lapply(posts, \(post) get_categories(post)) |>
setNames(dirname(post_names))
all_cats <- unlist(cats_list)
table(all_cats) |> sort(decreasing = TRUE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment