torchlight/torchlight-laravel
Torchlight syntax highlighting for Laravel Blade/Markdown using the Torchlight API. VS Code-compatible highlighting with themes, line highlighting and diffing, no JavaScript required. Includes caching, config publishing, and optional Blade components.
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.
@torchlight('php', '<?php echo "Hello, Torchlight!";')
Torchlight::highlight() for programmatic highlighting.@torchlight('language', 'code') for inline usage.@torchlight('javascript', 'console.log("Hello");')
@torchlight('php', $dynamicCodeVariable)
@torchlight(['language' => 'python', 'class' => 'custom-class'], 'print("Hi")')
$highlighted = Torchlight::highlight('css', '.selector { color: red; }');
$block = Torchlight::highlight('javascript', $largeCode);
$block->setCacheTTL(3600); // Override cache TTL
public function render()
{
return view('livewire.component', [
'highlighted' => Torchlight::highlight('html', '<div>...</div>'),
]);
}
Torchlight::addPostProcessor(function ($html) {
return '<div class="code-container">' . $html . '</div>';
});
$block = Torchlight::highlight('php', '<?php ...');
$block->addPostProcessor(fn($html) => str_replace(' ', ' ', $html));
$highlighted = Torchlight::highlightFile('path/to/file.php');
torchlight.php) for reusable code blocks.Torchlight::setTheme('dark' ? 'material-theme-darker' : 'material-theme-palenight');
Torchlight::setThemeUsing(function () {
return request()->user()->prefers_dark_mode ? 'dark-theme' : 'light-theme';
});
Torchlight::setCacheDriver('redis');
$block = Torchlight::highlight('js', '...')->setCacheTags(['user-guides']);
cache()->forget('torchlight:tags:user-guides');
Block functionality:
\Torchlight\Block::macro('addLineNumbers', function () {
$this->addPostProcessor(function ($html) {
return '<ol>' . str_replace('<div class="line">', '<li><div class="line">', $html) . '</ol>';
});
return $this;
});
@torchlight(['language' => 'php'], '<?php ...')->addLineNumbers()
Torchlight::highlight('python', '...', [
'showLineNumbers' => true,
'tabSize' => 4,
]);
API Token Leaks:
.env with TORCHLIGHT_TOKEN. Use Laravel’s .env.example to document required vars.env() in config or a secure secrets manager.Cache Invalidation:
Torchlight::clearCache();
Large Code Blocks:
highlightFile() with chunked reading.Blade Component Registration:
@torchlight directive doesn’t work:
'blade_components' => true in torchlight.php.php artisan torchlight:install or manually register the component in AppServiceProvider.Livewire Version Conflicts:
Tabs vs. Spaces:
tab_width in config).'tab_width' => false,
tab_width: 4 for consistent indentation.Post-Processor Timing:
php artisan view:cache).API Requests:
config/torchlight.php:
'debug' => env('TORCHLIGHT_DEBUG', false),
Cache Issues:
php artisan cache:clear
php artisan view:clear
storage/framework/cache for file driver).Language Detection:
@torchlight(['language' => 'php'], '<?php ...')
Blade Parsing Errors:
@torchlight directive is properly closed. Use:
@torchlight(['language' => 'js'], 'code')->withAttributes(['class' => 'compact'])
Custom Themes:
TORCHLIGHT_HOST.Snippet Directories:
torchlight.php:
'snippet_directories' => [
resource_path('views/snippets'),
base_path('snippets'),
],
@torchlightSnippet('api-response.json')
Macros for Blocks:
Block to add domain-specific logic:
\Torchlight\Block::macro('highlightAsDiff', function ($oldCode, $newCode) {
$this->addPostProcessor(function ($html) use ($oldCode, $newCode) {
return Torchlight::highlight('diff', "{$oldCode}\n\n{$newCode}");
});
});
Middleware for Livewire:
Torchlight::registerLivewireMiddleware();
Environment-Specific Config:
Torchlight::setConfigUsing(function () {
return config('torchlight', []) + [
'theme' => env('APP_ENV') === 'production' ? 'minimal' : 'palenight',
];
});
How can I help you explore Laravel packages today?