Xao / Technical Fieldnotes

Framework Reference · Issue 1.0.0-dev · 2 min read

Markdown Aggregation

Convert CommonMark and GFM into CommonMark XML 1.0 natively in PHC via comrak and graft it into Xao DomDoc trees.

Audience: Content platform and CMS developersWords: 250Source: content/markdown-aggregation.md
Open library index (12 documents)

PHC's default build compiles the markdown-xml extension (comrak in safe Rust), providing phc_markdown_to_xml() and phc_markdown_to_dom() out of the box. Both emit CommonMark XML 1.0 (http://commonmark.org/xml/1.0) without DOCTYPE declarations so Markdown parses straight into DOMDocument, DOMXPath, Xao\DomDoc, and XSLTProcessor.

Native PHC Functions & Options

$xml = phc_markdown_to_xml($markdown, [
    'gfm' => true,
    'sourcepos' => false,
    'unsafe_html' => false,
    'front_matter_delimiter' => '---',
]);

$dom = phc_markdown_to_dom($markdown, ['gfm' => true, 'unsafe_html' => false]);
OptionTypeDefaultBehavior
gfmboolfalseEnables GFM tables, strikethrough, task lists, and autolinks
sourceposboolfalseAdds sourcepos attributes to XML elements
unsafe_htmlboolfalseStrips/escapes raw HTML blocks and inline HTML when false
front_matter_delimiterstringnullStrips leading frontmatter fenced by the delimiter

Preserving the CommonMark Namespace in DomDoc

When grafting a phc_markdown_to_dom() tree into a larger Xao\DomDoc conglomeration, use ndImportNode() rather than ndConsumeNode() so xmlns="http://commonmark.org/xml/1.0" is preserved for namespaced XSLT templates (xmlns:cm="http://commonmark.org/xml/1.0"):

$mdDoc = new DomDoc(phc_markdown_to_dom($markdownBody, [
    'gfm' => true,
    'unsafe_html' => false,
]));

$page = new DomDoc('xao-docs');
$bodyNode = $page->ndAppendToRoot('body');
$page->ndImportNode($mdDoc->getRoot(), true, $bodyNode);

CommonMark XML Elements for XSLT Authors

CommonMark + GFM emits clean, predictable XML elements:

  • Blocks: cm:heading[@level], cm:paragraph, cm:code_block[@info], cm:block_quote, cm:thematic_break

  • Lists: cm:list[@type, @start, @tasklist], cm:item, cm:taskitem[@completed]

  • Inline: cm:text, cm:emph, cm:strong, cm:code, cm:strikethrough, cm:link[@destination, @title], cm:image[@destination, @title], cm:softbreak, cm:linebreak

  • Tables: cm:table, cm:table_header, cm:table_row, cm:table_cell

Roadmap (P06)

Phase P06 in Xao's roadmap builds first-class Xao\MarkdownToXml and DomDoc::ndConsumeMarkdown() / ndConsumeMarkdownFile() helpers on top of this PHC primitive and integrates Markdown articles directly into the built-in catalog reference app alongside SQLite DbToXml.