feat: testing and checks for tolerating whitespace on code fences

This commit is contained in:
Jacobtread 2025-03-16 21:08:03 +13:00
parent a6412d55f9
commit 83d7e78e26
2 changed files with 11 additions and 2 deletions

View file

@ -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}]}`;

View file

@ -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]