bgaze/parsedown-bundle
Symfony 2 bundle adding Parsedown and Parsedown Extra Markdown parsing. Provides parsedown.standard and parsedown.extra services plus Twig filters md and mde to render Markdown/Markdown Extra in templates or PHP. Note: project is unmaintained.
Installation:
Add to composer.json:
"require": {
"bgaze/parsedown-bundle": "dev-master"
}
Run composer update and enable the bundle in AppKernel.php:
new Bgaze\ParsedownBundle\BgazeParsedownBundle(),
First Use Case: Parse Markdown in a Twig template:
{{ markdown_content|md }} {# Standard Parsedown #}
{{ markdown_content|mde }} {# Parsedown Extra #}
Or in PHP:
$parsed = $this->get('parsedown.standart')->text($markdown);
Where to Look First:
md and mde for quick template integration.parsedown.standart and parsedown.extra for programmatic use.Twig Integration:
|md for standard Markdown (CommonMark).|mde for extended syntax (tables, definition lists, fenced code blocks).<div class="content">
{{ article.body|mde }}
</div>
Programmatic Parsing:
use Symfony\Component\DependencyInjection\ContainerInterface;
class PostService {
protected $container;
public function __construct(ContainerInterface $container) {
$this->container = $container;
}
public function parseMarkdown($markdown) {
return $this->container->get('parsedown.extra')->text($markdown);
}
}
Dynamic Parser Selection:
{% if content.requiresExtra %}
{{ content.text|mde }}
{% else %}
{{ content.text|md }}
{% endif %}
Custom Parsing in Controllers:
public function showAction($id) {
$post = $this->postRepository->find($id);
$html = $this->get('parsedown.extra')->text($post->getContent());
return $this->render('post/show.html.twig', ['content' => $html]);
}
Reusable Parsing Logic:
namespace AppBundle\Service;
use Symfony\Component\DependencyInjection\ContainerInterface;
class MarkdownParser {
protected $container;
public function __construct(ContainerInterface $container) {
$this->container = $container;
}
public function parse($markdown, $useExtra = false) {
$service = $useExtra ? 'parsedown.extra' : 'parsedown.standart';
return $this->container->get($service)->text($markdown);
}
}
Caching Parsed Results: Use Symfony's cache system to avoid reprocessing static Markdown:
$cache = $this->get('cache.app');
$key = 'parsed_'.$markdownHash;
$html = $cache->get($key, function() use ($markdown) {
return $this->get('parsedown.extra')->text($markdown);
});
Combining with Other Packages:
KnpMenuBundle for dynamic menus from Markdown:
{{ knp_menu_render('main_menu', {'parser': 'mde'}) }}
Custom Extensions:
# config.yml
services:
parsedown.extra:
class: ParsedownExtra
arguments:
- ['Tables', 'DefinitionLists', 'FencedCode']
Error Handling: Wrap parsing in try-catch blocks for robustness:
try {
$html = $this->get('parsedown.extra')->text($markdown);
} catch (\Exception $e) {
$html = '<p>Error parsing Markdown: '.$e->getMessage().'</p>';
}
Unmaintained Package:
Parsedown Version Limitations:
|mde) for extended features or migrate to league/commonmark.Twig Filter Overhead:
Service Naming Confusion:
parsedown.standart vs. parsedown.extra can be confusing.config/services.yml).PHP Version Incompatibility:
Check Parsed Output:
Use var_dump() or browser dev tools to inspect parsed HTML:
{{ dump(markdown_content|mde) }}
Service Availability: Verify services are registered:
$this->get('debug:container')->dump('parsedown.extra');
Markdown Syntax Errors: Parsedown may silently fail on invalid Markdown. Validate input:
if (!preg_match('/^[^\n]*\n.*$/', $markdown)) {
throw new \InvalidArgumentException('Invalid Markdown format');
}
Caching Issues: Clear cache after updating Markdown content:
php app/console cache:clear
Service Overrides:
Override services in config.yml for custom behavior:
services:
parsedown.extra:
class: ParsedownExtra
arguments:
- ['Tables', 'DefinitionLists']
Twig Environment: Ensure Twig is properly configured to use the bundle’s filters:
$twig = $this->get('twig');
$twig->addFilter(new \Twig_SimpleFilter('md', [$this->get('parsedown.standart'), 'text']));
Dependency Conflicts:
Resolve conflicts with other bundles using parsedown:
composer require erusev/parsedown:^1.7 --ignore-platform-reqs
Custom Parsedown Extensions: Extend Parsedown by subclassing and overriding methods:
namespace AppBundle\Service;
use Parsedown;
class CustomParsedown extends Parsedown {
public function __construct() {
parent::__construct();
$this->setBreaksEnabled(true);
}
}
Register the service in services.yml:
services:
custom.parsedown:
class: AppBundle\Service\CustomParsedown
Twig Filter Extensions: Add custom Twig filters for additional parsing logic:
$twig->addFilter(new \Twig_SimpleFilter('custom_md', function($text) {
return $this->get('custom.parsedown')->text($text);
}));
Event Listeners: Hook into Parsedown’s lifecycle (if extending):
$parsedown->setEventDispatcher($dispatcher);
Avoid Redundant Parsing: Parse Markdown once and cache the result:
$cacheKey = md5($markdown);
$html = $cache->get($cacheKey, function() use ($markdown) {
return $this->get('parsedown.extra')->text($markdown);
});
Minimize Twig Filters: Pre-parse Markdown in controllers to reduce template overhead:
$html = $this->get('parsedown.extra')->text($markdown);
return $this->render('template.twig', ['content' => $html]);
Use Standard Parser for Simple Content:
parsedown.standart is faster than parsedown.extra for basic Markdown.
How can I help you explore Laravel packages today?