From 9ea3bb1f7d64c7e430a13340692fba22f379b4bf Mon Sep 17 00:00:00 2001 From: Ross Keenan Date: Tue, 30 Nov 2021 08:48:21 +0200 Subject: [PATCH] refactor(CreateIndex): :recycle: Refactor addAliasesToIndex() --- .vscode/settings.json | 3 ++- src/graphUtils.ts | 3 +-- src/main.ts | 60 ++++++++++++++++++++++++++----------------- types.d.ts | 45 ++++++++++++++++++++++++++++++++ 4 files changed, 85 insertions(+), 26 deletions(-) create mode 100644 types.d.ts diff --git a/.vscode/settings.json b/.vscode/settings.json index ea6067f4..3d0cb47d 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -17,6 +17,7 @@ "FolderNote", "TagNote", "LinkNote", - "TraverseNote" + "TraverseNote", + "DownView" ] } \ No newline at end of file diff --git a/src/graphUtils.ts b/src/graphUtils.ts index 0628ce8a..1aa9e463 100644 --- a/src/graphUtils.ts +++ b/src/graphUtils.ts @@ -194,8 +194,7 @@ export function dfsAllPaths(g: MultiGraph, startNode: string): string[][] { visited.push(...succsNotVisited); queue.unshift(...newItems); - // if (!g.hasNode(node) || !g.outDegree(node)) - allPaths.push(extPath); + if (!g.hasNode(node) || !g.outDegree(node)) allPaths.push(extPath); } return allPaths; } diff --git a/src/main.ts b/src/main.ts index f2328cfc..1f776a77 100644 --- a/src/main.ts +++ b/src/main.ts @@ -95,7 +95,7 @@ export default class BCPlugin extends Plugin { this.mainG = await this.initGraphs(); for (const view of VIEWS) await this.getActiveTYPEView(view.type)?.draw(); if (this.settings.showTrail) await this.drawTrail(); - new Notice("Index refreshed"); + if (this.settings.showRefreshNotice) new Notice("Index refreshed"); } registerActiveLeafChangeEvent() { @@ -286,7 +286,7 @@ export default class BCPlugin extends Plugin { const onlyDowns = getSubInDirs(closed, "down"); const allPaths = dfsAllPaths(onlyDowns, basename); - const index = this.createIndex(allPaths); + const index = this.addAliasesToIndex(this.createIndex(allPaths)); info({ index }); await copy(index); }, @@ -308,7 +308,8 @@ export default class BCPlugin extends Plugin { sinks.forEach((terminal) => { globalIndex += terminal + "\n"; const allPaths = dfsAllPaths(onlyDowns, terminal); - globalIndex += this.createIndex(allPaths) + "\n"; + globalIndex += + this.addAliasesToIndex(this.createIndex(allPaths)) + "\n"; }); info({ globalIndex }); @@ -1159,7 +1160,6 @@ export default class BCPlugin extends Plugin { [node: string]: /** The depths at which `node` was visited */ number[]; } = {}; - const { metadataCache } = this.app; reversed.forEach((path) => { for (let depth = 0; depth < path.length; depth++) { const currNode = path[depth]; @@ -1176,25 +1176,6 @@ export default class BCPlugin extends Plugin { currNode )}`; - if (aliasesInIndex) { - const currFile = metadataCache.getFirstLinkpathDest(currNode, ""); - - if (currFile !== null) { - const cache = metadataCache.getFileCache(currFile); - - const alias: string[] = cache?.frontmatter?.alias ?? []; - const aliases: string[] = cache?.frontmatter?.aliases ?? []; - - const allAliases: string[] = [ - ...[alias].flat(3), - ...[aliases].flat(3), - ]; - if (allAliases.length) { - index += ` (${allAliases.join(", ")})`; - } - } - } - index += "\n"; if (!visited.hasOwnProperty(currNode)) visited[currNode] = []; @@ -1205,6 +1186,39 @@ export default class BCPlugin extends Plugin { return index; } + /** + * Returns a copy of `index`, doesn't mutate. + * @param {string} index + */ + addAliasesToIndex(index: string) { + const { aliasesInIndex } = this.settings; + const copy = index.slice(); + const lines = copy.split("\n"); + for (let line of lines) { + if (aliasesInIndex) { + const note = line.split("- ")[1]; + if (!note) continue; + const currFile = this.app.metadataCache.getFirstLinkpathDest(note, ""); + + if (currFile !== null) { + const cache = this.app.metadataCache.getFileCache(currFile); + + const alias: string[] = cache?.frontmatter?.alias ?? []; + const aliases: string[] = cache?.frontmatter?.aliases ?? []; + + const allAliases: string[] = [ + ...[alias].flat(3), + ...[aliases].flat(3), + ]; + if (allAliases.length) { + line += ` (${allAliases.join(", ")})`; + } + } + } + } + return lines.join("\n"); + } + // SECTION Breadcrumbs bfsAllPaths(g: Graph, startNode: string): string[][] { diff --git a/types.d.ts b/types.d.ts new file mode 100644 index 00000000..f339b0a8 --- /dev/null +++ b/types.d.ts @@ -0,0 +1,45 @@ +import "obsidian"; +import { DataviewApi } from "obsidian-dataview"; + +declare module "obsidian" { + interface App { + plugins: { + enabledPlugins: Set; + plugins: { + [id: string]: any; + dataview?: { + api?: DataviewApi; + }; + juggl: any; + metaedit: { + api: { + getAutopropFunction: () => any; + getUpdateFunction: () => any; + getFileFromTFileOrPath: () => any; + getGetPropertyValueFunction: () => any; + getGetFilesWithPropertyFunction: () => any; + getCreateYamlPropertyFunction: () => any; + getGetPropertiesInFile: () => any; + }; + }; + }; + }; + } + interface MetadataCache { + on( + name: "dataview:api-ready", + callback: (api: DataviewPlugin["api"]) => any, + ctx?: any + ): EventRef; + on( + name: "dataview:metadata-change", + callback: ( + ...args: + | [op: "rename", file: TAbstractFile, oldPath: string] + | [op: "delete", file: TFile] + | [op: "update", file: TFile] + ) => any, + ctx?: any + ): EventRef; + } +}