spatie/shiki-php
Use Shiki syntax highlighting from PHP. Highlight code snippets with editor-quality themes and 100+ languages, plus Antlers and Blade. Works great with Laravel via spatie/laravel-markdown and CommonMark through a companion extension.
Pros:
spatie/laravel-markdown), reducing friction for Laravel-based projects. Supports Blade and Antlers out of the box, which are critical for Laravel templating.stdin input in v2.3.3) and includes speed improvements (e.g., v1.0.2).Cons:
shiki as an npm package, adding complexity to PHP-only deployments (e.g., serverless, Docker containers without Node).proc_open() to invoke Node.js, which introduces latency (~50–200ms per highlight call) and potential failure modes (e.g., Node not found, permission issues).spatie/laravel-markdown for Markdown-based applications (e.g., documentation, blogs).spatie/commonmark-shiki-highlighter for CommonMark parsers.@highlight directives or custom components.Mailable classes)./usr/local/bin/node), adding DevOps overhead.shiki has vulnerabilities).proc_open() limits (mitigated in v2.3.3 but still a risk for edge cases).RuntimeException). Graceful fallbacks (e.g., plain-text output) are not built-in.proc_open() or external process execution?proc_open() limits?spatie/laravel-markdown, Blade templates, or CommonMark (via spatie/commonmark-shiki-highlighter).proc_open() may be prohibitive (e.g., real-time IDE plugins).Assess Node.js Compatibility:
FROM php:8.2-cli
RUN apt-get update && apt-get install -y nodejs npm
RUN npm install -g shiki@latest
/usr/local/bin/node → ~/.nvm/versions/node/v20.x/bin/node).Install Dependencies:
composer require spatie/shiki-php
npm install shiki
spatie/laravel-markdown if using Markdown:
composer require spatie/laravel-markdown
Configuration:
Shiki service in AppServiceProvider:
$app->singleton(\Spatie\ShikiPhp\Shiki::class, fn() => new \Spatie\ShikiPhp\Shiki());
/usr/local/bin/node or /opt/homebrew/bin/node).Implementation:
// app/Helpers/CodeHelper.php
function highlightCode($code, $language = 'php', $theme = 'github-light') {
return \Spatie\ShikiPhp\Shiki::highlight($code, $language, $theme);
}
{!! highlightCode('<?php echo "Hello"; ?>') !!}
spatie/laravel-markdown with Shiki extension:
use Spatie\Markdown\MarkdownRenderer;
$renderer = new MarkdownRenderer();
$renderer->useShikiHighlighter();
echo $renderer->toHtml('# Title with `code`');
spatie/commonmark-shiki-highlighter.Testing:
$shiki = Mockery::mock(\Spatie\ShikiPhp\Shiki::class)->shouldReceive('highlight')->andThrow(new \RuntimeException('Node failed'))->getMock();
spatie/laravel-markdown and spatie/commonmark-shiki-highlighter..shiki) are not overridden by global styles.shiki in all environments.How can I help you explore Laravel packages today?