From 2c6662422531c6c1e1e9373771d90388b82c90a6 Mon Sep 17 00:00:00 2001 From: Joshua Martius <32636689+joshua-martius@users.noreply.github.com> Date: Sat, 29 Jul 2023 13:52:45 +0200 Subject: [PATCH] Submission review (#5) * moved source code in src * added sentence case * refactored tag adding * canvases now ignore themselves * preparation for later --- esbuild.config.mjs | 2 +- main.ts | 61 ---------------------- manifest.json | 4 +- EnterTagsModal.ts => src/EnterTagsModal.ts | 4 +- src/main.ts | 47 +++++++++++++++++ 5 files changed, 52 insertions(+), 66 deletions(-) delete mode 100644 main.ts rename EnterTagsModal.ts => src/EnterTagsModal.ts (94%) create mode 100644 src/main.ts diff --git a/esbuild.config.mjs b/esbuild.config.mjs index b13282b..be2cafe 100644 --- a/esbuild.config.mjs +++ b/esbuild.config.mjs @@ -15,7 +15,7 @@ const context = await esbuild.context({ banner: { js: banner, }, - entryPoints: ["main.ts"], + entryPoints: ["src/main.ts"], bundle: true, external: [ "obsidian", diff --git a/main.ts b/main.ts deleted file mode 100644 index 26b0456..0000000 --- a/main.ts +++ /dev/null @@ -1,61 +0,0 @@ -import { Plugin, TFile, TFolder } from 'obsidian'; -import { EnterTagsModal } from 'EnterTagsModal'; - -export default class TagManyPlugin extends Plugin { - settings: TagManyPlugin; - - async onload() { - this.registerEvent( - this.app.workspace.on("file-menu", (menu, folder) => { - if (!(folder instanceof TFolder)) return; - menu.addItem((item) => { - item - .setTitle("Tag all Notes in this Folder") - .setIcon("tags") - .onClick(async () => { - new EnterTagsModal(this.app, async (tags, includeSubfolders) => { - if (tags) { - await this.addTagsToNotes(tags.split(","), folder, includeSubfolders); - } - }).open(); - }); - }); - }) - ); - } - - onunload(): void { - - } - - async addTagsToNotes(tags: string[], folder: TFolder, includeSubfolders: boolean, counter: number[] = [0]) { - for (const note of folder.children) { - if(note.name.endsWith(".canvas")) continue; // ignore canvas files for now - if (note instanceof TFolder) { - if (includeSubfolders) await this.addTagsToNotes(tags, note, true, counter); - continue; - } - const noteContent = await this.app.vault.read(note as TFile); - const regex = /^---\ntags:\s*\[(.*?)\]\n---$/gm; - const regexResult = regex.exec(noteContent); - const trimmedTags = tags.map(tag => tag.trim()); // Trim leading and trailing whitespaces - - let newNoteContent = ""; - if (noteContent.contains("---\ntags: []\n---")) { - newNoteContent = noteContent.replace("---\ntags: []\n---", `---\ntags: [${trimmedTags.join(",")}]\n---`); - } - else if (regexResult && regexResult[1]) { - const oldTags = regexResult[1].split(",").map(tag => tag.trim()).filter(tag => !trimmedTags.includes(tag)); // Trim existing tags and remove new tags that are already present - const newTags = [...new Set([...oldTags, ...trimmedTags])]; - newNoteContent = noteContent.replace(regex, `---\ntags: [${newTags.join(",")}]\n---`); - } - else { // No tags yet, no frontmatter yet - newNoteContent = `---\ntags: [${trimmedTags.join(",")}]\n---\n\n${noteContent}`; - } - - await this.app.vault.modify(note as TFile, newNoteContent); - counter[0]++; - } - } -} - diff --git a/manifest.json b/manifest.json index 0d382cb..8be11cc 100644 --- a/manifest.json +++ b/manifest.json @@ -1,9 +1,9 @@ { "id": "tag-many", "name": "TagMany", - "version": "0.2.4", + "version": "0.2.5", "minAppVersion": "0.15.0", - "description": "This plugin allows you to add the same tag(s) to multiple notes in a folder (optionally including subfolders) at once.", + "description": "Add the same tag(s) to multiple notes in a folder (optionally including subfolders) at once.", "author": "Joshua Martius", "authorUrl": "https://fngt.nl/joshua", "isDesktopOnly": true diff --git a/EnterTagsModal.ts b/src/EnterTagsModal.ts similarity index 94% rename from EnterTagsModal.ts rename to src/EnterTagsModal.ts index 90e6748..24b5bc0 100644 --- a/EnterTagsModal.ts +++ b/src/EnterTagsModal.ts @@ -13,7 +13,7 @@ export class EnterTagsModal extends Modal { onOpen() { const { contentEl } = this; - contentEl.createEl("h2", { text: "What tags do you want to add to the Notes?" }); + contentEl.createEl("h2", { text: "What tags do you want to add to the notes?" }); new Setting(contentEl) .setName("Tags (separate with commas)") @@ -23,7 +23,7 @@ export class EnterTagsModal extends Modal { })); new Setting(contentEl) - .setName("Include Subfolders?") + .setName("Include subfolders?") .addToggle((toggle) => toggle.onChange((value) => { this.includeSubfolders = value diff --git a/src/main.ts b/src/main.ts new file mode 100644 index 0000000..63b5341 --- /dev/null +++ b/src/main.ts @@ -0,0 +1,47 @@ +import { Plugin, TFile, TFolder } from 'obsidian'; +import { EnterTagsModal } from './EnterTagsModal'; + +export default class TagManyPlugin extends Plugin { + settings: TagManyPlugin; + + async onload() { + this.registerEvent( + this.app.workspace.on("file-menu", (menu, folder) => { + if (!(folder instanceof TFolder)) return; + menu.addItem((item) => { + item + .setTitle("Tag all notes in this folder") + .setIcon("tags") + .onClick(async () => { + new EnterTagsModal(this.app, async (tags, includeSubfolders) => { + if (tags) { + const tagArray = tags.split(","); + await this.addTagsToNotes(tagArray, folder, includeSubfolders); + } + }).open(); + }); + }); + }) + ); + } + + onunload(): void { + + } + + async addTagsToNotes(tags: string[], folder: TFolder, includeSubfolders: boolean, counter: number[] = [0]) { + for (const note of folder.children) { + if (note instanceof TFolder) { + if (includeSubfolders) await this.addTagsToNotes(tags, note, true, counter); + continue; + } + + this.app.fileManager.processFrontMatter(note as TFile, (frontmatter) => { + frontmatter.tags = [...new Set([...frontmatter.tags, ...tags])]; + }) + + counter[0]++; + } + } +} +