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.
league/commonmark is a pure PHP library with no Laravel-specific dependencies, making it highly portable across PHP-based stacks. It integrates seamlessly with Laravel’s dependency injection (via Composer) and service container.html_input/allow_unsafe_links) mitigate XSS risks, critical for user-generated content (e.g., comments, wiki pages).league/commonmark itself is framework-agnostic, Laravel has community packages (e.g., graham-campbell/laravel-markdown) that wrap it for easier integration with Blade templates, Eloquent models, or API responses.@markdown, markdown()) for rendering Markdown in views.Post::markdown_content).html_input/allow_unsafe_links is mandatory for untrusted input (e.g., public forums). HTML Purifier may be needed for additional sanitization.parsedown) already in use that need migration?cebe/markdown.league/commonmark’s defaults?composer require league/commonmark.CommonMarkConverter with GFM extensions) in AppServiceProvider.graham-campbell/laravel-markdown for Blade integration or build a custom facade.GithubFlavoredMarkdownConverter.Environment::addExtension() (e.g., emoji, YouTube iframes).CommonMarkConverter.parsedown) with league/commonmark.html_cache column).v1.x of the package.// app/Providers/AppServiceProvider.php
Blade::directive('markdown', function ($expression) {
return "<?php echo app('markdown')->parse({$expression}); ?>";
});
@markdown($post->content)
return response()->json([
'content' => app('markdown')->parse($post->content),
]);
AppServiceProvider:
$this->app->singleton('markdown', function () {
return new GithubFlavoredMarkdownConverter([
'html_input' => 'strip',
'allow_unsafe_links' => false,
]);
});
PHPUnit).Cache::remember).composer why-not league/commonmark to check for outdated dependencies.config/markdown.php for consistency.enabled_extensions: ['tables', 'tasklists']).MarkdownToXmlConverter) to debug AST issues.Cache::forever() for static content).| Failure Scenario | Mitigation Strategy | Laravel-Specific Fix |
|---|---|---|
| Malicious Markdown (XSS) | Disable html_input and allow_unsafe_links. Use HTML Purifier for extra safety. |
Middleware to sanitize input before parsing. |
| Invalid UTF-8 Input | Validate encoding at the API/database layer. | Use Laravel’s Illuminate\Support\Str::of() to check encoding. |
| Extension Misconfiguration |
How can I help you explore Laravel packages today?