Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,14 +215,16 @@ export class Parser {
}

const firstMatch = matches[0];
const splitVal = firstMatch.split(":");
const value = splitVal.slice(1).join(":").trim();

// Don't do anything if it already contains a double quote
if (firstMatch.indexOf("\"") !== -1) {
// Don't do anything if the user has already quoted the value
if (value.startsWith("\"") || value.startsWith("'")) {
continue;
}

const splitVal = firstMatch.split(":");
const wrappedString = `${splitVal[0]}: "${splitVal.slice(1).join(":").trim()}"`;
const escapedValue = value.replace(/\\/g, "\\\\").replace(/"/g, "\\\"");
const wrappedString = `${splitVal[0]}: "${escapedValue}"`;

definitionsBlock = definitionsBlock.replace(firstMatch, wrappedString);
}
Expand Down
43 changes: 43 additions & 0 deletions tests/parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,49 @@ describe("Template parser", () => {
expect(parsedTemplate).toBeNull();
});

test("should quote special variable values that contain double quotes", async () => {
const template = {
id: "note-id",
title: "Some Template",
body: dedent`
---
template_title: {{ datetime format="dddd D MMMM YYYY" }}
template_tags: {{ datetime format="YYYY" }}, weekly
template_todo_alarm: {{ datetime set_time="09:00" delta_days=1 }}

---
`
};

const parsedTemplate = await parser.parseTemplate(template);

assert(parsedTemplate);
expect(parsedTemplate.title).toEqual("Thursday 12 August 2021");
expect(parsedTemplate.tags).toStrictEqual(["2021", "weekly"]);
expect(parsedTemplate.todo_due).toEqual(1628845200000); // 13 August 2021 09:00
});

test("should keep backslashes and already quoted special variable values as written", async () => {
const titleWithBackslash = "Q3\\Q4 {{ datetime format=\"YYYY\" }}";
const template = {
id: "note-id",
title: "Some Template",
body: dedent`
---
template_title: ${titleWithBackslash}
template_tags: 'weekly, {{ datetime format="YYYY" }}'

---
`
};

const parsedTemplate = await parser.parseTemplate(template);

assert(parsedTemplate);
expect(parsedTemplate.title).toEqual("Q3\\Q4 2021");
expect(parsedTemplate.tags).toStrictEqual(["weekly", "2021"]);
});

test("should show an error message if notebook defined in templates_notebook doesn't exist", async () => {
const template = {
id: "note-id",
Expand Down