Skip to content

Commit 53e743e

Browse files
committed
feat: automatically load previous templates
1 parent abd49e3 commit 53e743e

4 files changed

Lines changed: 84 additions & 0 deletions

File tree

src/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { DateAndTimeUtils } from "./utils/dateAndTime";
55
import { getTemplateFromId, getUserTemplateSelection, Note } from "./utils/templates";
66
import { setDefaultTemplatesView } from "./views/defaultTemplates";
77
import { JoplinCommand } from "./types";
8+
import { loadLegacyTemplates } from "./legacyTemplates";
89

910
joplin.plugins.register({
1011
onStart: async function() {
@@ -35,6 +36,10 @@ joplin.plugins.register({
3536
const parser = new Parser(dateAndTimeUtils, dialogViewHandle);
3637

3738

39+
// Asynchronously load legacy templates
40+
loadLegacyTemplates(dateAndTimeUtils);
41+
42+
3843
// Utility Functions
3944
const executeCommandWithParsedTemplate = async (command: JoplinCommand, template: Note | null) => {
4045
const parsedTemplate = await parser.parseTemplate(template);

src/legacyTemplates.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import joplin from "api";
2+
import { DateAndTimeUtils } from "./utils/dateAndTime";
3+
import { createFolder } from "./utils/folders";
4+
import { applyTagToNote, getAnyTagWithTitle } from "./utils/tags";
5+
6+
const README_BODY = (
7+
`As the templates feature was removed from the main application and was repackaged in a form a plugin, we noticed that you had some templates previously. We imported those templates for you in this notebook. Here are some quick tips for getting started with the templates plugin.
8+
9+
- You can rename this notebook if you want. You can infact, shift your templates to any other notebook.
10+
- All the notes or to-dos with a tag titled \`template\` are considered as templates.
11+
- You can delete this readme or notebook if you've shifted your templates to any other notebook.
12+
- Your templates are still present in your templates directory but are renamed from \`.md\` to \`.md.old\`.
13+
- For full documentation and features, please refer to the official [readme](https://github.com/joplin/plugin-templates#readme).`);
14+
15+
const createTemplatesFolder = async (utils: DateAndTimeUtils): Promise<string> => {
16+
const folderTitle = `Imported Templates - ${utils.getCurrentTime(utils.getDateFormat())}`;
17+
return createFolder(folderTitle);
18+
}
19+
20+
const getTemplatesTag = async (): Promise<string> => {
21+
return (await getAnyTagWithTitle("template")).id;
22+
}
23+
24+
export const loadLegacyTemplates = async (dateAndTimeUtils: DateAndTimeUtils): Promise<void> => {
25+
const fs = joplin.require("fs-extra");
26+
27+
let folderId = null;
28+
let templatesTagId = null;
29+
30+
const profileDir = await joplin.settings.globalValue("profileDir");
31+
const templatesDir = `${profileDir}/templates`;
32+
33+
if (await fs.pathExists(templatesDir)) {
34+
try {
35+
const directoryContents = await fs.readdir(templatesDir);
36+
for (const contentName of directoryContents) {
37+
const contentPath = `${templatesDir}/${contentName}`;
38+
if (contentName.endsWith(".md") && (await fs.stat(contentPath)).isFile()) {
39+
if (!folderId) folderId = await createTemplatesFolder(dateAndTimeUtils);
40+
if (!templatesTagId) templatesTagId = await getTemplatesTag();
41+
42+
const templateBody = await fs.readFile(contentPath, "utf-8");
43+
44+
const note = await joplin.data.post(["notes"], null, { title: contentName, body: templateBody, parent_id: folderId });
45+
await applyTagToNote(templatesTagId, note.id);
46+
47+
const newPath = `${contentPath}.old`;
48+
await fs.rename(contentPath, newPath);
49+
}
50+
}
51+
} catch (error) {
52+
console.error(`Failed to load legacy templates: ${error}`);
53+
}
54+
}
55+
56+
if (folderId) {
57+
await joplin.data.post(["notes"], null, { title: "README", body: README_BODY, parent_id: folderId });
58+
}
59+
}

src/utils/folders.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,8 @@ export const doesFolderExist = async (folderId: string): Promise<boolean> => {
1414
export const getAllNotesInFolder = async (folderId: string): Promise<Note[]> => {
1515
return fetchAllItems(["folders", folderId, "notes"], { fields: ["id", "title", "body"] });
1616
}
17+
18+
export const createFolder = async (title: string): Promise<string> => {
19+
const folder = await joplin.data.post(["folders"], null, { title: title });
20+
return folder.id;
21+
}

src/utils/tags.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import joplin from "api";
12
import { fetchAllItems } from "./dataApi";
23
import { Note } from "./templates";
34

@@ -15,6 +16,20 @@ export const getAllTagsWithTitle = async (title: string): Promise<Tag[]> => {
1516
});
1617
}
1718

19+
export const getAnyTagWithTitle = async (title: string): Promise<Tag> => {
20+
const existingTags = await getAllTagsWithTitle(title);
21+
if (existingTags.length) {
22+
return existingTags[0];
23+
}
24+
25+
const tag = await joplin.data.post(["tags"], null, { title: title });
26+
return tag.id;
27+
}
28+
1829
export const getAllNotesWithTag = async (tagId: string): Promise<Note[]> => {
1930
return fetchAllItems(["tags", tagId, "notes"], { fields: ["id", "title", "body"] });
2031
}
32+
33+
export const applyTagToNote = async (tagId: string, noteId: string): Promise<void> => {
34+
await joplin.data.post(["tags", tagId, "notes"], null, { id: noteId });
35+
}

0 commit comments

Comments
 (0)