|
| 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 | +} |
0 commit comments