Skip to content

Commit

Permalink
feat(#44): add support for template-todo-alarm (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
nishantwrp authored Sep 15, 2024
1 parent 21d16a9 commit 4020af2
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 3 deletions.
8 changes: 7 additions & 1 deletion src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@ const performInsertTextAction = async (template: NewNote) => {

const performNewNoteAction = async (template: NewNote, isTodo: 0 | 1) => {
const folderId = template.folder ? template.folder : await getSelectedFolder();
const note = await joplin.data.post(["notes"], null, { body: template.body, parent_id: folderId, title: template.title, is_todo: isTodo });
const notePayload = { body: template.body, parent_id: folderId, title: template.title, is_todo: isTodo };

if (isTodo && template.todo_due) {
notePayload["todo_due"] = template.todo_due;
}

const note = await joplin.data.post(["notes"], null, notePayload);
await joplin.commands.execute("openNote", note.id);
for (const tag of template.tags) {
const tagId = (await getAnyTagWithTitle(tag)).id;
Expand Down
12 changes: 10 additions & 2 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,19 @@ export interface NewNote {
tags: string[];
body: string;
folder: string | null;
todo_due: number | null;
}

const NOTE_TITLE_VARIABLE_NAME = "template_title";
const NOTE_TAGS_VARIABLE_NAME = "template_tags";
const NOTE_FOLDER_VARIABLE_NAME = "template_notebook";
const TODO_DUE_VARIABLE_NAME = "template_todo_alarm";

export class Parser {
private utils: DateAndTimeUtils;
private dialog: string;
private logger: Logger;
private specialVariableNames = [NOTE_TITLE_VARIABLE_NAME, NOTE_TAGS_VARIABLE_NAME, NOTE_FOLDER_VARIABLE_NAME];
private specialVariableNames = [NOTE_TITLE_VARIABLE_NAME, NOTE_TAGS_VARIABLE_NAME, NOTE_FOLDER_VARIABLE_NAME, TODO_DUE_VARIABLE_NAME];

constructor(dateAndTimeUtils: DateAndTimeUtils, dialogViewHandle: string, logger: Logger) {
this.utils = dateAndTimeUtils;
Expand Down Expand Up @@ -131,7 +133,8 @@ export class Parser {
const meta = {
title: parsedSpecialVariables.fallback_note_title,
tags: [],
folder: null
folder: null,
todo_due: null,
};

if (NOTE_TITLE_VARIABLE_NAME in parsedSpecialVariables) {
Expand All @@ -151,6 +154,11 @@ export class Parser {
}
}

if (TODO_DUE_VARIABLE_NAME in parsedSpecialVariables) {
const rawTodoDue = parsedSpecialVariables[TODO_DUE_VARIABLE_NAME].trim()
meta.todo_due = this.utils.formatLocalToJoplinCompatibleUnixTime(rawTodoDue, this.utils.getDateTimeFormat());
}

return meta;
}

Expand Down
13 changes: 13 additions & 0 deletions src/utils/dateAndTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,19 @@ export class DateAndTimeUtils {
return moment(ms).format(format);
}

public formatLocalToJoplinCompatibleUnixTime(input: string, format: string = null): number {
if (!format) {
format = this.getDateTimeFormat();
}

const date = moment(input, format, true);
if (!date.isValid()) {
throw new Error(`Was not able to parse ${input} according to format ${format}`);
}

return date.unix() * 1000;
}

public getCurrentTime(format: string = null): string {
return this.formatMsToLocal(new Date().getTime(), format);
}
Expand Down
63 changes: 63 additions & 0 deletions tests/parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ describe("Template parser", () => {
expect(parsedTemplate.folder).toEqual("8e4d3851a1237028");
expect(parsedTemplate.tags).toStrictEqual(["scrum", "Project 2"]);
expect(parsedTemplate.title).toEqual("Scrum - Project 2 - Title 1");
expect(parsedTemplate.todo_due).toBeNull();
expect(parsedTemplate.body).toEqual(dedent`
title: Title 1
project: Project 2
Expand Down Expand Up @@ -987,4 +988,66 @@ describe("Template parser", () => {

expect(errorMessagesShown).toEqual(invalidTemplates.length);
});

// template_todo_alarm special variable tests
test("should parse template_todo_alarm special variable correctly", async () => {
const template = {
id: "note-id",
title: "Some Template",
body: dedent`
---
my_date: date
template_todo_alarm: {{ datetime set_date=my_date set_time='00:00' delta_days=-1 }}
---
my_date: {{ my_date }}
template_todo_alarm: {{ template_todo_alarm }}
`
};

testVariableTypes({
my_date: DateCustomVariable,
});

handleVariableDialog("ok", {
my_date: "2023-03-15",
});

const parsedTemplate = await parser.parseTemplate(template);
expect(parsedTemplate.folder).toBeNull();
expect(parsedTemplate.tags.length).toEqual(0);
expect(parsedTemplate.title).toEqual("Some Template");
expect(parsedTemplate.todo_due).toEqual(1678752000000); // 14 March 2023
expect(parsedTemplate.body).toEqual(dedent`
my_date: 15/03/2023
template_todo_alarm: 14/03/2023 00:00
`);
});

test("should show error if template_todo_alarm special variable can't be parsed", async () => {
const template = {
id: "note-id",
title: "Some Template",
body: dedent`
---
template_todo_alarm: abc
---
Hi
`
};

let errorMessagesShown = 0;
jest.spyOn(joplin.views.dialogs, "showMessageBox").mockImplementation(async () => {
errorMessagesShown++;
return 0;
});

const parsedTemplate = await parser.parseTemplate(template);

expect(parsedTemplate).toBeNull();
expect(errorMessagesShown).toEqual(1);
});
});

0 comments on commit 4020af2

Please sign in to comment.