From af83ac6539e2f4ed7863cb1c85138731168de325 Mon Sep 17 00:00:00 2001 From: Horizontal42 <75383519+Horizontal42@users.noreply.github.com> Date: Fri, 3 Jul 2026 11:30:45 +0300 Subject: [PATCH] a URL in a line got truncated at the comment marker rawLine.indexOf("//") matched the "//" inside "https://..." too, so any line with a URL lost everything after it. A comment now only starts at the beginning of a line or after whitespace. --- src/engine/edge.test.ts | 9 +++++++++ src/engine/index.ts | 10 +++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/engine/edge.test.ts b/src/engine/edge.test.ts index f2dcec3..8ba4348 100644 --- a/src/engine/edge.test.ts +++ b/src/engine/edge.test.ts @@ -235,4 +235,13 @@ describe("review fixes", () => { it("timezone names with a slash resolve as IANA ids", () => { expect(calc("time in Europe/Berlin")).toMatch(/^\d{2}:\d{2}$/); }); + it("a URL in a line is not mistaken for a comment", () => { + const r = eng.evaluateDocument("see https://example.com"); + expect(r[0].commentStart).toBeNull(); + }); + it("an inline comment after whitespace still works", () => { + const r = eng.evaluateDocument("5 + 5 // note"); + expect(r[0].text).toBe("10"); + expect(r[0].commentStart).toBe(6); + }); }); diff --git a/src/engine/index.ts b/src/engine/index.ts index 35e1bd0..2d3b83f 100644 --- a/src/engine/index.ts +++ b/src/engine/index.ts @@ -12,6 +12,14 @@ import { CRYPTO } from "./extraunits"; const cryptoSnapshot: Record = {}; for (const c of CRYPTO) cryptoSnapshot[c.code] = 1 / c.snapshotUsd; +// "//" only starts a comment at line start or after whitespace, so URLs like +// "see https://example.com" aren't cut off. +const COMMENT_RE = /(^|\s)\/\//; +function findCommentStart(line: string): number { + const m = COMMENT_RE.exec(line); + return m ? m.index + m[1].length : -1; +} + export interface LineResult { /** formatted result, null when the line has none (text, comment, header, error) */ text: string | null; @@ -83,7 +91,7 @@ export class SumEngine { for (let i = 0; i < lines.length; i++) { const rawLine = lines[i]; - const commentStart = rawLine.indexOf("//"); + const commentStart = findCommentStart(rawLine); const line = commentStart >= 0 ? rawLine.slice(0, commentStart) : rawLine; const trimmed = line.trim();