Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 64 additions & 1 deletion app/Services/CodeSpans.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,74 @@ private function block(string $part): array
}
}

$code = implode("\n", array_slice($lines, 1, -1));

return [
'type' => 'block',
'language' => $language,
'filename' => $attributes['title'] ?? $attributes['filename'] ?? null,
'html' => $this->highlighter->parse(implode("\n", array_slice($lines, 1, -1)), $language === null ? null : mb_strtolower($language)),
'html' => match (mb_strtolower($language ?? '')) {
'ansi' => $this->ansi($code),
// Tempest has no Fix grammar, but Discord colors these blocks yellow, so keep the escape-only
// rendering and paint it the same way.
'fix' => '<span class="hl-fix">'.htmlspecialchars(str_replace("\r\n", "\n", $code), ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8').'</span>',
default => $this->highlighter->parse($code, $language === null ? null : mb_strtolower($language)),
},
];
}

/**
* Renders an ANSI colored block the way Discord does: ESC[<codes>m runs set the style until ESC[0m resets it.
* Only the classic codes are supported (bold, underline, the 30-37 foreground and 40-47 background colors), which
* the reference reserves for this. Anything else, including a stray escape, is dropped so no control character
* reaches the page. Every span class comes from this fixed list, never from the code itself.
*/
private function ansi(string $code): string
{
$parts = preg_split('/\x1b\[([0-9;]*)m/', str_replace("\r\n", "\n", $code), -1, PREG_SPLIT_DELIM_CAPTURE) ?: [];
$html = '';
$open = false;
$classes = [];

foreach ($parts as $index => $part) {
if ($index % 2 === 1) {
if ($open) {
$html .= '</span>';
$open = false;
}

foreach (explode(';', $part) as $value) {
$classes = match ($value) {
'', '0' => [],
'1' => [...array_diff($classes, ['ansi-1']), 'ansi-1'],
'4' => [...array_diff($classes, ['ansi-4']), 'ansi-4'],
'30', '31', '32', '33', '34', '35', '36', '37' => [
...array_filter($classes, static fn (string $class): bool => ! str_starts_with($class, 'ansi-3')),
"ansi-{$value}",
],
'40', '41', '42', '43', '44', '45', '46', '47' => [
...array_filter($classes, static fn (string $class): bool => preg_match('/^ansi-4\d$/', $class) !== 1),
"ansi-{$value}",
],
default => $classes,
};
}

if ($classes !== []) {
$html .= '<span class="'.implode(' ', $classes).'">';
$open = true;
}

continue;
}

$text = str_replace("\x1b", '', $part);

if ($text !== '') {
$html .= htmlspecialchars($text, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
}
}

return $open ? $html.'</span>' : $html;
}
}
121 changes: 121 additions & 0 deletions resources/css/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,127 @@ html body[data-scroll-locked] {
color: var(--destructive);
}

.hl-fix {
color: var(--lane-5);
}

/* Spoilers hide their text until tapped. */
.spoiler {
border-radius: 0.25rem;
background-color: color-mix(
in srgb,
var(--foreground) 85%,
transparent
);
color: transparent;
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

/* Colored children like links would shine through, so the content hides as a whole. */
.spoiler:not([data-revealed='true']) .spoiler-content {
visibility: hidden;
}

/* The reveal control sits beside the content and stays readable on the hidden block. */
.spoiler-toggle {
color: var(--background);
text-decoration: underline;
font-size: 0.75rem;
}

.spoiler[data-revealed='true'] {
background-color: color-mix(
in srgb,
var(--foreground) 12%,
transparent
);
color: inherit;
}

/* ANSI colored code blocks, the classic Discord codes only. */
.ansi-1 {
font-weight: 700;
}

.ansi-4 {
text-decoration: underline;
}

.ansi-30 {
color: var(--foreground);
}

.ansi-31 {
color: var(--destructive);
}

.ansi-32 {
color: var(--lane-1);
}

.ansi-33 {
color: var(--lane-5);
}

.ansi-34 {
color: var(--primary);
}

.ansi-35 {
color: var(--lane-4);
}

.ansi-36 {
color: var(--lane-3);
}

.ansi-37 {
color: var(--muted-foreground);
}

.ansi-40 {
background-color: color-mix(
in srgb,
var(--foreground) 22%,
transparent
);
}

.ansi-41 {
background-color: color-mix(
in srgb,
var(--destructive) 22%,
transparent
);
}

.ansi-42 {
background-color: color-mix(in srgb, var(--lane-1) 22%, transparent);
}

.ansi-43 {
background-color: color-mix(in srgb, var(--lane-5) 22%, transparent);
}

.ansi-44 {
background-color: color-mix(in srgb, var(--primary) 22%, transparent);
}

.ansi-45 {
background-color: color-mix(in srgb, var(--lane-4) 22%, transparent);
}

.ansi-46 {
background-color: color-mix(in srgb, var(--lane-3) 22%, transparent);
}

.ansi-47 {
background-color: color-mix(
in srgb,
var(--muted-foreground) 22%,
transparent
);
}

/* A GitHub profile README, sanitized on the server: it keeps GitHub's layout without GitHub's stylesheet. */
.readme {
overflow-wrap: anywhere;
Expand Down
Loading