Skip to content

Commit

Permalink
fix(getFieldValues): 🐛 splitAndDrop regex was => null when the link d…
Browse files Browse the repository at this point in the history
…idn't have [[]]
  • Loading branch information
SkepticMystic committed Aug 5, 2021
1 parent d2b35bd commit 37083df
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 22 deletions.
11 changes: 9 additions & 2 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
debug,
getDVMetadataCache,
getNeighbourObjArr,
getNeighbourObjArr2,
getObsMetadataCache,
} from "src/sharedFunctions";
import TrailGrid from "./Components/TrailGrid.svelte";
Expand Down
48 changes: 28 additions & 20 deletions src/sharedFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down Expand Up @@ -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[] =>
Expand Down

0 comments on commit 37083df

Please sign in to comment.