From 0d1e3670ea9bcff335e9004955d6d23cb4072cc3 Mon Sep 17 00:00:00 2001 From: leenzhu Date: Sat, 25 May 2024 14:10:32 +0800 Subject: [PATCH] feat #25 auto add tag for journal notes --- CHANGELOG.md | 4 ++++ package.json | 2 +- src/index.ts | 50 ++++++++++++++++++++++++++++++++++++++++++++++- src/manifest.json | 2 +- 4 files changed, 55 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b0716ab..acff95a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## v1.1.3 (2023-10-06) +1. **FIX** Settings: typo fix and label improvements. Thanks @andypiper +2. **FEAT** #25 Feature Request: Add tags automatically to new journal notes + ## v1.1.1 (2023-10-06) 1. support weeknum diff --git a/package.json b/package.json index e327618..8ea280c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "joplin-plugin-journal", - "version": "1.1.2", + "version": "1.1.3", "scripts": { "dist": "export NODE_OPTIONS=--openssl-legacy-provider && webpack --joplin-plugin-config buildMain && webpack --joplin-plugin-config buildExtraScripts && webpack --joplin-plugin-config createArchive", "prepare": "export NODE_OPTIONS=--openssl-legacy-provider && npm run dist", diff --git a/src/index.ts b/src/index.ts index f14676f..1a0ff73 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,6 +4,7 @@ import { SettingItemType } from 'api/types'; const defaultNoteName = 'Journal/{{year}}/{{monthName}}/{{year}}-{{month}}-{{day}}'; const defaultMonthName = '01-Jan,02-Feb,03-Mar,04-Apr,05-May,06-Jun,07-Jul,08-Aug,09-Sep,10-Oct,11-Nov,12-Dec'; const defaultWeekdayName = 'Sun,Mon,Tue,Wed,Thu,Fri,Sat'; +const defaultTagName = 'journal' function getWeek(d) { var date = new Date(d.getTime()); @@ -184,10 +185,40 @@ async function createNote(notePath) { return note; } +async function addNoteTags(noteId) { + const enableAutoTag = await joplin.settings.value('AutoTag') || false; + if (!enableAutoTag) { + return; + } + + const tagName = await joplin.settings.value('Tags') || defaultTagName; + const tagNames = tagName.split(",") + + for (let i = 0; i < tagNames.length; i++) { + const tagTitle = tagNames[i].trim(); + let tagId = ""; + const tagFound = await joplin.data.get(["search"], { query: tagTitle, type: "tag", fields:"id, title"}); + if (tagFound.items.length == 0) { + console.log("TagName:", tagTitle, "Not found"); + const newTag = await joplin.data.post(["tags"], null, {title: tagTitle}); + console.log("CrateTagName:", tagTitle, newTag); + tagId = newTag.id; + }else { + tagId = tagFound.items[0].id; + } + + console.log("TagName:", tagTitle, tagId); + await joplin.data.post(['tags', tagId, 'notes'], null, { + id: noteId + }); + } +} + async function createNoteByDate(d) { let noteName = await makeNoteName(d); console.log("Make noteName: ", noteName); let note = await createNote(noteName); + await addNoteTags(note.id); return note; } @@ -373,7 +404,24 @@ joplin.plugins.register({ }, description: "Padding number: 01, 02, ..., 06, 07, Number: 1, 2, ..., 6, 7." }, - + 'AutoTag': { + value: false, + type: SettingItemType.Bool, + section: 'Journal', + public: true, + advanced: true, + label: 'Enable AutoTag', + description: "Auto add tag(s) when create new journal notes", + }, + 'Tags': { + value: defaultTagName, + type: SettingItemType.String, + section: 'Journal', + public: true, + advanced: true, + label: 'Tag Names', + description: "Custom tag names, each value is separated by ','. eg", + }, }); await joplin.commands.register({ diff --git a/src/manifest.json b/src/manifest.json index fe4c9c5..e594bdc 100644 --- a/src/manifest.json +++ b/src/manifest.json @@ -2,7 +2,7 @@ "manifest_version": 1, "id": "com.leenzhu.journal", "app_min_version": "2.7", - "version": "1.1.2", + "version": "1.1.3", "name": "Journal", "description": "A simple journal. Create or open a note for today, or any selected date.", "author": "Leen Zhu",