From 37083dfc1aca536017615118fc6dba645e19fba9 Mon Sep 17 00:00:00 2001 From: Ross Keenan Date: Thu, 5 Aug 2021 10:31:37 +0200 Subject: [PATCH] fix(getFieldValues): :bug: splitAndDrop regex was => null when the link didn't have [[]] --- src/interfaces.ts | 11 ++++++++-- src/main.ts | 1 + src/sharedFunctions.ts | 48 ++++++++++++++++++++++++------------------ 3 files changed, 38 insertions(+), 22 deletions(-) diff --git a/src/interfaces.ts b/src/interfaces.ts index 9a0f4900..1186f758 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -31,8 +31,15 @@ export interface BreadcrumbsSettings { } export interface dvFrontmatterCache { - file: TFile | any; - [field: string]: string | string[] | string[][] | dvLink | dvLink[] | Pos; + file: TFile; + [field: string]: + | string + | string[] + | string[][] + | dvLink + | dvLink[] + | Pos + | TFile; } export interface dvLink { diff --git a/src/main.ts b/src/main.ts index dcd41c9f..8ca5f733 100644 --- a/src/main.ts +++ b/src/main.ts @@ -21,6 +21,7 @@ import { debug, getDVMetadataCache, getNeighbourObjArr, + getNeighbourObjArr2, getObsMetadataCache, } from "src/sharedFunctions"; import TrailGrid from "./Components/TrailGrid.svelte"; diff --git a/src/sharedFunctions.ts b/src/sharedFunctions.ts index 323c9264..a02e9dc4 100644 --- a/src/sharedFunctions.ts +++ b/src/sharedFunctions.ts @@ -83,7 +83,7 @@ export function getObsMetadataCache( files.forEach((file) => { superDebug(settings, `GetObsMetadataCache: ${file.basename}`); const obs: FrontMatterCache = - app.metadataCache.getFileCache(file).frontmatter; + app.metadataCache.getFileCache(file)?.frontmatter; superDebug(settings, { obs }); if (obs) { fileFrontmatterArr.push({ file, ...obs }); @@ -182,26 +182,34 @@ export function getFieldValues( field: string, settings: BreadcrumbsSettings ) { - // Narrow down the possible types - const rawValues: (string | dvLink | Pos | undefined)[] = [ - frontmatterCache?.[field], - ].flat(5); - - superDebug(settings, `${field} of: ${frontmatterCache.file.path}`); - superDebug(settings, { rawValues }); - const values: string[] = []; - rawValues.forEach((rawItem) => { - if (!rawItem) return; - if (typeof rawItem === "string") { - values.push( - ...splitAndDrop(rawItem).map((str: string) => str.split("/").last()) - ); - } else if (rawItem.path) { - values.push((rawItem as dvLink).path.split("/").last()); - } - }); - return values; + try { + const rawValues: (string | dvLink | Pos | TFile | undefined)[] = [ + frontmatterCache?.[field], + ].flat(5); + + superDebug(settings, `${field} of: ${frontmatterCache?.file?.path}`); + superDebug(settings, { rawValues }); + + rawValues.forEach((rawItem) => { + if (!rawItem) return; + if (typeof rawItem === "string") { + const splits = rawItem.match(splitLinksRegex); + if (splits !== null) { + const strs = splits + .map((link) => link.match(dropHeaderOrAlias)[1]) + .map((str: string) => str.split("/").last()); + } else { + values.push(rawItem.split("/").last()); + } + } else if (rawItem.path) { + values.push((rawItem as dvLink).path.split("/").last()); + } + }); + return values; + } catch (error) { + return values; + } } export const splitAndTrim = (fields: string): string[] =>