Installation Add the package via Composer:
composer require pomodocs/commonmark-alert
Register the extension in your Laravel app (e.g., in config/commonmark.php):
'extensions' => [
// ... other extensions
\Pomodocs\CommonMark\Alert\AlertExtension::class,
],
First Use Case
Use the alert block in Markdown to render styled alerts (e.g., in a blog post or documentation):
::: alert
This is an alert message!
:::
Outputs a styled blockquote with a warning icon (default) or customizable variants (success, danger, info).
Where to Look First
AlertExtension – Defines block parsing logic.alert, success, warning, etc.).config/commonmark-alert.php).AlertRenderer – Now merges attributes instead of overwriting them (v0.8+).Markdown Processing Pipeline
commonmark or spatie/laravel-markdown):
use Pomodocs\CommonMark\Alert\AlertExtension;
$markdown = file_get_contents('post.md');
$html = \CommonMark\CommonMarkConverter::create()
->getEnvironment()
->addExtension(new AlertExtension())
->convert($markdown);
spatie/laravel-caching) to avoid reprocessing.Dynamic Alert Types
::: success
Operation completed successfully!
:::
::: danger
Failed to save changes.
:::
tip, note) by overriding the getAlertTypes() method.Blade Integration
@markdown(file_get_contents('docs/alerts.md'))
API Responses
return response()->json([
'message' => 'User created',
'alert' => '<div class="alert success">...</div>', // Pre-rendered HTML
]);
AlertRenderer to merge custom attributes without overwriting existing ones:
::: alert{data-custom="value" class="custom-class"}
This alert has merged attributes!
:::
Block Parsing Conflicts
CommonMarkConverter.$environment->getBlockParserRegistry()->getBlockParsers();
to verify the AlertBlockParser is last in the chain.Nested Alerts
<div> wrappers or flatten structure.Customization Limits
style parameter:
::: alert{style="custom"}
Custom-styled alert.
:::
Performance
CommonMarkConverter instance:
$converter = app()->makeWith(\CommonMark\CommonMarkConverter::class, [
'environment' => fn() => tap(new \CommonMark\Environment\Environment(), fn($env) =>
$env->addExtension(new AlertExtension())
),
]);
$environment->addRenderer(\CommonMark\Node\Block\Paragraph::class, function ($node) {
return '<pre>' . print_r($node->getData(), true) . '</pre>';
});
::: alert) may silently fail. Validate with:
try {
$html = $converter->convert($markdown);
} catch (\CommonMark\CommonMarkException $e) {
report($e);
}
data-custom and default classes coexisting).Add Custom Alert Types
Extend AlertExtension to support new types (e.g., example):
protected function getAlertTypes(): array {
return array_merge(parent::getAlertTypes(), ['example']);
}
Then use in Markdown:
::: example
Usage: `php artisan migrate`
:::
Modify Renderer Logic (v0.8+)
Override the AlertRenderer to customize attribute merging or HTML structure:
$environment->addRenderer(\Pomodocs\CommonMark\Alert\AlertBlock::class, function ($node) {
$type = $node->getData('type') ?? 'alert';
$attributes = $node->getData('attributes', []);
return sprintf(
'<div class="alert %s %s">%s</div>',
$type,
$attributes['class'] ?? '',
$node->getLiteral()
);
});
Integrate with Tailwind CSS Replace Bootstrap classes with Tailwind:
// In your CSS or extension
.alert { @apply p-4 mb-4 text-sm text-gray-700 bg-gray-100 rounded-lg }
.alert.success { @apply bg-green-100 border border-green-400 text-green-700 }
::: alert{class="border-red-500"}
Custom border alert.
:::
Localization
Support translated alert labels (e.g., warning → advertencia):
$environment->addRenderer(\Pomodocs\CommonMark\Alert\AlertBlock::class, function ($node) use ($translator) {
$type = $translator->get('alert.types.' . ($node->getData('type') ?? 'alert'));
$attributes = $node->getData('attributes', []);
return sprintf('<div class="alert %s" %s>%s</div>',
$type,
$this->renderAttributes($attributes),
$node->getLiteral()
);
});
How can I help you explore Laravel packages today?