Skip to content

Commit

Permalink
refactor(CreateIndex): ♻️ Refactor addAliasesToIndex()
Browse files Browse the repository at this point in the history
  • Loading branch information
SkepticMystic committed Nov 30, 2021
1 parent cb4cc74 commit 9ea3bb1
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 26 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"FolderNote",
"TagNote",
"LinkNote",
"TraverseNote"
"TraverseNote",
"DownView"
]
}
3 changes: 1 addition & 2 deletions src/graphUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
60 changes: 37 additions & 23 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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);
},
Expand All @@ -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 });
Expand Down Expand Up @@ -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];
Expand All @@ -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] = [];
Expand All @@ -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[][] {
Expand Down
45 changes: 45 additions & 0 deletions types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import "obsidian";
import { DataviewApi } from "obsidian-dataview";

declare module "obsidian" {
interface App {
plugins: {
enabledPlugins: Set<string>;
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;
}
}

0 comments on commit 9ea3bb1

Please sign in to comment.