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]