Installation Run:
composer require demontpx/parsedown-bundle ^1.3
Register the bundle in config/bundles.php (Symfony 4+):
return [
// ...
Demontpx\ParsedownBundle\DemontpxParsedownBundle::class => ['all' => true],
];
(No AppKernel.php needed for Symfony 4+.)
First Use Case Use the Twig filter in a template:
{{ '# Hello, Markdown!'|markdown }}
Outputs: <h1>Hello, Markdown!</h1>
Twig Integration
|markdown filter for inline parsing:
{% set content = '# Heading\n- List item' %}
{{ content|markdown }}
{{ markdown_content|striptags|truncate(100) }}
Service Injection
Inject the Parsedown service in controllers/services:
use Demontpx\ParsedownBundle\Service\Parsedown;
public function __construct(Parsedown $parsedown) {
$this->parsedown = $parsedown;
}
public function renderMarkdown(string $text): string {
return $this->parsedown->text($text);
}
API Endpoint
Enable the REST endpoint via config/routes.yaml:
demontpx_parsedown:
resource: "@DemontpxParsedownBundle/Resources/config/routing.yml"
prefix: /api/parsedown
Send a POST request with raw markdown to /api/parsedown for HTML output.
Dynamic Content Parse markdown from database fields or user input:
{{ article.body|markdown }}
Parsedown via Symfony’s compiler pass or override the service definition.htmlspecialchars on untrusted input).Symfony Version Mismatch
demontpx/parsedown-bundle:dev-main if targeting newer Symfony.Twig Auto-Reloading
|markdown if the bundle isn’t properly registered.php bin/console cache:clear) or restart the dev server.API Endpoint CORS
/parsedown endpoint lacks CORS headers by default, blocking frontend requests.NelmioCorsBundle.Markdown Edge Cases
$parsedown->useMarkdownExtra();
php bin/console debug:container demontpx_parsedown.parsedown
{{ dump(markdown_content|markdown) }}
Override Parsedown Service
Define a custom service in config/services.yaml:
Demontpx\ParsedownBundle\Service\Parsedown:
arguments:
$extensions: ['!tag parser', '!autolink'] # Disable extensions
Add Custom Extensions Create a compiler pass to inject extensions:
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class AddCustomExtensionsPass implements CompilerPassInterface {
public function process(ContainerBuilder $container) {
$definition = $container->findDefinition('demontpx_parsedown.parsedown');
$definition->addMethodCall('useCustomExtension', [new CustomExtension()]);
}
}
Register the pass in DemontpxParsedownBundle.php.
Twig Filter Customization Override the Twig filter globally:
// config/packages/twig.yaml
twig:
filters:
markdown:
method: customMarkdownFilter
class: App\Twig\CustomExtension
How can I help you explore Laravel packages today?