From 617cef81c192778f75d5360c6c25e1dfb6c1de33 Mon Sep 17 00:00:00 2001 From: chinesepowered <22500229+chinesepowered@users.noreply.github.com> Date: Sun, 19 Jul 2026 21:48:42 -0700 Subject: [PATCH] fix(CodeWindow): don't treat // inside a string as a comment hl() located the line comment with line.indexOf('//'), which matches // inside string literals too. A line like const url = 'https://example.com' was split at the // inside the string, so the rest of the string (and its closing quote) rendered as a grey comment and the string highlighting broke. Scan for the first // that is outside any string literal instead. --- src/components/CodeWindow.tsx | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/components/CodeWindow.tsx b/src/components/CodeWindow.tsx index e186432..a2ad1ad 100644 --- a/src/components/CodeWindow.tsx +++ b/src/components/CodeWindow.tsx @@ -26,9 +26,27 @@ function tintNumbers(s: string, key: number): ReactNode { ); } +// Find the start of a `//` line comment, skipping `//` that appears inside a +// string literal (e.g. a URL like 'https://…') so the string isn't cut short. +function commentStart(line: string): number { + let quote = ''; + for (let i = 0; i < line.length; i++) { + const c = line[i]; + if (quote) { + if (c === '\\') i++; + else if (c === quote) quote = ''; + } else if (c === '"' || c === "'" || c === '`') { + quote = c; + } else if (c === '/' && line[i + 1] === '/') { + return i; + } + } + return -1; +} + function hl(line: string): ReactNode { - // split off a trailing line comment - const ci = line.indexOf('//'); + // split off a trailing line comment (ignoring `//` inside strings) + const ci = commentStart(line); const code = ci >= 0 ? line.slice(0, ci) : line; const comment = ci >= 0 ? line.slice(ci) : ''; // strings first (odd indices), then keywords, then numeric literals