From a738e39415cf89391509c8bbc2e7953073980082 Mon Sep 17 00:00:00 2001 From: Ross Keenan Date: Sat, 21 Aug 2021 08:45:09 +0200 Subject: [PATCH] fix(Hierarchy Note): :bug: Any consistent depth should be allowed, not just tabs --- src/main.ts | 53 +++++++++++++++++++++++++----------------- src/sharedFunctions.ts | 2 +- 2 files changed, 33 insertions(+), 22 deletions(-) diff --git a/src/main.ts b/src/main.ts index a170da89..ab9aaa6f 100644 --- a/src/main.ts +++ b/src/main.ts @@ -315,21 +315,45 @@ export default class BreadcrumbsPlugin extends Plugin { const depth = (line: string) => line.split("-")[0].length; + const depths = layers.map(depth); + const differences = []; + + depths.forEach((dep, i) => { + if (i >= 1) { + differences.push(dep - depths[i - 1]); + } + }); + + const posFilteredDifferences = differences + .filter((diff) => diff !== 0) + .map(Math.abs); + + const noDup = removeDuplicates(posFilteredDifferences); + if (noDup.length > 1) { + new Notice( + "Please make sure the indentation is consistent in your hierarchy note." + ); + return []; + } + const difference = noDup[0]; + const hier: { note: string; depth: number; children: string[] }[] = []; const lineRegex = new RegExp(/\s*- \[\[(.*)\]\]/); - console.log({ str }); - - const getNoteUp = ( + const pushNoteUp = ( hier: { note: string; depth: number; children: string[] }[], + currNote: string, currDepth: number ) => { const copy = [...hier]; const noteUp = copy.reverse().find((adjItem, i) => { - return adjItem.depth === currDepth - 1; + return adjItem.depth === currDepth - difference; }); - return noteUp; + debug(this.settings, { noteUp }); + if (noteUp) { + hier[hier.indexOf(noteUp)].children.push(currNote); + } }; let lineNo = 0; @@ -343,36 +367,23 @@ export default class BreadcrumbsPlugin extends Plugin { if (lineNo !== layers.length - 1) { const nextLine = layers[lineNo + 1]; - console.log({ nextLine }); const nextNote = nextLine.match(lineRegex)[1]; const nextDepth = depth(nextLine); if (nextDepth > currDepth) { debug(this.settings, { currNote, nextNote }); hier[lineNo].children.push(nextNote); - - const noteUp = getNoteUp(hier, currDepth); - debug(this.settings, { noteUp }); - if (noteUp) { - hier[hier.indexOf(noteUp)].children.push(currNote); - } + pushNoteUp(hier, currNote, currDepth); } else if (currDepth === 0) { } else { - const noteUp = getNoteUp(hier, currDepth); - debug(this.settings, { noteUp }); - if (noteUp) { - hier[hier.indexOf(noteUp)].children.push(currNote); - } + pushNoteUp(hier, currNote, currDepth); } } else { const prevLine = layers[lineNo - 1]; const prevDepth = depth(prevLine); if (prevDepth >= currDepth) { - const noteUp = getNoteUp(hier, currDepth); - if (noteUp) { - hier[hier.indexOf(noteUp)].children.push(currNote); - } + pushNoteUp(hier, currNote, currDepth); } } diff --git a/src/sharedFunctions.ts b/src/sharedFunctions.ts index 0192e950..93ed6563 100644 --- a/src/sharedFunctions.ts +++ b/src/sharedFunctions.ts @@ -551,6 +551,6 @@ export function hierToStr(hier: userHierarchy) { ↓: ${hier.down.join(", ")}`; } -export function removeDuplicates(arr: string[]) { +export function removeDuplicates(arr: T[]) { return [...new Set(arr)]; }