From c1c8f17f8d09b11285e2eaa8265e305df5fee7f1 Mon Sep 17 00:00:00 2001 From: Ross Keenan Date: Tue, 7 Sep 2021 08:41:22 +0200 Subject: [PATCH] fix(CreateIndex): :bug: Check if currFile is null This occurs when showAliasesInIndex is enabled, but the link is unresolved. So getFileCache is checking for the cache of null --- main.js | 23 +++++++++++++---------- src/MatrixView.ts | 26 ++++++++++++++++---------- 2 files changed, 29 insertions(+), 20 deletions(-) diff --git a/main.js b/main.js index 78649870..1c13e0f8 100644 --- a/main.js +++ b/main.js @@ -26165,6 +26165,7 @@ class MatrixView extends obsidian.ItemView { reversed.forEach((path) => path.shift()); const indent = " "; const visited = {}; + const activeFile = this.app.workspace.getActiveFile(); reversed.forEach((path) => { var _a, _b, _c, _d; for (let depth = 0; depth < path.length; depth++) { @@ -26180,16 +26181,18 @@ class MatrixView extends obsidian.ItemView { index += currNode; index += settings.wikilinkIndex ? "]]" : ""; if (settings.aliasesInIndex) { - const currFile = this.app.metadataCache.getFirstLinkpathDest(currNode, this.app.workspace.getActiveFile().path); - const cache = this.app.metadataCache.getFileCache(currFile); - const alias = (_b = (_a = cache === null || cache === void 0 ? void 0 : cache.frontmatter) === null || _a === void 0 ? void 0 : _a.alias) !== null && _b !== void 0 ? _b : []; - const aliases = (_d = (_c = cache === null || cache === void 0 ? void 0 : cache.frontmatter) === null || _c === void 0 ? void 0 : _c.aliases) !== null && _d !== void 0 ? _d : []; - const allAliases = [ - ...[alias].flat(3), - ...[aliases].flat(3), - ]; - if (allAliases.length) { - index += ` (${allAliases.join(", ")})`; + const currFile = this.app.metadataCache.getFirstLinkpathDest(currNode, activeFile.path); + if (currFile !== null) { + const cache = this.app.metadataCache.getFileCache(currFile); + const alias = (_b = (_a = cache === null || cache === void 0 ? void 0 : cache.frontmatter) === null || _a === void 0 ? void 0 : _a.alias) !== null && _b !== void 0 ? _b : []; + const aliases = (_d = (_c = cache === null || cache === void 0 ? void 0 : cache.frontmatter) === null || _c === void 0 ? void 0 : _c.aliases) !== null && _d !== void 0 ? _d : []; + const allAliases = [ + ...[alias].flat(3), + ...[aliases].flat(3), + ]; + if (allAliases.length) { + index += ` (${allAliases.join(", ")})`; + } } } index += "\n"; diff --git a/src/MatrixView.ts b/src/MatrixView.ts index 18bf8680..240ddeff 100644 --- a/src/MatrixView.ts +++ b/src/MatrixView.ts @@ -195,6 +195,8 @@ export default class MatrixView extends ItemView { const indent = " "; const visited: { [node: string]: number[] } = {}; + + const activeFile = this.app.workspace.getActiveFile(); reversed.forEach((path) => { for (let depth = 0; depth < path.length; depth++) { const currNode = path[depth]; @@ -212,21 +214,25 @@ export default class MatrixView extends ItemView { index += settings.wikilinkIndex ? "]]" : ""; if (settings.aliasesInIndex) { + const currFile = this.app.metadataCache.getFirstLinkpathDest( currNode, - this.app.workspace.getActiveFile().path + activeFile.path ); - const cache = this.app.metadataCache.getFileCache(currFile); - const alias = cache?.frontmatter?.alias ?? []; - const aliases = cache?.frontmatter?.aliases ?? []; + if (currFile !== null) { + const cache = this.app.metadataCache.getFileCache(currFile); + + const alias = cache?.frontmatter?.alias ?? []; + const aliases = cache?.frontmatter?.aliases ?? []; - const allAliases: string[] = [ - ...[alias].flat(3), - ...[aliases].flat(3), - ]; - if (allAliases.length) { - index += ` (${allAliases.join(", ")})`; + const allAliases: string[] = [ + ...[alias].flat(3), + ...[aliases].flat(3), + ]; + if (allAliases.length) { + index += ` (${allAliases.join(", ")})`; + } } }