feat: torchlight in script diff tabs

This commit is contained in:
Ahmed Darrazi 2026-01-01 22:44:33 +01:00
parent 49a4a9f15f
commit 2f3788372a
2 changed files with 103 additions and 6 deletions

View File

@ -105,6 +105,39 @@
} }
}; };
$highlightInline = static function (?string $policyType, string $code) use ($selectGrammar): ?string {
if (! class_exists(\Torchlight\Engine\Engine::class)) {
return null;
}
if ($code === '') {
return '';
}
try {
$html = (new \Torchlight\Engine\Engine())->codeToHtml(
code: $code,
grammar: $selectGrammar($policyType, $code),
theme: [
'light' => 'github-light',
'dark' => 'github-dark',
],
withGutter: false,
withWrapper: false,
);
if (! preg_match('/^<pre><code[^>]*>(.*)<\\/code><\\/pre>$/s', $html, $matches)) {
return null;
}
$inner = $matches[1] ?? '';
return trim((string) preg_replace('/<!--\s*Syntax highlighted by[^>]*-->/', '', $inner));
} catch (\Throwable $e) {
return null;
}
};
$splitLines = static function (string $text): array { $splitLines = static function (string $text): array {
$text = str_replace(["\r\n", "\r"], "\n", $text); $text = str_replace(["\r\n", "\r"], "\n", $text);
@ -248,6 +281,7 @@
$isScriptContent = $canHighlightScripts($policyType) && $isScriptKey($name); $isScriptContent = $canHighlightScripts($policyType) && $isScriptKey($name);
$ops = $isScriptContent ? $scriptLineDiff((string) $fromText, (string) $toText) : []; $ops = $isScriptContent ? $scriptLineDiff((string) $fromText, (string) $toText) : [];
$useTorchlight = $isScriptContent && class_exists(\Torchlight\Engine\Engine::class);
@endphp @endphp
<div class="grid grid-cols-1 gap-2 sm:grid-cols-3"> <div class="grid grid-cols-1 gap-2 sm:grid-cols-3">
<div class="text-sm font-medium text-gray-900 dark:text-white"> <div class="text-sm font-medium text-gray-900 dark:text-white">
@ -330,13 +364,32 @@
$leftType = $left['type']; $leftType = $left['type'];
$leftLine = (string) ($left['line'] ?? ''); $leftLine = (string) ($left['line'] ?? '');
$leftHighlighted = $useTorchlight ? $highlightInline($policyType, $leftLine) : null;
$leftRendered = (is_string($leftHighlighted) && $leftHighlighted !== '') ? $leftHighlighted : e($leftLine);
if ($leftType === 'equal') { if ($leftType === 'equal') {
echo e($leftLine)."\n"; if ($useTorchlight) {
@endphp
@once
@include('filament.partials.torchlight-dark-overrides')
@endonce
@php
}
echo $leftRendered."\n";
continue; continue;
} }
if ($leftType === 'delete') { if ($leftType === 'delete') {
echo '<span class="block bg-danger-50 text-danger-700 dark:bg-danger-950/40 dark:text-danger-200">- '.e($leftLine)."</span>\n"; if ($useTorchlight) {
@endphp
@once
@include('filament.partials.torchlight-dark-overrides')
@endonce
@php
}
echo '<span class="block bg-danger-50 text-danger-700 dark:bg-danger-950/40 dark:text-danger-200">- '.$leftRendered."</span>\n";
continue; continue;
} }
@ -353,13 +406,32 @@
$rightType = $right['type']; $rightType = $right['type'];
$rightLine = (string) ($right['line'] ?? ''); $rightLine = (string) ($right['line'] ?? '');
$rightHighlighted = $useTorchlight ? $highlightInline($policyType, $rightLine) : null;
$rightRendered = (is_string($rightHighlighted) && $rightHighlighted !== '') ? $rightHighlighted : e($rightLine);
if ($rightType === 'equal') { if ($rightType === 'equal') {
echo e($rightLine)."\n"; if ($useTorchlight) {
@endphp
@once
@include('filament.partials.torchlight-dark-overrides')
@endonce
@php
}
echo $rightRendered."\n";
continue; continue;
} }
if ($rightType === 'insert') { if ($rightType === 'insert') {
echo '<span class="block bg-success-50 text-success-700 dark:bg-success-950/40 dark:text-success-200">+ '.e($rightLine)."</span>\n"; if ($useTorchlight) {
@endphp
@once
@include('filament.partials.torchlight-dark-overrides')
@endonce
@php
}
echo '<span class="block bg-success-50 text-success-700 dark:bg-success-950/40 dark:text-success-200">+ '.$rightRendered."</span>\n";
continue; continue;
} }
@ -372,12 +444,36 @@
<div x-show="tab === 'before'" x-cloak> <div x-show="tab === 'before'" x-cloak>
<div class="text-xs font-semibold uppercase tracking-wide text-gray-500 dark:text-gray-400">Before</div> <div class="text-xs font-semibold uppercase tracking-wide text-gray-500 dark:text-gray-400">Before</div>
<pre class="mt-1 max-h-96 overflow-auto font-mono text-xs text-gray-800 dark:text-gray-200 whitespace-pre">{{ (string) $fromText }}</pre> @php
$highlightedBefore = $useTorchlight ? $highlight($policyType, (string) $fromText) : null;
@endphp
@if (is_string($highlightedBefore) && $highlightedBefore !== '')
@once
@include('filament.partials.torchlight-dark-overrides')
@endonce
<div class="mt-1 max-h-96 overflow-auto">{!! $highlightedBefore !!}</div>
@else
<pre class="mt-1 max-h-96 overflow-auto font-mono text-xs text-gray-800 dark:text-gray-200 whitespace-pre">{{ (string) $fromText }}</pre>
@endif
</div> </div>
<div x-show="tab === 'after'" x-cloak> <div x-show="tab === 'after'" x-cloak>
<div class="text-xs font-semibold uppercase tracking-wide text-gray-500 dark:text-gray-400">After</div> <div class="text-xs font-semibold uppercase tracking-wide text-gray-500 dark:text-gray-400">After</div>
<pre class="mt-1 max-h-96 overflow-auto font-mono text-xs text-gray-800 dark:text-gray-200 whitespace-pre">{{ (string) $toText }}</pre> @php
$highlightedAfter = $useTorchlight ? $highlight($policyType, (string) $toText) : null;
@endphp
@if (is_string($highlightedAfter) && $highlightedAfter !== '')
@once
@include('filament.partials.torchlight-dark-overrides')
@endonce
<div class="mt-1 max-h-96 overflow-auto">{!! $highlightedAfter !!}</div>
@else
<pre class="mt-1 max-h-96 overflow-auto font-mono text-xs text-gray-800 dark:text-gray-200 whitespace-pre">{{ (string) $toText }}</pre>
@endif
</div> </div>
</div> </div>
</details> </details>

View File

@ -21,6 +21,7 @@ ## Phase 4: Script Content Display (Safe)
- [x] T009 Highlight script content with Torch (shebang-based shell + PowerShell default). - [x] T009 Highlight script content with Torch (shebang-based shell + PowerShell default).
- [x] T010 Hide script content behind a Show/Hide button (collapsed by default). - [x] T010 Hide script content behind a Show/Hide button (collapsed by default).
- [x] T011 Highlight script content in Normalized Diff view (From/To). - [x] T011 Highlight script content in Normalized Diff view (From/To).
- [x] T012 Enable Torchlight highlighting in Diff + Before/After views.
## Open TODOs (Follow-up) ## Open TODOs (Follow-up)
- None yet. - None yet.