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

Shiki Php Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

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.

Implementation Patterns

  • Template rendering: Use highlight() inside Blade components or helpers (e.g., shiki($code, 'js')) for dynamic code display in docs or blogs.
  • Static site builders: Pre-highlight files during build steps (e.g., via a custom Artisan command or Jigsaw/Middleman integration) to avoid runtime overhead.
  • Markdown processing: Hook into markdown parsers (e.g., league/commonmark) using extension nodes or post-processors to inject highlighted code blocks.
  • Theming: Switch themes on-the-fly by passing a theme name to setTheme() or passing it in highlight():
    $shiki->setTheme('github-dark');
    
  • Multi-language support: Cache and reuse highlighters per language if rendering mixed-content pages at scale (though Shiki::create() caches internally per default).

For Laravel Blade, create a component like @shiki('php', $code) to keep templates clean.

Gotchas and Tips

  • Performance: 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.
  • Themes and languages: Not all Shiki languages/themes are bundled by default — verify languages() and themes() after instantiation:
    $availableLanguages = $shiki->getLanguages();
    
  • Error handling: Check for RuntimeException if Shiki binary (e.g., shiki-cli) fails — log stderr output and validate Node.js or PHAR availability.
  • HTML escaping: Output is already escaped HTML-safe — avoid double-escaping in Blade (use !! $html !! only if you control the input; otherwise, default {{ }} is fine).
  • Custom grammars/themes: Supply custom paths via Shiki::createWithOptions(['pathToGrammars' => '/path']) if extending supported syntaxes.
  • Laravel config: Store theme preferences in config/shiki.php (if package provides it) or env vars to swap themes per environment (e.g., dark for dev, light for production).
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
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
twbs/bootstrap4