if (Sys.getenv("QUARTO_PROJECT_RENDER_ALL") != "1") {
  quit(save = "no")
}

output_dir <- Sys.getenv("QUARTO_PROJECT_OUTPUT_DIR")
stopifnot(nzchar(output_dir))

pkgdown::build_site_github_pages(
  pkg = "../r",
  dest_dir = file.path(normalizePath(output_dir, mustWork = TRUE), "r"),
  install = TRUE,
  examples = FALSE
)

# pkgdown 2.2.1 emits {"path": []} for sections with no indexed content,
# including See also. These are not search results and have no URL to publish.
search_file <- file.path(output_dir, "r", "search.json")
search <- Filter(
  function(entry) !identical(entry$path, list()),
  jsonlite::read_json(search_file)
)
jsonlite::write_json(search, search_file, auto_unbox = TRUE)

# Repository links in the package README target guides beside the r/ subsite.
home <- file.path(output_dir, "r", "index.html")
html <- readLines(home, encoding = "UTF-8", warn = FALSE)
html <- gsub('href="../docs/', 'href="../', html, fixed = TRUE)
writeLines(html, home, useBytes = TRUE)
