From 49540e8d99841da10161f9176a5fbaca71079cc4 Mon Sep 17 00:00:00 2001 From: Ross Keenan Date: Sat, 21 Aug 2021 08:25:14 +0200 Subject: [PATCH] fix(Hierarchy Note): :bug: Send Notice if a note in hierarchyNotes has been deleted Rather than failing everything --- src/main.ts | 60 +++++++++++++++++++++++++++++++---------------------- 1 file changed, 35 insertions(+), 25 deletions(-) diff --git a/src/main.ts b/src/main.ts index fe1d9371..a170da89 100644 --- a/src/main.ts +++ b/src/main.ts @@ -312,7 +312,6 @@ export default class BreadcrumbsPlugin extends Plugin { hierarchyNoteAdjList = (str: string) => { const layers = str.split("\n").filter((line) => line); - console.log({ layers }); const depth = (line: string) => line.split("-")[0].length; @@ -321,6 +320,18 @@ export default class BreadcrumbsPlugin extends Plugin { const lineRegex = new RegExp(/\s*- \[\[(.*)\]\]/); console.log({ str }); + + const getNoteUp = ( + hier: { note: string; depth: number; children: string[] }[], + currDepth: number + ) => { + const copy = [...hier]; + const noteUp = copy.reverse().find((adjItem, i) => { + return adjItem.depth === currDepth - 1; + }); + return noteUp; + }; + let lineNo = 0; while (lineNo < layers.length) { const currLine = layers[lineNo]; @@ -340,22 +351,14 @@ export default class BreadcrumbsPlugin extends Plugin { debug(this.settings, { currNote, nextNote }); hier[lineNo].children.push(nextNote); - const copy = [...hier]; - const noteUp = copy.reverse().find((adjItem, i) => { - debug(this.settings, { i, currNote, currDepth }); - return adjItem.depth === currDepth - 1; - }); + const noteUp = getNoteUp(hier, currDepth); debug(this.settings, { noteUp }); if (noteUp) { hier[hier.indexOf(noteUp)].children.push(currNote); } } else if (currDepth === 0) { } else { - const copy = [...hier]; - const noteUp = copy.reverse().find((adjItem, i) => { - debug(this.settings, { i, currNote, currDepth }); - return adjItem.depth === currDepth - 1; - }); + const noteUp = getNoteUp(hier, currDepth); debug(this.settings, { noteUp }); if (noteUp) { hier[hier.indexOf(noteUp)].children.push(currNote); @@ -366,11 +369,10 @@ export default class BreadcrumbsPlugin extends Plugin { const prevDepth = depth(prevLine); if (prevDepth >= currDepth) { - const copy = [...hier]; - const noteUp = copy.reverse().find((adjItem, i) => { - return adjItem.depth === currDepth - 1; - }); - hier[hier.indexOf(noteUp)].children.push(currNote); + const noteUp = getNoteUp(hier, currDepth); + if (noteUp) { + hier[hier.indexOf(noteUp)].children.push(currNote); + } } } @@ -417,17 +419,25 @@ export default class BreadcrumbsPlugin extends Plugin { }[]; if (this.settings.hierarchyNotes[0] !== "") { const currPath = this.app.workspace.getActiveFile().path; - const contentArr = await Promise.all( - this.settings.hierarchyNotes.map(async (note) => { - const file = this.app.metadataCache.getFirstLinkpathDest( - note, - currPath - ); + const contentArr = []; + + this.settings.hierarchyNotes.forEach(async (note) => { + const file = this.app.metadataCache.getFirstLinkpathDest( + note, + currPath + ); + if (file) { const content = await this.app.vault.cachedRead(file); - return content; - }) - ); + contentArr.push(content); + } else { + new Notice( + `${note} is no long in your vault. The Hierarchy note should still work, but it is best to remove ${note} from your list of hierarchy notes in Breadcrumbs settings.` + ); + } + }); + await Promise.all(contentArr); + console.log({ contentArr }); hierarchyNotesArr = contentArr.map(this.hierarchyNoteAdjList).flat(); debug(this.settings, { hierarchyNotesArr }); }