From c630b6f7c7502a607bb8e07017ceb31f3e2ab752 Mon Sep 17 00:00:00 2001 From: Ross Keenan Date: Tue, 23 Nov 2021 17:37:43 +0200 Subject: [PATCH] feat(TagNote): :sparkles: Tag Notes! Similar to Folder Notes, all notes with the tag you specify link to the chosen note --- .vscode/settings.json | 3 +- main.js | 44 +++++++++++++++++++++++++++ src/main.ts | 69 ++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 114 insertions(+), 2 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index c9477030..1ccbda0c 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -14,6 +14,7 @@ "CSV Crumbs", "nextPrev", "DucksView", - "FolderNote" + "FolderNote", + "TagNote" ] } \ No newline at end of file diff --git a/main.js b/main.js index b15a5647..f912d38b 100644 --- a/main.js +++ b/main.js @@ -49904,6 +49904,8 @@ class BCPlugin extends obsidian.Plugin { field = upFields[0]; } const oppField = (_a = getOppFields(userHiers, field)[0]) !== null && _a !== void 0 ? _a : getFields(userHiers, "down")[0]; + if (!oppField) + return; sources.forEach((source) => { var _a; // This is getting the order of the folder note, not the source pointing up to it @@ -49914,6 +49916,43 @@ class BCPlugin extends obsidian.Plugin { } }); } + addTagLinksToGraph(fileFrontmatterArr, mainG) { + const { userHiers } = this.settings; + fileFrontmatterArr.forEach((fileFront) => { + var _a; + const tagNoteFile = fileFront.file; + if (fileFront["BC-tag-note"]) { + const tagNoteBasename = getDVBasename(tagNoteFile); + const tag = fileFront["BC-tag-note"].trim(); + if (!tag.startsWith("#")) + return; + const sources = fileFrontmatterArr + .map((ff) => ff.file) + .filter((file) => { + var _a, _b; + return file.path !== tagNoteFile.path && + ((_b = (_a = this.app.metadataCache + .getFileCache(file)) === null || _a === void 0 ? void 0 : _a.tags) === null || _b === void 0 ? void 0 : _b.map((t) => t.tag).some((t) => t.includes(tag))); + }) + .map(getDVBasename); + let field = fileFront["BC-tag-note-up"]; + const upFields = getFields(userHiers, "up"); + if (typeof field !== "string" || !upFields.includes(field)) { + field = upFields[0]; + } + const oppField = (_a = getOppFields(userHiers, field)[0]) !== null && _a !== void 0 ? _a : getFields(userHiers, "down")[0]; + if (!oppField) + return; + sources.forEach((source) => { + var _a; + // This is getting the order of the folder note, not the source pointing up to it + const sourceOrder = (_a = parseInt(fileFront.order)) !== null && _a !== void 0 ? _a : 9999; + this.populateMain(mainG, source, "up", field, [tagNoteBasename], sourceOrder, fileFrontmatterArr); + this.populateMain(mainG, tagNoteBasename, "down", oppField, [source], sourceOrder, fileFrontmatterArr); + }); + } + }); + } async initGraphs() { const { settings, app } = this; debugGroupStart(settings, "debugMode", "Initialise Graphs"); @@ -49967,7 +50006,12 @@ class BCPlugin extends obsidian.Plugin { this.addHNsToGraph(hierarchyNotesArr, mainG); // !SECTION Hierarchy Notes debugGroupEnd(settings, "debugMode"); + console.time("Folder-Notes"); this.addFolderNoteLinksToGraph(fileFrontmatterArr, mainG); + console.timeEnd("Folder-Notes"); + console.time("Tag-Notes"); + this.addTagLinksToGraph(fileFrontmatterArr, mainG); + console.timeEnd("Tag-Notes"); files.forEach((file) => { const { basename } = file; addNodesIfNot(mainG, [basename]); diff --git a/src/main.ts b/src/main.ts index 8c5385b9..23d6b04d 100644 --- a/src/main.ts +++ b/src/main.ts @@ -776,6 +776,7 @@ export default class BCPlugin extends Plugin { const oppField = getOppFields(userHiers, field as string)[0] ?? getFields(userHiers, "down")[0]; + if (!oppField) return; sources.forEach((source) => { // This is getting the order of the folder note, not the source pointing up to it @@ -803,6 +804,67 @@ export default class BCPlugin extends Plugin { }); } + addTagLinksToGraph( + fileFrontmatterArr: dvFrontmatterCache[], + mainG: MultiGraph + ) { + const { userHiers } = this.settings; + fileFrontmatterArr.forEach((fileFront) => { + const tagNoteFile = fileFront.file; + if (fileFront["BC-tag-note"]) { + const tagNoteBasename = getDVBasename(tagNoteFile); + const tag = (fileFront["BC-tag-note"] as string).trim(); + if (!tag.startsWith("#")) return; + + const sources = fileFrontmatterArr + .map((ff) => ff.file) + .filter( + (file) => + file.path !== tagNoteFile.path && + this.app.metadataCache + .getFileCache(file) + ?.tags?.map((t) => t.tag) + .some((t) => t.includes(tag)) + ) + .map(getDVBasename); + + let field = fileFront["BC-tag-note-up"]; + const upFields = getFields(userHiers, "up"); + if (typeof field !== "string" || !upFields.includes(field)) { + field = upFields[0]; + } + + const oppField = + getOppFields(userHiers, field as string)[0] ?? + getFields(userHiers, "down")[0]; + if (!oppField) return; + + sources.forEach((source) => { + // This is getting the order of the folder note, not the source pointing up to it + const sourceOrder = parseInt(fileFront.order) ?? 9999; + this.populateMain( + mainG, + source, + "up", + field as string, + [tagNoteBasename], + sourceOrder, + fileFrontmatterArr + ); + this.populateMain( + mainG, + tagNoteBasename, + "down", + oppField, + [source], + sourceOrder, + fileFrontmatterArr + ); + }); + } + }); + } + async initGraphs(): Promise { const { settings, app } = this; debugGroupStart(settings, "debugMode", "Initialise Graphs"); @@ -828,7 +890,7 @@ export default class BCPlugin extends Plugin { const basename = getDVBasename(fileFrontmatter.file); iterateHiers(userHiers, (hier, dir, field) => { const values = this.parseFieldValue(fileFrontmatter[field]); - const sourceOrder = parseInt(fileFrontmatter.order) ?? 9999; + const sourceOrder = parseInt(fileFrontmatter.order as string) ?? 9999; this.populateMain( mainG, basename, @@ -875,7 +937,12 @@ export default class BCPlugin extends Plugin { debugGroupEnd(settings, "debugMode"); + console.time("Folder-Notes"); this.addFolderNoteLinksToGraph(fileFrontmatterArr, mainG); + console.timeEnd("Folder-Notes"); + console.time("Tag-Notes"); + this.addTagLinksToGraph(fileFrontmatterArr, mainG); + console.timeEnd("Tag-Notes"); files.forEach((file) => { const { basename } = file;