Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Cmark Laravel Package

commonmark/cmark

Fast, standards-compliant C implementation of CommonMark Markdown. Parse Markdown to an AST and render to HTML (and other formats) with a stable API, full spec test coverage, and high performance—ideal for embedding in apps, CLIs, and tooling.

Deep Wiki
Context7

Getting Started

Start by installing the package via Composer: composer require commonmark/cmark. Since this is a PHP extension binding, ensure the cmark extension is installed and enabled in your PHP environment (e.g., via pecl install cmark or your OS package manager). Verify installation with php -m | grep cmark.
The most immediate use case is rendering Markdown to HTML:

$parser = new \Cmark\Parser();
$ast = $parser->parseFile('README.md');
$html = \Cmark\Renderer::renderHtml($ast);

Check the examples/ directory in the repository for minimal working demos.

Implementation Patterns

  • Batch processing: Pre-parse Markdown into AST once, then reuse across multiple renderers (e.g., HTML, plain text, or JSON) to avoid redundant parsing.
  • Custom renderers: Extend \Cmark\Renderer to inject behavior like syntax highlighting, link rewriting, or access control (e.g., strip ![]() for unauthenticated users).
  • CLI integration: Use the bundled CLI tool (cmark) for shell scripts or build processes (e.g., cmark README.md > output.html).
  • Framework integration: Wrap in Laravel service providers for dependency injection—e.g., bind Cmark\ParserInterface and Cmark\RendererInterface to leverage auto-wiring.
  • AST traversal: Walk the node tree manually for advanced features like table of contents generation, inline metadata extraction, or custom admonitions.

Gotchas and Tips

  • Extension dependency: This package depends on the native cmark C library and PHP extension—both must be installed separately. Without them, Composer installs succeed but runtime fails with class not found or undefined symbol errors.
  • AST immutability: The parser returns a read-only AST. To modify content, rebuild the AST using Parser::parseString() after editing the source Markdown.
  • Encoding pitfalls: Ensure your Markdown is UTF-8 encoded. Non-UTF-8 inputs can cause silent truncation or segmentation faults in older cmark versions.
  • Performance trade-off: While faster than pure-PHP parsers (e.g., league/commonmark), parsing large documents (100k+ lines) still benefits from caching ASTs or using lazy rendering.
  • Custom extensions: The underlying cmark library supports extension modules (e.g., tables, task lists). Register them via Parser::parseWithExtensions() and ensure the cmark build includes them.
  • Debugging AST: Use \Cmark\Renderer::renderCommonMark($ast) to output normalized CommonMark as a sanity check—great for diffing expected vs actual behavior.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport