Skip to content

Commit

Permalink
fix(Juggl): 🐛 Get Juggl syntax working again
Browse files Browse the repository at this point in the history
  • Loading branch information
SkepticMystic committed Aug 13, 2021
1 parent 7dc7e8c commit 579bd14
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 51 deletions.
7 changes: 7 additions & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export interface dvLink {
export interface JugglLink {
note: string;
links: {
dir: Directions | "";
type: string;
linksInLine: string[];
}[];
Expand Down Expand Up @@ -164,3 +165,9 @@ export type HierData = {
};
};
};

export interface HierarchyFields {
up: { [field: string]: string[] };
same: { [field: string]: string[] };
down: { [field: string]: string[] };
}
110 changes: 59 additions & 51 deletions src/sharedFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,21 @@ import {
TFile,
WorkspaceLeaf,
} from "obsidian";
import { dropHeaderOrAlias, splitLinksRegex } from "src/constants";
import { DIRECTIONS, dropHeaderOrAlias, splitLinksRegex } from "src/constants";
import type {
BreadcrumbsSettings,
Directions,
dvFrontmatterCache,
dvLink,
HierarchyFields,
JugglLink,
neighbourObj,
relObj,
userHierarchy,
} from "src/interfaces";
import type BreadcrumbsPlugin from "src/main";
import type MatrixView from "src/MatrixView";
import { link } from "fs";

export function sum(arr: number[]): number {
return arr.reduce((a, b) => a + b);
Expand Down Expand Up @@ -109,13 +112,16 @@ export async function getJugglLinks(
settings: BreadcrumbsSettings
): Promise<JugglLink[]> {
const files = app.vault.getMarkdownFiles();
const { userHierarchies } = settings;

// Add Juggl links
const typedLinksArr: JugglLink[] = await Promise.all(
files.map(async (file) => {
const jugglLink: JugglLink = { note: file.basename, links: [] };

// Use Obs metadatacache to get the links in the current file
const links = app.metadataCache.getFileCache(file)?.links ?? [];
// TODO Only get cachedRead if links.length
const content = await app.vault.cachedRead(file);

links.forEach((link) => {
Expand All @@ -132,8 +138,20 @@ export async function getJugglLinks(
?.map((innerText) => innerText.split("|")[0]) ?? [];

const parsedLinks = parseTypedLink(link, line, "-");
const type = parsedLinks?.properties?.type ?? "";
let typeDir: Directions | "" = "";
DIRECTIONS.forEach((dir) => {
userHierarchies.forEach((hier) => {
if (hier[dir].includes(type)) {
typeDir = dir;
return;
}
});
});

jugglLink.links.push({
type: parsedLinks?.properties?.type ?? "",
dir: typeDir,
type,
linksInLine,
});
});
Expand All @@ -145,22 +163,24 @@ export async function getJugglLinks(

const allFields: string[] = settings.userHierarchies
.map((hier) => Object.values(hier))
.flat()
.flat(2)
.filter((field: string) => field !== "");

typedLinksArr.forEach((jugglLink) => {
if (jugglLink.links.length) {
// Filter out links whose type is not in allFields
// TODO This could probably be done better with filter?
const fieldTypesOnly = [];
jugglLink.links.forEach((link) => {
if (allFields.includes(link.type)) {
fieldTypesOnly.push(link);
}
});
// I don't remember why I'm mutating the links instead of making a new obj
jugglLink.links = fieldTypesOnly;
}
// Filter out links whose type is not in allFields

const fieldTypesOnly = jugglLink.links.filter((link) =>
allFields.includes(link.type)
);

// // const fieldTypesOnly = [];
// jugglLink.links.forEach((link) => {
// if (allFields.includes(link.type)) {
// fieldTypesOnly.push(link);
// }
// });
// I don't remember why I'm mutating the links instead of making a new obj
jugglLink.links = fieldTypesOnly;
});

// Filter out the juggl links with no links
Expand Down Expand Up @@ -224,59 +244,47 @@ export async function getNeighbourObjArr(

let jugglLinks: JugglLink[] = [];
if (plugin.app.plugins.plugins.juggl !== undefined) {
console.log("Using Juggl");
jugglLinks = await getJugglLinks(plugin.app, plugin.settings);
debug(plugin.settings, { jugglLinks });
}

const neighbourObjArr: {
current: TFile;
hierarchies: {
up: { [field: string]: string[] };
same: { [field: string]: string[] };
down: { [field: string]: string[] };
}[];
hierarchies: HierarchyFields[];
}[] = fileFrontmatterArr.map((fileFrontmatter) => {
const hierFields: {
current: TFile;
hierarchies: {
up: { [field: string]: string[] };
same: { [field: string]: string[] };
down: { [field: string]: string[] };
}[];
hierarchies: HierarchyFields[];
} = {
current: fileFrontmatter.file,
hierarchies: [],
};

userHierarchies.forEach((hier, i) => {
const fieldsArr = Object.values(hier) as [string[], string[], string[]];
const newHier: {
up: { [field: string]: string[] };
same: { [field: string]: string[] };
down: { [field: string]: string[] };
} = { up: {}, same: {}, down: {} };

fieldsArr[0].forEach((upField) => {
newHier.up[upField] = getFieldValues(
fileFrontmatter,
upField,
plugin.settings
);
});
fieldsArr[1].forEach((sameField) => {
newHier.same[sameField] = getFieldValues(
fileFrontmatter,
sameField,
plugin.settings
);
});
fieldsArr[2].forEach((downField) => {
newHier.down[downField] = getFieldValues(
fileFrontmatter,
downField,
plugin.settings
);
const newHier: HierarchyFields = { up: {}, same: {}, down: {} };

DIRECTIONS.forEach((dir, i) => {
fieldsArr[i].forEach((field) => {
newHier[dir][field] = getFieldValues(
fileFrontmatter,
field,
plugin.settings
);
});
});

if (jugglLinks.length) {
jugglLinks.forEach((jugglLink) => {
jugglLink.links.forEach((link) => {
if (newHier[link.dir][link.type]) {
newHier[link.dir][link.type] = link.linksInLine;
}
});
});
}

hierFields.hierarchies.push(newHier);
});

Expand Down

0 comments on commit 579bd14

Please sign in to comment.