Skip to content

Commit

Permalink
Create basic dashboard syntax highlighter
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva committed Oct 25, 2023
1 parent f45bbde commit 2feefae
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions packages/realtime-compiler/src/Http/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,14 @@
use Hyde\Framework\Actions\CreatesNewMarkdownPostFile;
use Symfony\Component\HttpKernel\Exception\HttpException;

use function e;
use function str;
use function time;
use function trim;
use function round;
use function rtrim;
use function strlen;
use function substr;
use function basename;
use function in_array;
use function json_decode;
Expand Down Expand Up @@ -169,6 +173,52 @@ public static function isMediaFileProbablyMinified(string $contents): bool
return substr_count(trim($contents), "\n") < 3 && strlen($contents) > 200;
}

/** @internal */
public static function highlightMediaLibraryCode(string $contents): HtmlString
{
$contents = e($contents);

if (static::isMediaFileProbablyMinified($contents)) {
return new HtmlString(substr($contents, 0, 800));
}

$highlighted = str($contents)->explode("\n")->slice(0, 25)->map(function (string $line): string {
$line = rtrim($line);

if (str_starts_with($line, '//')) {
return "<span style='font-size: 80%; color: gray'>$line</span>";
}

if (str_starts_with($line, '/*') && str_ends_with($line, '*/')) {
// Commented code should not be additionally formatted, though we always want to comment multiline blocks
$quickReturn = true;

}

$line = str_replace('/*', "<span style='font-size: 80%; color: gray'>/*", $line);
$line = str_replace('*/', '*/</span>', $line);

if ($quickReturn ?? false) {
return rtrim($line);
}

$line = strtr($line, [
'{' => "<span style='color: #0f6674'>{</span>",
'}' => "<span style='color: #0f6674'>}</span>",
'(' => "<span style='color: #0f6674'>(</span><span style=\"color: #f77243;\">",
')' => "</span><span style='color: #0f6674'>)</span>",
':' => "<span style='color: #0f6674'>:</span>",
';' => "<span style='color: #0f6674'>;</span>",
'return' => "<span style='color: #8e44ad'>return</span>",
'function' => "<span style='color: #8e44ad'>function</span>",
]);

return rtrim($line);
})->implode("\n");

return new HtmlString($highlighted);
}

public function showTips(): bool
{
return config('hyde.server.tips', true);
Expand Down

0 comments on commit 2feefae

Please sign in to comment.