Extensions provide a simple way to add new syntax and features to the CommonMark parser.
Starting with version
1.3.0, this library includes several extensions to support GitHub Flavored Markdown (GFM) and
many other common use-cases. Most of these extensions started out as 3rd-party community based extensions that have
since been officially adopted by this library in an effort to ensure future compatibility and to provide an easy way
to enhance your experience out-of-the-box depending on your specific use-cases.
|Autolinks||Enables automatic linking of URLs within text without needing to wrap them with Markdown syntax||
|Disallowed Raw HTML||Disables certain kinds of HTML tags that could affect page rendering||
|External Links||Tags external links with additional markup||
|GitHub Flavored Markdown||Enables full support for GFM. Automatically includes the extensions noted in the
|Inlines Only||Only includes standard CommonMark inline elements - perfect for handling comments and other short bits of text where you only want bold, italic, links, etc.||
|Strikethrough||Allows using tilde characters (
|Tables||Enables you to create HTML tables||
|Task Lists||Allows the creation of task lists||
|Smart Punctuation||Intelligently converts ASCII quotes, dashes, and ellipses to their fancy Unicode equivalents||
You can enable extensions by simply calling
->addExtension() on the Environment.
In an effort to streamline the extensions used in GitHub Flavored Markdown (GFM), a special extension named
GithubFlavoredMarkdownExtension can be used that will automatically add all the extensions checked in the
column above for you:
use League\CommonMark\CommonMarkConverter; use League\CommonMark\Environment; use League\CommonMark\Extension\GithubFlavoredMarkdownExtension; $environment = Environment::createCommonMarkEnvironment(); $environment->addExtension(new GithubFlavoredMarkdownExtension()); $converter = new CommonMarkConverter(, $environment); echo $converter->convertToHtml('Hello World!');
Or maybe you only want a subset of GFM extensions, plus the Smart Punctuation extension:
use League\CommonMark\CommonMarkConverter; use League\CommonMark\Environment; use League\CommonMark\Extension\Autolink\AutolinkExtension; use League\CommonMark\Extension\DisallowedRawHtml\DisallowedRawHtmlExtension; use League\CommonMark\Extension\SmartPunct\SmartPunctExtension; use League\CommonMark\Extension\Strikethrough\StrikethroughExtension; use League\CommonMark\Extension\Table\TableExtension; $environment = Environment::createCommonMarkEnvironment(); $environment->addExtension(new AutolinkExtension()); $environment->addExtension(new DisallowedRawHtmlExtension()); $environment->addExtension(new SmartPunctExtension()); $environment->addExtension(new StrikethroughExtension()); $environment->addExtension(new TableExtension()); $converter = new CommonMarkConverter(, $environment); echo $converter->convertToHtml('Hello World!');
The extension system makes it easy to mix-and-match extensions to fit your needs.
Writing Custom Extensions
See the Custom Extensions page for details on how you can create your own custom extensions.