spatie/shiki-php
Bring Shiki's beautiful syntax highlighting to PHP. A simple static call highlights code using the same language engine as popular code editors, with 100+ languages and themes plus bundled Antlers and Blade support.
Architecture fit
spatie/shiki-php is a thin PHP facade over the Shiki JavaScript highlighter: PHP builds arguments/stdin, shells out to a Node renderer script shipped with the package, and returns HTML (<pre class="shiki">...).spatie/laravel-markdown and spatie/commonmark-shiki-highlighter, plus bundled Blade and Antlers grammars. If our content pipeline already uses Spatie's markdown stack, this drops into fenced code blocks with near-zero glue code.Integration feasibility
composer require spatie/shiki-php and npm install shiki, plus Node 20+ on any host that executes highlighting.highlightLines, addLines, deleteLines, focusLines) and theme/language introspection. Time-to-first-highlight is hours, not days.Technical risk
/usr/local/bin, /opt/homebrew/bin, NVM symlinks). NVM users must symlink; this has generated repeated patch releases (1.1.6, 1.1.7). Node version drift is the top recurring failure class.shiki package is absent the <pre> element is not present in output — a styling regression, not an exception. Requires an explicit CI assertion and deploy smoke test.posix_spawn argument-list overflow) were fixed only in 2.3.3 by switching to stdin — pin ≥ 2.3.3.theme parameter accepts an absolute filesystem path (custom VSCode themes). Never pass user-controlled values; constrain to an allowlist. Output is escaped by Shiki, but treat generated HTML as trusted markup, not user input.Key questions
<pre><code> or client-side highlighting?proc_open available and permitted on the target host?Stack fit
Migration path
hash(code, language, theme, line markers). Web runtime never touches Node for published pages.Highlighter interface until visual and performance parity is validated on a representative sample.Compatibility
spatie/shiki-php: ^2.4 against npm shiki v4; treat as a single joint dependency with a single upgrade ticket.Sequencing
<pre class="shiki"> appears — this catches the silent-degradation mode.Maintenance
Support
npm ls shiki in the deployed artifact.Scaling
Failure modes
| Failure | Symptom | Mitigation |
|---|---|---|
| Node missing / wrong version / bad path | No <pre>, unstyled output |
CI assertion + deploy smoke test |
npm shiki not installed |
Same silent degradation | Verify npm ls shiki in artifact |
| Subprocess timeout or non-zero exit | Broken page section | Timeout wrapper + escaped <pre><code> fallback |
| Oversized code block | Spawn failure (pre-2.3.3) | Pin ≥ 2.3.3; enforce size cap |
proc_open disabled |
Hard failure | Host qualification gate |
| User-controlled theme path | Arbitrary file read | Allowlist themes |
| Supply chain (npm + composer) | Compromise | Lockfiles + SCA on both sides |
Ramp-up
node_modules, deploy verification.proc_open.TPM recommendation Adopt for precomputed content highlighting where we control the container image — low code risk, best-in-class output, and a cheap path through the Spatie markdown stack. Do not adopt as a read-time dependency for untrusted, high-volume code blocks on PHP-only infrastructure; evaluate Torchlight or a PHP-native highlighter first. Gate the decision on answers to the Node-placement and precompute-vs-read-time questions.
How can I help you explore Laravel packages today?