twig/markdown-extra
Twig extension that adds markdown_to_html and html_to_markdown filters for converting Markdown blocks to HTML and HTML back to Markdown directly in your Twig templates, with configurable parsers.
twig/laravel). It leverages Laravel’s service provider architecture for seamless integration, requiring minimal boilerplate. The bidirectional Markdown/HTML conversion aligns with Laravel’s content-heavy use cases (e.g., CMS, forums, documentation systems).e() filters in Blade), streamlining security.markdown_to_html hooks). This is valuable for domain-specific languages (DSLs) or scientific/technical documentation platforms.league/commonmark (default) and tempest/markdown as underlying Markdown parsers. This reduces vendor lock-in and allows teams to choose the parser that best fits their performance or feature requirements, though the default remains fully compatible with existing setups.twig/laravel (Twig 3.x). No additional configuration required beyond registering the extension in config/twig.php.MarkdownHelper).parsedown/parsedown, michelf/php-markdown) with twig/markdown-extra:^3.29.0.{{ user_input|e }}) with {{ user_input|markdown_to_html }}. Use html_to_markdown for reverse workflows (e.g., HTML-to-Markdown migrations).<script> tags; output should escape)./comments, /wiki) first.{{ input|markdown_to_html(escape: false) }}). Solution: Add a Laravel validation rule (e.g., ValidMarkdown) to reject unescaped content.<iframe> tags). Solution: Implement a whitelist mechanism for trusted HTML blocks.twig/twig:^2.15 or use a compatibility layer.Cache::remember()).league/commonmark and tempest/markdown introduces a minor decision point. Solution: Default to league/commonmark unless specific needs justify the alternative; document the choice clearly.markdown-extra recipe in the Laravel Wiki).twig/laravel, requiring only a service provider registration:
// config/twig.php
'extensions' => [
Twig\MarkdownExtraExtension::class,
],
// app/Helpers/MarkdownHelper.php
function markdown($content) {
return app('twig')->getExtension('markdown_extra')->markdownToHtml($content);
}
symfony/markdown:^6.4 (or league/commonmark by default), ensuring compatibility with Laravel’s modern PHP stack (PHP 8.1+).tempest/markdown as an alternative parser. Laravel teams can select the parser via configuration, though the default remains compatible with existing setups.composer require twig/markdown-extra.storage/framework/cache/ (e.g., for blog posts).parsedown/parsedown) for redundancy.composer require twig/markdown-extra:^3.29.0 --update-with-dependencies
{{ user_comment|e }} {# Old: Manual escaping #}
With:
{{ user_comment|markdown_to_html }} {# New: Auto-escaping #}
{{ legacy_html|html_to_markdown }}
use Illuminate\Validation\Rule;
Rule::markdownSafe(function ($attribute, $value, $fail) {
if (str_contains($value, '<script>')) {
$fail('Markdown contains unsafe HTML.');
}
});
| Laravel Version | Twig Version | Compatibility Notes |
|---|---|---|
| 10.x/11.x | 3.x | Full support. |
| 9.x | 2.x | Test with twig/twig:^2.15. |
| <9.x | 1.x | Not recommended (deprecated Twig 1.x). |
/api/docs).{{ content|markdown_to_html(custom_extension) }}) after core functionality is stable.Cache::remember()). If using tempest/markdown, benchmark against the default parser under expected load.escape: false), untrusted content can execute scripts. Mitigate with validation rules, code reviews, and automated security scans.twig/twig, symfony/markdown, and league/commonmark can break installation. Use Composer’s conflict resolution and test in staging.MarkdownHelper wrapper to ease Blade developers into the pattern.How can I help you explore Laravel packages today?