diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 00000000..d041bf4f --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "conventionalCommits.scopes": [ + "CreateIndex" + ] +} \ No newline at end of file diff --git a/src/MatrixView.ts b/src/MatrixView.ts index ca84388a..f2fa8227 100644 --- a/src/MatrixView.ts +++ b/src/MatrixView.ts @@ -125,6 +125,36 @@ export default class MatrixView extends ItemView { return pathsArr; } + createIndex(allPaths: string[][], currFile: string): string { + const reversed = allPaths.map((path) => path.reverse()); + reversed.forEach((path) => path.shift()); + + let txt = currFile + "\n"; + const indent = " "; + const visited: { [node: string]: number[] } = {}; + reversed.forEach((path) => { + for (let depth = 0; depth < path.length; depth++) { + const currNode = path[depth]; + + // If that node has been visited before at the current depth + if ( + visited.hasOwnProperty(currNode) && + visited[currNode].includes(depth) + ) { + continue; + } else { + txt += `${indent.repeat(depth)} - [[${currNode}]]\n`; + + if (!visited.hasOwnProperty(currNode)) { + visited[currNode] = []; + } + visited[currNode].push(depth); + } + } + }); + return txt; + } + async draw(): Promise { this.contentEl.empty(); // this.currGraphs = this.plugin.currGraphs; @@ -138,33 +168,6 @@ export default class MatrixView extends ItemView { closeImpliedLinks(gChildren, gParents), currFile.basename ); - const reversed = allPaths.map((path) => path.reverse()); - reversed.forEach((path) => path.shift()); - - let txt = currFile.basename + "\n"; - const indent = " "; - const visited: string[] = []; - const depths: number[] = []; - reversed.forEach((path) => { - for (let i = 0; i < path.length; i++) { - const curr = path[i]; - if (!visited.includes(curr)) { - const index = visited.indexOf(curr); - if (depths[index] !== i) { - txt += indent.repeat(i + 1); - txt += `- ${curr}\n`; - visited.push(curr); - depths.push(i); - } - } else { - const next = path[i + 1]; - if (next) { - txt += indent.repeat(i + 2); - txt += `- ${next}\n`; - } - } - } - }); // !SECTION Create Index @@ -180,7 +183,9 @@ export default class MatrixView extends ItemView { const createIndexButton = this.contentEl.createEl("button", { text: "Create Index", }); - createIndexButton.addEventListener("click", () => console.log(txt)); + createIndexButton.addEventListener("click", () => { + console.log(this.createIndex(allPaths, currFile.basename)); + }); const [parentFieldName, siblingFieldName, childFieldName] = [ settings.showNameOrType ? settings.parentFieldName : "Parent", diff --git a/src/main.ts b/src/main.ts index 68aa9cef..caa33422 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,4 +1,3 @@ -import * as graphlib from "graphlib"; import { Graph } from "graphlib"; import { addIcon, MarkdownView, Plugin, TFile, WorkspaceLeaf } from "obsidian"; import { BreadcrumbsSettingTab } from "src/BreadcrumbsSettingTab"; @@ -35,7 +34,9 @@ const DEFAULT_SETTINGS: BreadcrumbsSettings = { showTrail: true, trailOrTable: 3, gridHeatmap: false, - heatmapColour: getComputedStyle(document.body).getPropertyValue("--text-accent"), + heatmapColour: getComputedStyle(document.body).getPropertyValue( + "--text-accent" + ), showAll: false, noPathMessage: `This note has no real or implied parents`, trailSeperator: "→",