Markdown Aggregation
Convert CommonMark and GFM into CommonMark XML 1.0 natively in PHC via comrak and graft it into Xao DomDoc trees.
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]);
| Option | Type | Default | Behavior |
|---|---|---|---|
gfm | bool | false | Enables GFM tables, strikethrough, task lists, and autolinks |
sourcepos | bool | false | Adds sourcepos attributes to XML elements |
unsafe_html | bool | false | Strips/escapes raw HTML blocks and inline HTML when false |
front_matter_delimiter | string | null | Strips 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_breakLists:
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:linebreakTables:
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.