From 83d7e78e26817a1c6fbdea5b03727ab2842e8bb6 Mon Sep 17 00:00:00 2001 From: Jacobtread Date: Sun, 16 Mar 2025 21:08:03 +1300 Subject: [PATCH] feat: testing and checks for tolerating whitespace on code fences --- src/timekeep/parser.test.ts | 9 +++++++++ src/timekeep/parser.ts | 4 ++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/timekeep/parser.test.ts b/src/timekeep/parser.test.ts index b2c97a6..8633db1 100644 --- a/src/timekeep/parser.test.ts +++ b/src/timekeep/parser.test.ts @@ -152,6 +152,15 @@ describe("loading timekeep", () => { expect(errorResult.error).toBe("Failed to parse timekeep JSON"); }); + it("should tolerate a timekeep with leading or trailing whitespaces", () => { + const input = ` + \`\`\`timekeep + \`\`\` + `; + // Start not code fences + replaceTimekeepCodeblock({ entries: [] }, input, 1, 2); + }); + it("should give error on invalid timekeep (validation)", () => { const data = `{"entries":[{"startTime":"2024-03-17T01:33:51.630Z","endTime":"2024-03-17T01:33:55.151Z","subEntries":null}]}`; diff --git a/src/timekeep/parser.ts b/src/timekeep/parser.ts index 788409e..c5f3c4b 100644 --- a/src/timekeep/parser.ts +++ b/src/timekeep/parser.ts @@ -61,7 +61,7 @@ export function extractTimekeepCodeblocks(value: string): Timekeep[] { const startLine = lines[i]; // Skip lines till a timekeep block is found - if (!startLine.startsWith("```timekeep")) { + if (!startLine.trim().startsWith("```timekeep")) { continue; } @@ -108,7 +108,7 @@ export function replaceTimekeepCodeblock( const lines = content.split("\n"); // Sanity checks to prevent overriding content - if (!lines[lineStart].startsWith("```")) { + if (!lines[lineStart].trim().startsWith("```")) { throw new Error( "Content timekeep out of sync, line number for codeblock start doesn't match: " + content[lineStart]