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).
## Getting Started
### Minimal Setup
1. **Install the package**:
```bash
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 facade for direct API calls (e.g., Torchlight::highlight()).@torchlight for inline highlighting or @torchlightBlock for reusable blocks.config/torchlight.php for customizing themes, cache, and API options.@torchlight('javascript', 'console.log("Hello");')
$highlighted = Torchlight::highlight('python', 'print("Hello")');
echo $highlighted;
@torchlightBlock('my-python-code', 'python', 'def hello(): ...')
@torchlightBlock('dynamic-' . $id, 'javascript', $code)
$highlighted = Torchlight::file('path/to/Controller.php');
Torchlight::highlight('php', $code, ['theme' => 'dracula']);
$theme = app()->environment('local') ? 'dark+' : 'light+';
Torchlight::setConfig(['theme' => $theme]);
use Torchlight\Livewire\HandlesTorchlight;
class MyComponent extends Component {
use HandlesTorchlight;
public function highlightCode() {
$this->highlight('javascript', $this->codeInput);
}
}
Torchlight::addPostProcessor(function ($html, $block) {
return '<div class="line-numbers">' . $html . '</div>';
});
Torchlight::highlight('php', $code, [
'postProcessors' => [fn($html) => str_replace(' ', ' ', $html)]
]);
cache.driver and cache.ttl in torchlight.php.Torchlight::clearCache(); // Clear all cached blocks
Torchlight::clearBlock('my-python-code'); // Clear specific block
// app/View/Components/TorchlightComponent.php
use Torchlight\Blade\TorchlightComponent as BaseComponent;
class TorchlightComponent extends BaseComponent {
public function class($class) {
$this->attributes['class'] = $class;
return $this;
}
}
AppServiceProvider:
Blade::component('torchlight', \App\View\Components\TorchlightComponent::class);
Torchlight::highlight('php', $code, [
'options' => [
'showLineNumbers' => true,
'tabWidth' => 4,
]
]);
Torchlight::setSnippetDirectories([
resource_path('snippets/php'),
resource_path('snippets/javascript'),
]);
<div x-data="{ code: 'console.log(\'Hello\')' }">
@torchlight('javascript', $code)
<button @click="code = 'alert(\'Updated!\')'">Update</button>
</div>
API Token Leaks
TORCHLIGHT_TOKEN in .env may expose it in logs or Git history.AppServiceProvider:
if (empty(config('torchlight.token'))) {
throw new \RuntimeException('Torchlight API token not configured.');
}
Cache Invalidation
Torchlight::clearBlock('my-code-' . md5($code));
Tab Replacement
Torchlight::highlight('yaml', $code, ['tabWidth' => false]);
Livewire Version Conflicts
AppServiceProvider:
if (version_compare(\Livewire\Livewire::VERSION, '2.0', '<')) {
Torchlight::registerLivewireMiddleware();
}
Memory Leaks in Octane
Torchlight::setConfig(['cache.ttl' => 3600]); // 1 hour
Blade Component Registration
@torchlight directive may not work if Blade components are disabled.blade_components is true in torchlight.php or register manually:
Torchlight::registerBladeComponents();
API Requests
config/torchlight.php:
'debug' => env('TORCHLIGHT_DEBUG', false),
Cached Responses
Torchlight::getCacheKey('php', '<?php echo 1;');
Language Detection
text/x-markdown vs. markdown).@torchlight('text/x-markdown', '```php\n...')
Macroable Blocks
Block functionality:
\Torchlight\Block::macro('addLineNumbers', function ($html) {
return '<div class="line-numbers">' . $html . '</div>';
});
$block = Torchlight::highlight('php', $code);
echo $block->addLineNumbers();
Custom Cache Drivers
Torchlight\Cache\CacheStore:
class MyCacheStore implements \Torchlight\Cache\CacheStore {
public function get($key) { ... }
public function put($key, $value, $ttl) { ... }
public function forget($key) { ... }
}
AppServiceProvider:
Torchlight::setCacheStore(new My
How can I help you explore Laravel packages today?