Skip to content

Commit

Permalink
fix(Juggl): 🐛 Fix juggl links. Wasn't adding all files
Browse files Browse the repository at this point in the history
  • Loading branch information
SkepticMystic committed Nov 19, 2021
1 parent e878213 commit 3f2b460
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 82 deletions.
56 changes: 18 additions & 38 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -20411,12 +20411,11 @@ function getDVMetadataCache(app, settings, files) {
debugGroupStart(settings, "debugMode", "getDVMetadataCache");
debug(settings, "Using Dataview");
debugGroupStart(settings, "superDebugMode", "dvCaches");
const fileFrontmatterArr = [];
files.forEach((file) => {
const fileFrontmatterArr = files.map((file) => {
superDebug(settings, `GetDVMetadataCache: ${file.basename}`);
const dvCache = app.plugins.plugins.dataview.api.page(file.path);
superDebug(settings, { dvCache });
fileFrontmatterArr.push(dvCache);
return dvCache;
});
debugGroupEnd(settings, "superDebugMode");
debug(settings, { fileFrontmatterArr });
Expand All @@ -20427,18 +20426,15 @@ function getObsMetadataCache(app, settings, files) {
debugGroupStart(settings, "debugMode", "getObsMetadataCache");
debug(settings, "Using Obsidian");
debugGroupStart(settings, "superDebugMode", "obsCaches");
const fileFrontmatterArr = [];
files.forEach((file) => {
const fileFrontmatterArr = files.map((file) => {
var _a;
superDebug(settings, `GetObsMetadataCache: ${file.basename}`);
const obs = (_a = app.metadataCache.getFileCache(file)) === null || _a === void 0 ? void 0 : _a.frontmatter;
superDebug(settings, { obs });
if (obs) {
fileFrontmatterArr.push(Object.assign({ file }, obs));
}
else {
fileFrontmatterArr.push({ file });
}
if (obs)
return Object.assign({ file }, obs);
else
return { file };
});
debugGroupEnd(settings, "superDebugMode");
debug(settings, { fileFrontmatterArr });
Expand All @@ -20455,18 +20451,16 @@ async function getJugglLinks(app, settings) {
// Add Juggl links
const typedLinksArr = await Promise.all(files.map(async (file) => {
var _a, _b;
const jugglLink = { note: file.basename, links: [] };
const jugglLink = { file, links: [] };
// Use Obs metadatacache to get the links in the current file
const links = (_b = (_a = app.metadataCache.getFileCache(file)) === null || _a === void 0 ? void 0 : _a.links) !== null && _b !== void 0 ? _b : [];
// TODO Only get cachedRead if links.length
const content = await app.vault.cachedRead(file);
const content = links.length ? await app.vault.cachedRead(file) : "";
const lines = content.split("\n");
links.forEach((link) => {
var _a, _b, _c, _d, _e, _f, _g;
// Get the line no. of each link
const lineNo = link.position.start.line;
// And the corresponding line content
const line = content.split("\n")[lineNo];
// Get an array of inner text of each link
const line = lines[lineNo];
// Check the line for wikilinks, and return an array of link.innerText
const linksInLine = (_c = (_b = (_a = line
.match(splitLinksRegex)) === null || _a === void 0 ? void 0 : _a.map((link) => link.slice(2, link.length - 2))) === null || _b === void 0 ? void 0 : _b.map((innerText) => innerText.split("|")[0])) !== null && _c !== void 0 ? _c : [];
const typedLinkPrefix = (_e = (_d = app.plugins.plugins.juggl) === null || _d === void 0 ? void 0 : _d.settings.typedLinkPrefix) !== null && _e !== void 0 ? _e : "-";
Expand All @@ -20491,24 +20485,12 @@ async function getJugglLinks(app, settings) {
return jugglLink;
}));
debug(settings, { typedLinksArr });
const allFields = settings.userHierarchies
.map((hier) => Object.values(hier))
.flat(2)
.filter((field) => field !== "");
typedLinksArr.forEach((jugglLink) => {
const allFields = getFields(userHierarchies);
const filteredLinks = typedLinksArr.map((jugglLink) => {
// 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;
jugglLink.links = jugglLink.links.filter((link) => allFields.includes(link.type));
return jugglLink;
});
// Filter out the juggl links with no links
const filteredLinks = typedLinksArr.filter((jugglLink) => jugglLink.links.length);
debug(settings, { filteredLinks });
debugGroupEnd(settings, "debugMode");
return filteredLinks;
Expand Down Expand Up @@ -20635,9 +20617,7 @@ async function getNeighbourObjArr(plugin, fileFrontmatterArr) {
}
// Add Juggl Links
if (jugglLinks.length) {
const jugglLinksInFile = jugglLinks.find((jugglLink) => {
return jugglLink.note === currNode;
});
const jugglLinksInFile = jugglLinks.find((jugglLink) => jugglLink.file.basename === currNode);
if (jugglLinksInFile) {
jugglLinksInFile.links.forEach((line) => {
var _a, _b;
Expand Down Expand Up @@ -22494,7 +22474,7 @@ class MatrixView extends obsidian.ItemView {
if (hier[dir] === undefined)
return "";
return hier[dir][0] === ""
? `${hier[getOppDir(dir)].join(",")}<${dir}]>`
? `${hier[getOppDir(dir)].join(",")}<${dir}>`
: hier[dir].join(", ");
};
[
Expand Down
2 changes: 1 addition & 1 deletion src/MatrixView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ export default class MatrixView extends ItemView {
const getFieldName = (dir: Directions) => {
if (hier[dir] === undefined) return "";
return hier[dir][0] === ""
? `${hier[getOppDir(dir)].join(",")}<${dir}]>`
? `${hier[getOppDir(dir)].join(",")}<${dir}>`
: hier[dir].join(", ");
};

Expand Down
3 changes: 2 additions & 1 deletion src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export interface dvLink {
}

export interface JugglLink {
note: string;
file: TFile;
links: {
dir: Directions | "";
type: string;
Expand Down Expand Up @@ -234,6 +234,7 @@ declare module "obsidian" {
metaedit: {
api: MetaeditApi;
};
juggl: { settings: { typedLinkPrefix: string } };
};
};
}
Expand Down
60 changes: 18 additions & 42 deletions src/sharedFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,15 @@ export function getDVMetadataCache(
debug(settings, "Using Dataview");
debugGroupStart(settings, "superDebugMode", "dvCaches");

const fileFrontmatterArr: dvFrontmatterCache[] = [];
files.forEach((file) => {
const fileFrontmatterArr: dvFrontmatterCache[] = files.map((file) => {
superDebug(settings, `GetDVMetadataCache: ${file.basename}`);

const dvCache: dvFrontmatterCache = app.plugins.plugins.dataview.api.page(
file.path
);

superDebug(settings, { dvCache });
fileFrontmatterArr.push(dvCache);
return dvCache;
});

debugGroupEnd(settings, "superDebugMode");
Expand All @@ -112,17 +111,13 @@ export function getObsMetadataCache(
debug(settings, "Using Obsidian");
debugGroupStart(settings, "superDebugMode", "obsCaches");

const fileFrontmatterArr: dvFrontmatterCache[] = [];
files.forEach((file) => {
const fileFrontmatterArr: dvFrontmatterCache[] = files.map((file) => {
superDebug(settings, `GetObsMetadataCache: ${file.basename}`);
const obs: FrontMatterCache =
app.metadataCache.getFileCache(file)?.frontmatter;
superDebug(settings, { obs });
if (obs) {
fileFrontmatterArr.push({ file, ...obs });
} else {
fileFrontmatterArr.push({ file });
}
if (obs) return { file, ...obs };
else return { file };
});

debugGroupEnd(settings, "superDebugMode");
Expand Down Expand Up @@ -154,20 +149,19 @@ export async function getJugglLinks(
// Add Juggl links
const typedLinksArr: JugglLink[] = await Promise.all(
files.map(async (file) => {
const jugglLink: JugglLink = { note: file.basename, links: [] };
const jugglLink: JugglLink = { file, 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);

const content = links.length ? await app.vault.cachedRead(file) : "";
const lines = content.split("\n");

links.forEach((link) => {
// Get the line no. of each link
const lineNo = link.position.start.line;
// And the corresponding line content
const line = content.split("\n")[lineNo];
const line = lines[lineNo];

// Get an array of inner text of each link
// Check the line for wikilinks, and return an array of link.innerText
const linksInLine =
line
.match(splitLinksRegex)
Expand Down Expand Up @@ -202,32 +196,15 @@ export async function getJugglLinks(

debug(settings, { typedLinksArr });

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

typedLinksArr.forEach((jugglLink) => {
const filteredLinks = typedLinksArr.map((jugglLink) => {
// Filter out links whose type is not in allFields

const fieldTypesOnly = jugglLink.links.filter((link) =>
jugglLink.links = 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;
return jugglLink;
});

// Filter out the juggl links with no links
const filteredLinks = typedLinksArr.filter(
(jugglLink) => jugglLink.links.length
);
debug(settings, { filteredLinks });
debugGroupEnd(settings, "debugMode");
return filteredLinks;
Expand Down Expand Up @@ -375,9 +352,9 @@ export async function getNeighbourObjArr(

// Add Juggl Links
if (jugglLinks.length) {
const jugglLinksInFile = jugglLinks.find((jugglLink) => {
return jugglLink.note === currNode;
});
const jugglLinksInFile = jugglLinks.find(
(jugglLink) => jugglLink.file.basename === currNode
);

if (jugglLinksInFile) {
jugglLinksInFile.links.forEach((line) => {
Expand All @@ -395,7 +372,6 @@ export async function getNeighbourObjArr(

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

return hierFields;
}
);
Expand Down

0 comments on commit 3f2b460

Please sign in to comment.