spatie/commonmark-shiki-highlighter
League/CommonMark code block renderer that highlights fenced code using Shiki PHP. Includes extra Antlers and Blade grammars in addition to Shiki’s 100+ languages. Ideal for Laravel setups (see spatie/laravel-markdown). Requires JS shiki v1.
league/commonmark, enabling syntax-highlighted code blocks via Shiki (a high-performance, language-agnostic syntax highlighter). It fits seamlessly into Laravel applications where Markdown processing is required (e.g., documentation, CMS, or dynamic content rendering).league/commonmark pipelines without requiring major architectural changes.league/commonmark (v1.0+), which is widely used in Laravel via spatie/laravel-markdown.spatie/shiki-php (Shiki PHP port), which must be installed separately.league/commonmark integration.league/commonmark and Shiki PHP requirements.spatie/laravel-markdown) and Antlers (Laravel’s templating engine).CommonMark environment (e.g., theme, language overrides).| Risk Area | Assessment | Mitigation Strategy |
|---|---|---|
| Performance | Shiki PHP is fast, but heavy Markdown processing may impact latency. | Cache rendered Markdown (e.g., spatie/laravel-caching). |
| Dependency Bloat | Adds shiki-php (~5MB) and league/commonmark (~1MB). |
Justify ROI for code-heavy apps; avoid if syntax highlighting is non-critical. |
| Breaking Changes | Shiki PHP updates may introduce syntax changes. | Pin versions in composer.json; monitor spatie/shiki-php releases. |
| Browser Compatibility | Shiki outputs CSS classes for styling; ensure theme supports them. | Use a Shiki-compatible CSS theme (e.g., github-dark, vscode-dark). |
| Edge Cases | Malformed Markdown or unsupported languages may fail gracefully. | Implement fallback rendering (e.g., plain-text code blocks). |
Why Syntax Highlighting?
Styling & Theming
Performance Trade-offs
Alternatives
league/commonmark-extensions) suffice?Long-Term Maintenance
spatie/laravel-markdown (if already in use) to wrap league/commonmark with Shiki.@markdown directive.league/commonmark with the CommonMarkShikiHighlighter renderer.league/commonmark directly or another library (e.g., michelf/php-markdown)?spatie/laravel-markdown, this package is a drop-in extension.composer require spatie/commonmark-shiki-highlighter spatie/shiki-php
use Spatie\CommonMarkShikiHighlighter\CommonMarkShikiHighlighter;
use League\CommonMark\Environment;
$environment = new Environment();
$environment->addRenderer(new CommonMarkShikiHighlighter());
// In a controller or service
$markdown = Markdown::parse('# Code Example', [
'extensions' => [new CommonMarkShikiHighlighter()],
]);
| Component | Compatibility Notes |
|---|---|
| Laravel Versions | 8.0+ (PHP 8.0+ required by Shiki PHP). |
| PHP Versions | 8.0+ (Shiki PHP drops PHP 7.x support). |
| Markdown Libraries | Only league/commonmark (v1.0+). |
| Frontend Frameworks | Works with any CSS-compatible frontend (e.g., Tailwind, Bootstrap, vanilla CSS). |
| Caching Layers | Compatible with Laravel’s cache (e.g., spatie/laravel-caching). |
tideways/xhprof).spatie/shiki-php for breaking changes (e.g., language syntax updates).composer.json to avoid surprises.config/markdown.php).| Failure Scenario | Impact | Mitigation |
|---|---|---|
| Shiki PHP Crashes | Broken rendering | Fallback to plain-text code blocks. |
| Unsupported Language | Unhighlighted code | Log warnings; document supported languages. |
| ** |
How can I help you explore Laravel packages today?