CommonMark PHP Documentation

website·Indexed 37 days ago

https://commonmark.thephpleague.com/

Documentation for the CommonMark PHP library, including guides for version 1.x and migration paths to 2.x. Covers basic usage, installation, security, and a wide array of extensions such as GitHub-Flavored Markdown, tables, footnotes, and task lists, as well as customization of block and inline parsing and rendering.

Tokens
64.1K
Snippets
279
Records
465
Agent score
70%

What's inside CommonMark

  1. Overview of the CommonMark Event Dispatcher

    The library provides a PSR-14 compliant event dispatcher that allows developers to execute custom code at specific hook points during the Markdown processing and rendering lifecycle. This system is used by both the core library and third-party extensions.
  2. Overview of league/commonmark PHP Markdown Parser

    The league/commonmark library is a robust and highly extensible Markdown parser for PHP. It is the only PHP library fully compatible with both the CommonMark and GitHub-Flavored Markdown (GFM) specifications. It features an extensible architecture that supports custom syntax and provides community integrations for various frameworks and CMSes.
  3. Use GFM support in CommonMark

    Full GitHub Flavored Markdown (GFM) support is included directly in the main library as of version 1.3.0. Users previously relying on separate league/commonmark-ext* packages can now switch to the integrated features.
  4. Enable GFM support in commonmark 1.x

    Full GitHub Flavored Markdown (GFM) support was integrated directly into the main library in version 1.3.0. Users previously relying on separate league/commonmark-ext* packages can now use these features natively.
  5. Convert ASCII punctuation to Unicode using SmartPunctExtension

    The SmartPunctExtension intelligently converts ASCII quotes, dashes, and ellipses to their Unicode equivalents during Markdown parsing. For example, standard double quotes (") are converted to curly quotes (“ ”), and double hyphens (--) are converted to en-dashes (–).
    "CommonMark is the PHP League's Markdown parser," she said. "It's super-configurable... you can even use additional extensions to expand its capabilities -- just like this one!"

    <!-- Results in HTML: -->

    <p>“CommonMark is the PHP League’s Markdown parser,” she said. “It’s super-configurable… you can even use additional extensions to expand its capabilities – just like this one!”</p>

  6. Filter dangerous HTML tags using DisallowedRawHtmlExtension

    The DisallowedRawHtmlExtension automatically filters specific HTML tags by replacing the leading < with the entity &lt; to prevent undesirable side-effects from malicious input. This is a requirement of the GFM spec. The following tags are filtered: <title>, <textarea>, <style>, <xmp>, <iframe>, <noembed>, <noframes>, <script>, and <plaintext>. All other HTML tags remain untouched. Note that this extension is included by default when using the GFM extension.
  7. Extend Markdown parsing with Custom Parsers

    Parsers analyze Markdown input to produce an Abstract Syntax Tree (AST) consisting of blocks (structural elements like headers and lists) and inlines (text, links, emphasis). There are two primary types of parsers: Block parsers and Inline parsers. Both operate by using a Cursor to examine text at the current position; if the parser can handle the text, it creates the corresponding AST element; otherwise, it aborts to allow other parsers to attempt processing. For simple delimiter-based inlines, the Delimiter Processing functionality can be used instead of a full inline parser.
  8. Prevent dangerous HTML tags using DisallowedRawHtmlExtension

    The DisallowedRawHtmlExtension automatically escapes specific HTML tags by replacing the leading < with the &lt; entity. This prevents malicious users from introducing tags that cause undesirable side-effects, as required by the GFM spec. By default, the following tags are escaped: <title>, <textarea>, <style>, <xmp>, <iframe>, <noembed>, <noframes>, <script>, and `<plaintext>.

    Note: This extension is included by default within the GFM extension.

  9. Customize HTML output with Custom Renderers

    Renderers are responsible for converting AST blocks and inlines into HTML. You can replace built-in renderers by registering your own with the Environment. When registering a custom renderer, you must specify which block or inline classes it is intended to handle.
  10. Enable GFM-style strikethrough in league/commonmark

    The Strikethrough extension adds support for GFM-style strikethrough syntax, allowing text wrapped in double tildes (~~) to be rendered within HTML <del> tags. Note that this extension is included by default if you are using the GFM extension.
  11. Use the Cursor class for string parsing

    The Cursor class is a string wrapper that tracks the current parsing position. It provides optimized methods for character inspection, movement, and regex matching. It currently only supports UTF-8 (and ASCII) encoding. When creating custom block or inline parsers, a pre-configured Cursor is automatically provided, already set to the current position being parsed.
    use League\CommonMark\Parser\Cursor;
    
    $cursor = new Cursor('Hello World!');
  12. Convert ASCII punctuation to Unicode using SmartPunctExtension

    The SmartPunctExtension automatically converts ASCII quotes, dashes, and ellipses to their corresponding Unicode equivalents during Markdown parsing. For example, it converts standard double quotes (") to curly quotes (“ ”), double hyphens (--) to en-dashes (–), and triple dots (...) to ellipses (…).