mirror of
https://github.com/Horizontal42/summarum.git
synced 2026-11-10 22:41:58 +03:00
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.
This commit is contained in:
parent
a9aaff5490
commit
af83ac6539
2 changed files with 18 additions and 1 deletions
|
|
@ -235,4 +235,13 @@ describe("review fixes", () => {
|
||||||
it("timezone names with a slash resolve as IANA ids", () => {
|
it("timezone names with a slash resolve as IANA ids", () => {
|
||||||
expect(calc("time in Europe/Berlin")).toMatch(/^\d{2}:\d{2}$/);
|
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);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,14 @@ import { CRYPTO } from "./extraunits";
|
||||||
const cryptoSnapshot: Record<string, number> = {};
|
const cryptoSnapshot: Record<string, number> = {};
|
||||||
for (const c of CRYPTO) cryptoSnapshot[c.code] = 1 / c.snapshotUsd;
|
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 {
|
export interface LineResult {
|
||||||
/** formatted result, null when the line has none (text, comment, header, error) */
|
/** formatted result, null when the line has none (text, comment, header, error) */
|
||||||
text: string | null;
|
text: string | null;
|
||||||
|
|
@ -83,7 +91,7 @@ export class SumEngine {
|
||||||
|
|
||||||
for (let i = 0; i < lines.length; i++) {
|
for (let i = 0; i < lines.length; i++) {
|
||||||
const rawLine = lines[i];
|
const rawLine = lines[i];
|
||||||
const commentStart = rawLine.indexOf("//");
|
const commentStart = findCommentStart(rawLine);
|
||||||
const line = commentStart >= 0 ? rawLine.slice(0, commentStart) : rawLine;
|
const line = commentStart >= 0 ? rawLine.slice(0, commentStart) : rawLine;
|
||||||
const trimmed = line.trim();
|
const trimmed = line.trim();
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue