twig/markdown-extra
Twig extension adding Markdown conversion filters: markdown_to_html to render Markdown as HTML, and html_to_markdown to convert HTML back to Markdown. Useful for templating content workflows where Markdown and HTML need to interoperate.
Pros:
markdown_to_html/html_to_markdown) enable clean content workflows.league/html-to-markdown for HTML-to-Markdown conversion, avoiding reinvention. MIT license ensures no legal barriers.Cons:
tightenco/jigsaw), which introduces architectural complexity. For Laravel, alternatives like cebe/markdown or league/commonmark are superior.html_to_markdown filter may not preserve complex HTML (e.g., nested tables, scripts), requiring manual validation or post-processing.Technical Risk:
league/html-to-markdown).html-sanitizer) and input validation for user-generated content.composer.json overrides or aliases.Key Questions:
markdown_to_html is needed, alternatives like league/commonmark may suffice with less risk.html_to_markdown.league/commonmark)?TwigExtraBundle with minimal configuration. Ideal for Symfony or custom Twig stacks.tightenco/jigsaw). For Laravel, prioritize:
cebe/markdown: Blade-compatible Markdown parser.league/commonmark: Direct PHP integration for full CommonMark support.Str::markdown(): For simple use cases.Dependency Setup:
twig/markdown-extra and explicitly require league/html-to-markdown (not auto-installed).composer require twig/markdown-extra league/html-to-markdown "^5.0" --with-all-dependencies
composer.json overrides if Twig version conflicts arise:
"extra": {
"laravel": {
"dont-discover": ["twig/markdown-extra"]
}
}
Filter Registration:
TwigExtraBundle in config/packages/twig.yaml:
twig:
extra:
markdown: true
use Twig\Extension\MarkdownExtraExtension;
public function register()
{
$this->app->make('twig')->addExtension(new MarkdownExtraExtension());
}
Template Integration:
{# Render Markdown to HTML #}
{{ markdown_content|markdown_to_html }}
{# Convert HTML to Markdown #}
{{ html_content|html_to_markdown }}
return view('template', ['content' => '**Bold Text**']);
Testing Strategy:
markdown_to_html with edge cases (e.g., nested lists, code blocks).
$twig = new \Twig\Environment($loader);
$twig->addExtension(new MarkdownExtraExtension());
$this->assertEquals(
'<p>Rendered <strong>Markdown</strong></p>',
$twig->createTemplate('{{ content|markdown_to_html }}')->render(['content' => '**Markdown**'])
);
html_to_markdown with real-world HTML to document lossiness.TwigExtraBundle.cebe/markdown.league/html-to-markdown v5.x.league/html-to-markdown and test for conflicts.league/commonmark if the package becomes unsustainable.league/html-to-markdown simplifies updates.league/html-to-markdown may introduce breaking changes.league/html-to-markdown, which has its own support risks.How can I help you explore Laravel packages today?