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
The minimum supported PHP version was increased from 7.1 to 7.4.
CommonMarkConverter and GithubFlavoredMarkdownConverter constructorsThe constructor methods for both CommonMarkConverter and GithubFlavoredMarkdownConverter no longer accept passing in a customized Environment. If you want to customize the extensions used in your converter you should switch to using MarkdownConverter. See the Basic Usage documentation for an example.
-use League\CommonMark\CommonMarkConverter;
use League\CommonMark\Environment\Environment;
use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;
use League\CommonMark\Extension\Table\TableExtension;
+use League\CommonMark\MarkdownConverter;
$config = [
'html_input' => 'escape',
'allow_unsafe_links' => false,
'max_nesting_level' => 100,
];
-$environment = new Environment();
+$environment = new Environment($config);
$environment->addExtension(new CommonMarkCoreExtension());
$environment->addExtension(new TableExtension());
-$converter = new CommonMarkConverter($config, $environment); // or GithubFlavoredMarkdownConverter
+$converter = new MarkdownConverter($environment);
echo $converter->convertToHtml('Hello World!');
CommonMarkConverter Return TypeIn 1.x, calling convertToHtml() would return a string. In 2.x this changed to return a RenderedContentInterface. To get the resulting HTML, either cast the return value to a string or call ->getContent(). (This new interface extends from Stringable so you can type hint against that instead, if needed.)
use League\CommonMark\CommonMarkConverter;
$converter = new CommonMarkConverter();
-echo $converter->convertToHtml('# Hello World!');
+echo $converter->convertToHtml('# Hello World!')->getContent();
+// or
+echo (string) $converter->convertToHtml('# Hello World!');
Table of Contents items are no longer wrapped with <p> tags:
<ul class="table-of-contents">
<li>
- <p><a href="#level-2-heading">Level 2 Heading</a></p>
+ <a href="#level-2-heading">Level 2 Heading</a>
</li>
<li>
- <p><a href="#level-4-heading">Level 4 Heading</a></p>
+ <a href="#level-4-heading">Level 4 Heading</a>
</li>
<li>
- <p><a href="#level-3-heading">Level 3 Heading</a></p>
+ <a href="#level-3-heading">Level 3 Heading</a>
</li>
</ul>
See #613 for details.
Additionally, the HTML (including URL fragments) for Heading Permalinks have changed:
-<h1><a id="user-content-hello-world" href="#hello-world" name="hello-world" class="heading-permalink" aria-hidden="true" title="Permalink">¶</a>Hello World!</h1>
+<h1><a id="content-hello-world" href="#content-hello-world" class="heading-permalink" aria-hidden="true" title="Permalink">¶</a>Hello World!</h1>
Note that the href now targets the id attribute instead of the name, which is deprecated in HTML 5. Additionally, the default prefix has changed to content. See the Heading Permalink extension documentation for more details on how to configure the prefixes.
Several configuration options now have new names:
| Old Key/Path | New Key/Path | Notes |
|---|---|---|
enable_em |
commonmark/enable_em |
|
enable_strong |
commonmark/enable_strong |
|
use_asterisk |
commonmark/use_asterisk |
|
use_underscore |
commonmark/use_underscore |
|
unordered_list_markers |
commonmark/unordered_list_markers |
Empty arrays no longer allowed |
heading_permalink/id_prefix |
(unchanged) | Default is now content |
heading_permalink/inner_contents |
heading_permalink/symbol |
|
heading_permalink/slug_normalizer |
slug_normalizer/instance |
|
max_nesting_level |
(unchanged) | Only integer values are supported |
mentions/*/symbol |
mentions/*/prefix |
|
mentions/*/regex |
mentions/*/pattern |
Cannot contain start/end / delimiters |
Return types have been added to virtually all class and interface methods. If you implement or extend anything from this library, ensure you also have the proper return types added.
The following classes have been moved to the league/config package:
| Old Class Namespace/Name (1.x) | Moved To |
|---|---|
League\CommonMark\Util\ConfigurationAwareInterface |
League\Config\ConfigurationAwareInterface |
League\CommonMark\Util\ConfigurationInterface |
League\Config\ConfigurationInterface |
League\CommonMark\Util\Configuration |
League\Config\Configuration |
Many classes now live in different namespaces, and some have also been renamed. Here's a quick guide showing their new locations:
(Note that the base namespace of League\CommonMark has been omitted from this table for brevity.)
| Old Class Namespace/Name (1.x) | New Class Namespace/Name (2.0) |
|---|---|
ConfigurableEnvironmentInterface |
Environment\ConfigurableEnvironmentInterface |
EnvironmentAwareInterface |
Environment\EnvironmentAwareInterface |
Environment |
Environment\Environment |
EnvironmentInterface |
Environment\EnvironmentInterface |
Extension\CommonMarkCoreExtension |
Extension\CommonMark\CommonMarkCoreExtension |
Delimiter\Processor\EmphasisDelimiterProcessor |
Extension\CommonMark\Delimiter\Processor\EmphasisDelimiterProcessor |
Block\Element\BlockQuote |
Extension\CommonMark\Node\Block\BlockQuote |
Block\Element\FencedCode |
Extension\CommonMark\Node\Block\FencedCode |
Block\Element\Heading |
Extension\CommonMark\Node\Block\Heading |
Block\Element\HtmlBlock |
Extension\CommonMark\Node\Block\HtmlBlock |
Block\Element\IndentedCode |
Extension\CommonMark\Node\Block\IndentedCode |
Block\Element\ListBlock |
Extension\CommonMark\Node\Block\ListBlock |
Block\Element\ListData |
Extension\CommonMark\Node\Block\ListData |
Block\Element\ListItem |
Extension\CommonMark\Node\Block\ListItem |
Block\Element\ThematicBreak |
Extension\CommonMark\Node\Block\ThematicBreak |
Inline\Element\AbstractWebResource |
Extension\CommonMark\Node\Inline\AbstractWebResource |
Inline\Element\Code |
Extension\CommonMark\Node\Inline\Code |
Inline\Element\Emphasis |
Extension\CommonMark\Node\Inline\Emphasis |
Inline\Element\HtmlInline |
Extension\CommonMark\Node\Inline\HtmlInline |
Inline\Element\Image |
Extension\CommonMark\Node\Inline\Image |
Inline\Element\Link |
Extension\CommonMark\Node\Inline\Link |
Inline\Element\Strong |
Extension\CommonMark\Node\Inline\Strong |
Block\Parser\BlockQuoteParser |
Extension\CommonMark\Parser\Block\BlockQuoteParser |
Block\Parser\FencedCodeParser |
Extension\CommonMark\Parser\Block\FencedCodeParser |
Block\Parser\ATXHeadingParser and Block\Parser\SetExtHeadingParser |
Extension\CommonMark\Parser\Block\HeadingParser |
Block\Parser\HtmlBlockParser |
Extension\CommonMark\Parser\Block\HtmlBlockParser |
Block\Parser\IndentedCodeParser |
Extension\CommonMark\Parser\Block\IndentedCodeParser |
Block\Parser\ListParser |
Extension\CommonMark\Parser\Block\ListParser |
Block\Parser\ThematicBreakParser |
Extension\CommonMark\Parser\Block\ThematicBreakParser |
Inline\Parser\AutolinkParser |
Extension\CommonMark\Parser\Inline\AutolinkParser |
Inline\Parser\BacktickParser |
Extension\CommonMark\Parser\Inline\BacktickParser |
Inline\Parser\BangParser |
Extension\CommonMark\Parser\Inline\BangParser |
Inline\Parser\CloseBracketParser |
Extension\CommonMark\Parser\Inline\CloseBracketParser |
Inline\Parser\EntityParser |
Extension\CommonMark\Parser\Inline\EntityParser |
Inline\Parser\EscapableParser |
Extension\CommonMark\Parser\Inline\EscapableParser |
Inline\Parser\HtmlInlineParser |
Extension\CommonMark\Parser\Inline\HtmlInlineParser |
Inline\Parser\OpenBracketParser |
Extension\CommonMark\Parser\Inline\OpenBracketParser |
Block\Renderer\BlockQuoteRenderer |
Extension\CommonMark\Renderer\Block\BlockQuoteRenderer |
Block\Renderer\FencedCodeRenderer |
Extension\CommonMark\Renderer\Block\FencedCodeRenderer |
Block\Renderer\HeadingRenderer |
Extension\CommonMark\Renderer\Block\HeadingRenderer |
Block\Renderer\HtmlBlockRenderer |
Extension\CommonMark\Renderer\Block\HtmlBlockRenderer |
Block\Renderer\IndentedCodeRenderer |
Extension\CommonMark\Renderer\Block\IndentedCodeRenderer |
Block\Renderer\ListBlockRenderer |
Extension\CommonMark\Renderer\Block\ListBlockRenderer |
Block\Renderer\ListItemRenderer |
Extension\CommonMark\Renderer\Block\ListItemRenderer |
Block\Renderer\ThematicBreakRenderer |
Extension\CommonMark\Renderer\Block\ThematicBreakRenderer |
Inline\Renderer\CodeRenderer |
Extension\CommonMark\Renderer\Inline\CodeRenderer |
Inline\Renderer\EmphasisRenderer |
Extension\CommonMark\Renderer\Inline\EmphasisRenderer |
Inline\Renderer\HtmlInlineRenderer |
Extension\CommonMark\Renderer\Inline\HtmlInlineRenderer |
Inline\Renderer\ImageRenderer |
Extension\CommonMark\Renderer\Inline\ImageRenderer |
Inline\Renderer\LinkRenderer |
Extension\CommonMark\Renderer\Inline\LinkRenderer |
Inline\Renderer\StrongRenderer |
Extension\CommonMark\Renderer\Inline\StrongRenderer |
Extension\SmartPunct\PunctuationParser |
Extension\SmartPunct\DashParser and Extension\SmartPunct\EllipsesParser |
Extension\TableOfContents\TableOfContents |
Extension\TableOfContents\Node\TableOfContents |
Block\Element\AbstractBlock |
Node\Block\AbstractBlock |
Block\Element\Document |
Node\Block\Document |
Block\Element\InlineContainerInterface |
Node\Block\InlineContainerInterface |
Block\Element\Paragraph |
Node\Block\Paragraph |
Block\Element\StringContainerInterface |
Node\StringContainerInterface |
Inline\Element\AbstractInline |
Node\Inline\AbstractInline |
Inline\Element\AbstractStringContainer |
Node\Inline\AbstractStringContainer |
Inline\AdjacentTextMerger |
Node\Inline\AdjacentTextMerger |
Inline\Element\Newline |
Node\Inline\Newline |
Inline\Element\Text |
Node\Inline\Text |
Block\Parser\BlockParserInterface |
Parser\Block\BlockContinueParserInterface and Parser\Block\BlockStartParserInterface |
Block\Parser\LazyParagraphParser |
Parser\Block\ParagraphParser |
Cursor |
Parser\Cursor |
DocParser |
Parser\MarkdownParser |
DocParserInterface |
Parser\MarkdownParserInterface |
Inline\Parser\InlineParserInterface |
Parser\Inline\InlineParserInterface |
Inline\Parser\NewlineParser |
Parser\Inline\NewlineParser |
InlineParserContext |
Parser\InlineParserContext |
InlineParserEngine |
Parser\InlineParserEngine |
Block\Renderer\DocumentRenderer |
Renderer\Block\DocumentRenderer |
Block\Renderer\ParagraphRenderer |
Renderer\Block\ParagraphRenderer |
ElementRendererInterface |
Renderer\ChildNodeRendererInterface |
HtmlRenderer |
Renderer\HtmlRenderer |
Inline\Renderer\NewlineRenderer |
Renderer\Inline\NewlineRenderer |
Inline\Renderer\TextRenderer |
Renderer\Inline\TextRenderer |
Block\Renderer\BlockRendererInterface and Inline\Renderer\InlineRendererInterface |
Renderer\NodeRendererInterface |
HtmlElement |
Util\HtmlElement |
We've completely changed how block parsing works in 2.0. In a nutshell, 1.x had parsing responsibilities split between the parser and the node. But nodes should be "dumb" and not know anything about how they are parsed - they should only know the bare minimum needed for rendering.
As a result, 2.x has delegated the parsing responsibilities to two different interfaces:
| Responsibility | Old Method (1.x) | New Method (2.0) |
|---|---|---|
| Identifying the start of a block | BlockParserInterface::parse() |
BlockStartParserInterface::tryStart() |
| Instantiating and configuring the new... |
How can I help you explore Laravel packages today?