torchlight/torchlight-laravel
Torchlight syntax highlighting for Laravel. Highlight code blocks with VS Code-compatible themes and zero JavaScript. Includes caching, configurable themes and host, and Blade components/directives. Requires a Torchlight API token (free for personal/open source).
composer require torchlight/torchlight-laravel
php artisan torchlight:install
.env:
TORCHLIGHT_TOKEN=your_api_token_from_torchlight.dev
TORCHLIGHT_THEME=material-theme-palenight
TORCHLIGHT_CACHE_DRIVER=file # or redis, database, etc.
Use the @torchlight directive in your Blade templates:
@torchlight('php', '<?php echo "Hello, Torchlight!";')
This will render a syntax-highlighted code block with the specified language (php).
torchlight) if blade_components is true in config.// app/Providers/AppServiceProvider.php
public function boot()
{
Blade::directive('highlight', function ($expression) {
return "<?php echo Torchlight::highlight($expression); ?>";
});
}
Usage:
@highlight('javascript', 'console.log("Custom directive!");')
// app/Http/Middleware/SwitchTheme.php
public function handle($request, Closure $next)
{
config(['torchlight.theme' => $request->user()->prefers_dark_mode ? 'dracula' : 'material-theme-palenight']);
return $next($request);
}
// config/torchlight.php
'cache' => env('TORCHLIGHT_CACHE_DRIVER', 'redis'),
'cache_ttl' => 60 * 60, // 1 hour
Torchlight::clearCache('block_id_123');
// app/Http/Livewire/CodeEditor.php
public function render()
{
return view('livewire.code-editor', [
'highlightedCode' => Torchlight::highlight('javascript', $this->code),
]);
}
// app/Providers/AppServiceProvider.php
public function boot()
{
Torchlight::addPostProcessor(function ($html, $block) {
return str_replace('<div class="line">', '<div class="line" data-block-id="'.$block->id.'">', $html);
});
}
$highlighted = Torchlight::highlightFile('path/to/file.php');
// config/torchlight.php
'snippet_directories' => [storage_path('app/snippets')],
Torchlight\Block::macro('addLineNumbers', function () {
$this->options['lineNumbers'] = true;
return $this;
});
Usage:
@torchlight('php')->addLineNumbers()
<?php echo "Line numbers enabled!"; ?>
@endtorchlight
API Rate Limiting:
Torchlight::setFallbackResponse('<div class="fallback">Code highlighting unavailable.</div>');
TORCHLIGHT_API_REQUESTS env variable (if supported).Tab Replacement:
'tab_width' => false,
Torchlight::highlight('javascript', $code, ['tab_width' => false]);
Livewire Version Conflicts:
php artisan torchlight:test-livewire
Blade Component Registration:
blade_components is false, manually register the component:
Blade::component('torchlight', Torchlight\Blade::class);
Cache Stale Data:
php artisan cache:clear
php artisan torchlight:clear-cache
Log API Responses:
'debug' => env('TORCHLIGHT_DEBUG', false),
Validate Token:
curl -X POST https://api.torchlight.dev/highlight \
-H "Authorization: Bearer $TORCHLIGHT_TOKEN" \
-d '{"code":"<?php echo 1; ?>","language":"php"}'
Inspect Blocks:
dd(Torchlight::getBlocks());
Check for Trailing Spaces:
Custom Themes:
'theme' => 'your-custom-theme-name',
Language Detection:
linguist or highlight.js:
Torchlight::highlight($code, Torchlight\LanguageDetector::detect($code));
Git Diff Highlighting:
diff language mode:
@torchlight('diff', '@@ -1 +1 @@
-old code
+new code')
Dark/Light Mode:
$theme = $request->user()->prefers_dark_mode ? 'dracula' : 'material-theme-palenight';
Torchlight::setTheme($theme);
Custom Fallbacks:
Torchlight::setFallbackResponse('<pre><code class="fallback">'.$code.'</code></pre>');
Cache Headers:
Cache-Control headers for static code blocks:
Torchlight::setCacheHeaders(['public', 'max-age=31536000']);
Batch Processing:
Torchlight::highlightBatch([
['code' => $code1, 'language' => 'php'],
['code' => $code2, 'language' => 'javascript'],
]);
Lazy Loading:
<div x-data="{ loadTorchlight: false }" @click="loadTorchlight = true">
@if($loadTorchlight)
@torchlight('javascript', 'console.log("Lazy loaded!");')
@endif
</div>
Sanitize Input:
$safeCode = htmlspecialchars($userInput, ENT_QUOTES, 'UTF-8');
Torchlight::highlight('php', $safeCode);
Token Storage:
env() or Vault for sensitive tokens:
TORCHLIGHT_TOKEN=${VAULT_TORCHLIGHT_TOKEN}
Rate Limiting:
// app/Http/Middleware/ThrottleTorchlight.php
public function handle($request, Closure $next)
{
if ($request->
How can I help you explore Laravel packages today?