Skip to content

Commit

Permalink
feat #25 auto add tag for journal notes
Browse files Browse the repository at this point in the history
  • Loading branch information
leenzhu committed May 25, 2024
1 parent ac95037 commit 0d1e367
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
50 changes: 49 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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({
Expand Down
2 changes: 1 addition & 1 deletion src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 0d1e367

Please sign in to comment.