Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message
Commonmark Shiki Highlighter

Commonmark Shiki Highlighter Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Highlight code blocks with league/commonmark and Shiki

Frequently asked questions about Commonmark Shiki Highlighter
How do I integrate this package with Laravel’s Blade templates for Markdown rendering?
Use the `@markdown` directive from `spatie/laravel-markdown` to render Markdown in Blade. The package automatically extends `league/commonmark` with Shiki syntax highlighting, so no extra configuration is needed beyond installing both packages. Ensure your Blade template includes the required CSS for Shiki’s output.
What Laravel versions and PHP requirements does this package support?
This package requires Laravel 8+ and PHP 8.0+ due to dependencies on `league/commonmark` (v1.0+) and `spatie/shiki-php`. Check the [README](https://github.com/spatie/commonmark-shiki-highlighter) for exact version constraints, as breaking changes may occur with major updates to these dependencies.
Can I customize the syntax highlighting theme or language defaults?
Yes, you can configure themes and language overrides via the CommonMark environment. For example, set a default theme like `'github-dark'` or override languages for specific code blocks. Refer to the `spatie/shiki-php` documentation for supported themes and languages.
Will this package work with Antlers or other templating engines in Laravel?
The package supports Antlers out of the box, as it extends `league/commonmark` directly. If you’re using Antlers, ensure your Markdown processor is configured to use the Shiki-highlighted renderer. For other templating engines, verify compatibility with `league/commonmark` first.
How do I handle performance issues with large Markdown files or high traffic?
Cache rendered Markdown output using Laravel’s caching system (e.g., `spatie/laravel-caching`) or a dedicated cache driver. Shiki PHP is fast, but heavy Markdown processing can impact latency. For static content, consider pre-rendering and storing HTML snippets.
Are there alternatives if I don’t want to use Shiki for syntax highlighting?
Yes, alternatives include client-side libraries like Prism.js or Highlight.js, which offload work to the browser. For server-side solutions, you could use `league/commonmark-extensions` or other syntax-highlighting packages like `vlucas/phpdox` (though they may lack Shiki’s language support).
How do I handle unsupported languages or malformed Markdown in production?
The package gracefully falls back to plain-text rendering for unsupported languages or malformed Markdown. Implement a fallback mechanism in your Markdown processor (e.g., wrap the renderer in a try-catch block) to ensure robustness. Test edge cases like nested code blocks or invalid syntax.
Do I need to install the JavaScript Shiki package (`shiki`) for this to work?
No, this package uses the PHP port of Shiki (`spatie/shiki-php`), so you don’t need the JavaScript version. However, ensure you install `spatie/shiki-php` via Composer (`composer require spatie/shiki-php`) as it’s a required dependency.
Can I add custom languages or modify the highlighted output post-rendering?
Yes, the package is extensible. You can register custom languages via `spatie/shiki-php` or post-process the HTML output with JavaScript (e.g., adding line numbers or copy buttons). Check the `spatie/shiki-php` docs for language registration and the CommonMark environment for renderer hooks.
How do I test this package in a Laravel application before deploying to production?
Test in a staging environment by processing sample Markdown files with code blocks in various languages. Use Laravel’s `Markdown` facade (from `spatie/laravel-markdown`) to render content and verify the output matches expectations. Check for CSS conflicts and edge cases like empty code blocks or unsupported syntax.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport