spatie/sidecar-shiki
Run Shiki syntax highlighting on AWS Lambda via Sidecar from your Laravel app—no Node.js or Shiki dependencies on your server. Install the package, configure Sidecar, register the HighlightFunction, and render highlighted code fast and consistently.
Installation:
composer require spatie/sidecar-shiki
Ensure your Laravel app has Sidecar configured (this package extends Sidecar’s functionality).
First Use Case: Highlight a code snippet in a Blade template:
use Spatie\SidecarShiki\Facades\SidecarShiki;
$highlighted = SidecarShiki::highlight(
code: 'function hello() { return "world"; }',
language: 'javascript'
);
Render in Blade:
<div>{{ $highlighted }}</div>
Key Files:
config/sidecar-shiki.php (default config, minimal setup required).app/Providers/SidecarShikiServiceProvider.php (auto-registered; no manual binding needed).Dynamic Highlighting in Controllers:
public function showCodeExample()
{
$code = file_get_contents('path/to/file.php');
$highlighted = SidecarShiki::highlight($code, 'php');
return view('examples.show', compact('highlighted'));
}
Caching Highlighted Output: Use Laravel’s cache to avoid redundant Lambda calls:
$cacheKey = "shiki_{$language}_{md5($code)}";
$highlighted = Cache::remember($cacheKey, now()->addHours(1), function() use ($code, $language) {
return SidecarShiki::highlight($code, $language);
});
Integration with Markdown Parsers:
Extend a Markdown parser (e.g., spatie/laravel-markdown) to auto-highlight code blocks:
// In a custom Markdown renderer
$highlighted = SidecarShiki::highlight($blockContent, $language);
return "<pre><code class=\"language-{$language}\">{$highlighted}</code></pre>";
Sidecar Configuration:
Ensure your sidecar.php config includes Shiki:
'services' => [
// ...
'shiki' => [
'handler' => Spatie\SidecarShiki\SidecarShikiHandler::class,
],
],
Lambda Timeouts:
timeout config:
'shiki' => [
'timeout' => 30, // seconds
],
Language Detection:
spatie/array-to-xml or regex to guess languages if omitted:
$language = preg_match('/^\s*<\?php/', $code) ? 'php' : 'text';
HTML/JS Injection:
e() or a whitelist:
{!! e($highlighted) !!}
config/sidecar.php:
'debug' => env('SIDECAR_DEBUG', false),
Sidecar::call():
$response = Sidecar::call('shiki', [
'code' => $code,
'language' => $language,
], debug: true);
Custom Themes: Override Shiki’s default theme by extending the handler:
// app/Providers/SidecarShikiServiceProvider.php
public function register()
{
$this->app->extend(SidecarShikiHandler::class, function ($handler) {
$handler->setTheme('github-dark');
return $handler;
});
}
Pre/Post-Processing: Hook into Sidecar’s pipeline to transform input/output:
Sidecar::extend('shiki', function ($payload, $next) {
$payload['code'] = str_replace("\r\n", "\n", $payload['code']);
return $next($payload);
});
Fallback for Offline: Use a local Shiki instance as a fallback (requires Node.js):
if (!app()->environment('production')) {
$highlighted = SidecarShiki::highlight($code, $language, useLocalFallback: true);
}
How can I help you explore Laravel packages today?