league/commonmark
Highly extensible PHP Markdown parser supporting full CommonMark and GitHub-Flavored Markdown. Convert Markdown to HTML with simple converters, customize rendering via extensions, and run safely with options like stripping HTML and blocking unsafe links.
layout: default title: XML Rendering description: Rendering Markdown documents in XML redirect_from:
Version 2.0 introduced the ability to render Markdown Document objects in XML. This is particularly useful for debugging custom extensions as you can see the XML representation of the Abstract Syntax Tree.
To convert Markdown to XML, you would instantiate a MarkdownToXmlConverter with an Environment and then call convert() on any Markdown.
use League\CommonMark\Environment\Environment;
use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;
use League\CommonMark\Xml\MarkdownToXmlConverter;
$environment = new Environment();
$environment->addExtension(new CommonMarkCoreExtension());
$converter = new MarkdownToXmlConverter($environment);
echo $converter->convert('# **Hello** World!');
This will display XML output like this:
<?xml version="1.0" encoding="UTF-8"?>
<document xmlns="http://commonmark.org/xml/1.0">
<heading level="1">
<strong>
<text>Hello</text>
</strong>
<text> World!</text>
</heading>
</document>
Alternatively, if you already have a Document object you want to visualize in XML, you can use theXmlRenderer class to convert it to XML.
Like with CommonMarkConverter::convert(), the renderDocument() actually returns an instance of League\CommonMark\Output\RenderedContentInterface. You can cast this (implicitly, as shown above, or explicitly) to a string or call getContent() to get the final XML output.
See the rendering documentation for information on customizing the XML output.
How can I help you explore Laravel packages today?