torchlight/laravel
torchlight/laravel integrates Torchlight syntax highlighting with Laravel, adding a Blade component and CommonMark extension so you can render beautiful, cached code blocks in views and Markdown content with minimal setup.
Torchlight is a hosted syntax highlighter: your app sends raw code blocks to the Torchlight API, gets back richly highlighted HTML (themes, line numbers, diff markers, focus/highlight lines), and caches the result locally. The Laravel package wires that API into Blade and league/commonmark, so you keep writing normal <pre><code> blocks or fenced code fences and let Torchlight upgrade them.
Minimum setup:
composer require torchlight/laravel
.env:
TORCHLIGHT_TOKEN=your-token-here
php artisan vendor:publish --tag=torchlight-config
This writes config/torchlight.php (theme, cache driver/store, defaults, withBackgrounds, multi-theme map).@torchlight
```php
Route::get('/', fn () => view('welcome'));
@endtorchlight
Everything between the directives is sent as-is — including the language hint on the fence line.
First places to look:
config/torchlight.php — theme (e.g. material-theme-palenight), cache store, and the defaults block that controls line numbers and background.data-line attributes, torchlight classes, and inline styles are what your CSS targets.First real use case: a docs/blog page where code fences in Markdown need to look polished. Add the Torchlight CommonMark extension to your converter and fenced blocks in your Markdown are highlighted with zero changes to content authoring.
Blade directive for inline code snippets. The @torchlight … @endtorchlight pair wraps a normal Markdown fence. Use it for marketing pages, lesson content, and copy-paste snippets. Keep the language hint on the fence line (```php) so Torchlight knows how to parse.
CommonMark extension for blog/docs pipelines. If your content comes from Markdown (files, database, a CMS), register the extension on your Environment:
use League\CommonMark\Environment\Environment;
use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;
use League\CommonMark\MarkdownConverter;
use Torchlight\Commonmark\V2\TorchlightExtension;
$environment = (new Environment([
'torchlight' => [
'withBackgrounds' => true,
],
]))->addExtension(new CommonMarkCoreExtension())
->addExtension(new TorchlightExtension());
$html = (new MarkdownConverter($environment))->convert($markdown);
This is the pattern most apps settle on: author in Markdown, render through CommonMark with Torchlight attached, cache the final HTML.
Multi-theme for dark/light mode. Configure themes in config/torchlight.php so one block renders both variants with CSS media queries, instead of shipping two cached copies:
'themes' => [
'light' => 'github-light',
'dark' => 'material-theme-palenight',
],
Then your front-end CSS toggles between .torchlight-light / .torchlight-dark wrappers.
Per-block options via the fence comment. Torchlight reads a JSON comment adjacent to the fence. This is how authors opt into line numbers, highlighted lines, diffs, and focus without touching PHP:
// torchlight! {"lineNumbers": true, "highlightLines": [2, 3]}
Wire this into your editor tooling/docs style guide instead of baking presentation into Blade partials.
Cache aggressively, in a shared store. The package caches each highlighted block keyed by code + options + theme. In production, point the cache at Redis/database rather than the default file store so multiple app servers share hits:
'cache' => [
'driver' => 'redis',
'store' => 'default',
'ttl' => null, // keep forever; invalidate by changing code
],
Pre-render for static sites and CI. For Jigsaw, Astro-style static hosting, or php artisan build commands, render all Markdown (with Torchlight) during the build step, ship the resulting HTML, and never call the API at request time. Your token only needs to exist in the build environment.
Custom rendering wrapper. When you need to control the surrounding markup (copy buttons, file-name headers), use Torchlight::highlight($code, 'php', $options) in a Blade component or service, and inject the copy-button markup yourself. Don't edit cached HTML after the fact.
TORCHLIGHT_TOKEN is unset, rendering throws rather than degrading. Guard local/dev environments: set a real token, or skip highlighting in tests by mocking the client. Never commit the token; it bills your account.php artisan torchlight:warm style pre-rendering in your build script.{"torchlight":…} options produces a new key. Renaming a theme invalidates everything — plan a cache-flush step when bumping themes.// torchlight! {...} comment from the output only when it recognizes the format. Whitespace or a wrong JSON key silently leaves the comment inside the highlighted code. Validate options and check rendered output after upgrading.withBackgrounds and themes interact. Background colors are emitted as inline styles; if you want your own CSS background, set withBackgrounds => false and style the wrapper — otherwise you'll fight specificity.@torchlight and @endtorchlight is emitted verbatim. Indenting it inside another Blade block indents the code in the output. Keep the directives flush-left, or use a Markdown source instead.V2). Upgrading league/commonmark across majors requires updating the extension class; read the package's changelog before bumping. Mixing versions produces a silent "no highlighting" result rather than an error.defaults array is the cleanest place to enforce project-wide presentation (line numbers on/off, default theme). Use it before writing per-block overrides, and reserve inline torchlight! comments for genuine exceptions like a single highlighted line.How can I help you explore Laravel packages today?