Installation:
composer require cdaguerre/markdown-bundle
Add to config/app.php under providers:
Cdaguerre\MarkdownBundle\MarkdownServiceProvider::class,
Publish the config (if needed):
php artisan vendor:publish --provider="Cdaguerre\MarkdownBundle\MarkdownServiceProvider"
Basic Usage:
Inject the Markdown facade or service into a controller/blade view:
use Cdaguerre\MarkdownBundle\Facades\Markdown;
$html = Markdown::parse('# Hello, Markdown!');
First Use Case: Convert markdown in a blog post model to HTML before displaying:
public function show(Post $post) {
$post->content_html = Markdown::parse($post->content);
return view('posts.show', compact('post'));
}
Blade Directives:
Use @markdown directive in views:
@markdown($post->content)
(Check if the package supports this—if not, create a custom directive.)
Model Observers: Automatically convert markdown on save:
class PostObserver {
public function saved(Post $post) {
$post->content_html = Markdown::parse($post->content);
$post->save();
}
}
Form Request Validation: Validate markdown syntax before processing:
use Cdaguerre\MarkdownBundle\Rules\ValidMarkdown;
public function rules() {
return [
'content' => ['required', new ValidMarkdown],
];
}
API Responses: Return markdown as HTML in API responses:
return response()->json([
'content' => Markdown::parse($request->content),
]);
marked.js).$cacheKey = 'post_'.$post->id.'_html';
$html = Cache::remember($cacheKey, now()->addHours(1), function() use ($post) {
return Markdown::parse($post->content);
});
No Built-in Facade: The package may not include a facade by default. Create one if needed:
php artisan make:facade Markdown
Then update the facade class to point to the service.
Configuration Overrides:
Ensure config/markdown.php is published and configured correctly. Defaults might not match your needs (e.g., parser engine like Parsedown vs. CommonMark).
Performance: Parsing large markdown strings repeatedly can be slow. Cache results aggressively or use a queue for async processing.
Security: Sanitize HTML output if markdown includes user-generated content to prevent XSS:
use Purifier;
$cleanHtml = Purifier::cleanHtml(Markdown::parse($input));
Parsedown) throws exceptions. Wrap calls in a try-catch:
try {
$html = Markdown::parse($markdown);
} catch (\Exception $e) {
Log::error("Markdown parse error: " . $e->getMessage());
$html = "<p>Error rendering markdown.</p>";
}
markdown.parser setting in config/markdown.php matches an available parser class.Custom Parsers: Register a custom parser by binding it in the service provider:
$this->app->bind('markdown.parser', function() {
return new \Your\Custom\MarkdownParser();
});
Filters/Plugins: Extend the parser with custom plugins (e.g., for syntax highlighting):
$parser = new \Parsedown();
$parser->setBreaksEnabled(true);
Markdown::setParser($parser);
Event Listeners: Listen for markdown parsing events (if the package supports them) to log or modify output:
Markdown::parse($input)->then(function($html) {
// Post-process HTML
});
How can I help you explore Laravel packages today?