spatie/shiki-php
PHP wrapper around Shiki for server-side syntax highlighting. Highlight code snippets to themed HTML using VS Code grammars, with support for many languages and themes. Great for docs, blogs, and static site generation in Laravel or any PHP app.
Install the package via Composer:
composer require spatie/shiki-php
Begin by creating a highlighter instance and generating HTML for a snippet:
use Spatie\ShikiPhp\Shiki;
$shiki = Shiki::create();
$code = <<<'CODE'
<?php
echo "Hello, world!";
CODE;
$html = $shiki->highlight($code, 'php');
echo $html;
For Laravel users, bind Shiki as a singleton in AppServiceProvider (or use the included service provider if available) to reuse the instance. Start with the highlight() method — it’s the core entry point for most use cases.
highlight() inside Blade components or helpers (e.g., shiki($code, 'js')) for dynamic code display in docs or blogs.setTheme() or passing it in highlight():
$shiki->setTheme('github-dark');
Shiki::create() caches internally per default).For Laravel Blade, create a component like @shiki('php', $code) to keep templates clean.
Shiki::create() boots Shiki via a PHAR or Node.js dependency (depending on config). Use a singleton or cache to prevent repeated initialization. On first request, cold-start delay (~100–500ms) is normal.languages() and themes() after instantiation:
$availableLanguages = $shiki->getLanguages();
RuntimeException if Shiki binary (e.g., shiki-cli) fails — log stderr output and validate Node.js or PHAR availability.!! $html !! only if you control the input; otherwise, default {{ }} is fine).Shiki::createWithOptions(['pathToGrammars' => '/path']) if extending supported syntaxes.config/shiki.php (if package provides it) or env vars to swap themes per environment (e.g., dark for dev, light for production).How can I help you explore Laravel packages today?