
Generate a KB-importable XML file from a Quarto document
Source:R/generate-kb-xml.R
generate_kb_xml.RdTakes a Quarto document and produces an XML file that is directly
importable into a UW-Madison Knowledge Base (KB) article. The function
re-renders the .qmd with embed-resources: true so all visual assets
are self-contained, extracts the HTML body, and wraps it in the KB XML
structure along with metadata drawn from the document's YAML header.
Arguments
- html_path
A string. Path to the rendered HTML file. Used to infer the output filename and, if
qmd_pathisNULL, the location of the source.qmd.- qmd_path
A string or
NULL. Path to the source.qmdfile. IfNULL(the default), inferred by replacing the.htmlextension ofhtml_pathwith.qmd.- output_dir
A string or
NULL. Directory where the.xmlfile will be written. IfNULL(the default), written to the same directory ashtml_path.
Details
generate_kb_xml() performs the following steps:
Validates that
html_pathexists.Infers
qmd_pathfromhtml_pathif not supplied, then validates it.Extracts
title,description, andcategoriesfrom the.qmdYAML header and maps them tokb_title,kb_summary, andkb_keywords.Re-renders the
.qmdin an isolated temporary directory withembed-resources: trueso all CSS, images, and JS are self-contained. Thedata/andassets/folders are copied alongside the.qmdto ensure the render succeeds.Extracts the
<body>from the embedded HTML.Escapes HTML entities in the body for XML compatibility, as required by the UW-Madison KB import format.
Builds the XML structure with
kb_title,kb_keywords,kb_summary, andkb_bodynodes.Writes the
.xmlfile tooutput_dir.
Temporary files are managed via withr::local_tempdir() and are
automatically cleaned up when the function exits, even on error.
When importing the resulting XML into the KB, check the Decode HTML entity in body content option.
Examples
# \donttest{
# Infer qmd_path automatically, write XML alongside the HTML
# generate_kb_xml(html_path = "docs/analysis.html")
# Supply qmd_path explicitly and write to a specific output directory
# generate_kb_xml(
# html_path = "docs/analysis.html",
# qmd_path = "analysis.qmd",
# output_dir = "exports"
# )
# }