league/commonmark
Extensible PHP Markdown parser supporting the full CommonMark spec and GitHub-Flavored Markdown. Works with PHP 7.4+ (mbstring) and provides simple converters to turn Markdown into HTML with configurable safety options.
league/commonmark is a highly extensible CommonMark/GFM parser, making it a strong fit for Laravel applications requiring structured, spec-compliant Markdown rendering (e.g., documentation, CMS content, or user-generated content).markdown() helpers, API responses, or rich-text editors).DisallowedRawHtml, DomainFilteringAdapter) mitigate risks from untrusted input, critical for Laravel’s multi-user environments.composer require league/commonmark; no Laravel-specific bootstrapping required.CommonMarkConverter bound to markdown).@markdown) or global helpers (e.g., markdown($content)).cache() or Redis).allow_unsafe_links: false) mitigates risks but requires customization for trusted HTML.cache()->remember()) is recommended..md files from storage/app)?CommonMarkConverter/GithubFlavoredMarkdownConverter as a singleton or context-bound service.@markdown directive or global markdown() helper.return response()->json(['content' => markdown($request->input('body'))]))..md files from storage/ or public/ (e.g., docs, blog posts).composer require league/commonmark
// app/Helpers/markdown.php
if (!function_exists('markdown')) {
function markdown(string $input): string {
return app(\League\CommonMark\CommonMarkConverter::class)->convert($input);
}
}
// app/Providers/AppServiceProvider.php
public function register() {
$this->app->singleton(\League\CommonMark\CommonMarkConverter::class, function () {
return new \League\CommonMark\CommonMarkConverter([
'html_input' => 'strip',
'allow_unsafe_links' => false,
]);
});
}
// app/Providers/BladeServiceProvider.php
Blade::directive('markdown', function ($expression) {
return "<?php echo markdown({$expression}); ?>";
});
Usage:
@markdown($post->content)
$converter = new CommonMarkConverter();
$converter->addExtension(new \League\CommonMark\Extension\Table\TableExtension());
$converter = new GithubFlavoredMarkdownConverter([
'autolink' => true,
'strikethrough' => true,
]);
mbstring (enabled by default in Laravel).text/longtext columns; avoid mediumtext for large docs (>16KB).cache()->remember('markdown_'.$id, ...)) for frequently accessed content.html_input: 'strip', allow_unsafe_links: false; add DomainFilteringAdapter if needed../vendor/bin/phpunit --testdox (if using tests).composer update league/commonmark --with-dependencies.composer outdated + composer update for security releases (e.g., v2.8.x fixes).@internal methods; rely on public CommonMarkConverter/GithubFlavoredMarkdownConverter.html_input: 'strip' and allow_unsafe_links: false are set../tests/benchmark/benchmark.php; cache parsed results.CommonMarkConverter::getEnvironment()->getExtensions().CommonMarkConverter::getEnvironment()->getParser()->parse() to inspect the Abstract Syntax Tree.try-catch to log malformed Markdown.How can I help you explore Laravel packages today?