spatie/laravel-markdown
Render Markdown in Laravel with a Blade x-markdown component or a configurable MarkdownRenderer. Converts content to HTML with heading IDs, links, and syntax-highlighted code blocks, using options from your config and container resolution.
Installation:
composer require spatie/laravel-markdown
Publish the config (optional):
php artisan vendor:publish --provider="Spatie\Markdown\MarkdownServiceProvider"
First Use Case: Render markdown in a Blade view:
@markdown
# Hello World
This is **rendered** markdown.
@endmarkdown
Or programmatically:
use Spatie\Markdown\MarkdownRenderer;
$markdown = MarkdownRenderer::parse('# Hello World');
echo $markdown;
Where to Look First:
config/markdown.php for configuration options.app/Providers/AppServiceProvider.php to register custom parsers or extensions.@markdown
Your markdown content here.
@endmarkdown
@markdown(class="prose")
# Heading
Content with **styles**.
@endmarkdown
<p>Inline: @markdown('*italic*')</p>
$html = MarkdownRenderer::parse($markdownString);
$renderer = app(MarkdownRenderer::class);
$html = $renderer->parse($markdownString);
$renderer = MarkdownRenderer::create()
->useParser('extra')
->useParser('github')
->parse($markdownString);
$post = Post::find(1);
$html = MarkdownRenderer::parse($post->content);
$cacheKey = 'markdown_' . md5($markdownString);
$html = Cache::remember($cacheKey, now()->addHours(1), function () use ($markdownString) {
return MarkdownRenderer::parse($markdownString);
});
@markdown directive in Nova tooltips or field descriptions:
<div class="form-help">
@markdown
This field is **required**.
@endmarkdown
</div>
Extend the default parsers in config/markdown.php:
'parsers' => [
'commonmark' => [
'extra' => true,
'tables' => true,
],
'extra',
'github',
],
Purifier or similar:
use Spatie\Markdown\MarkdownRenderer;
use Spatie\Markdown\Parsers\CommonMarkParser;
$parser = new CommonMarkParser();
$parser->setInput($markdownString);
$parser->parse();
$html = e($parser->getOutput()); // Escape output
commonmark + extra) may lead to unexpected behavior. Test thoroughly.@markdown directive escapes HTML by default. Use @verbatim or {{ !! }} to bypass escaping if needed:
@verbatim
@markdown
<script>alert('XSS')</script>
@endmarkdown
@endverbatim
config/markdown.php to log parser errors:
'debug' => env('MARKDOWN_DEBUG', false),
Spatie\Markdown\Parsers\Parser to create custom markdown processors:
namespace App\Markdown;
use Spatie\Markdown\Parsers\Parser;
class CustomParser extends Parser
{
public function parse()
{
// Custom logic
$this->output = 'Custom HTML';
}
}
Register it in AppServiceProvider:
MarkdownRenderer::create()->useParser(CustomParser::class);
Highlight.js or Prism.js for code blocks:
@markdown
```javascript
console.log('Hello');
```
@endmarkdown
<script src="https://cdn.jsdelivr.net/npm/prismjs@1.24.1/prism.min.js"></script>
trix, ckeditor) for user-friendly markdown input:
<input type="hidden" name="content" id="markdown-content">
<trix-editor input="markdown-content"></trix-editor>
MarkdownRenderer::parse($markdownString)->dispatch();
MarkdownParsed) for post-processing:
event(new MarkdownParsed($markdownString, $html));
AppServiceProvider:
public function boot()
{
MarkdownRenderer::macro('customMethod', function () {
return 'Custom logic';
});
}
How can I help you explore Laravel packages today?