Skip to content

Commit

Permalink
feat(FolderNote): ✨ BC-folder-note-recursive: true traverses the fo…
Browse files Browse the repository at this point in the history
…lder structure, adding nodes along the way
  • Loading branch information
SkepticMystic committed Jan 15, 2022
1 parent 5137253 commit d1c3584
Show file tree
Hide file tree
Showing 6 changed files with 219 additions and 36 deletions.
105 changes: 91 additions & 14 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3460,9 +3460,9 @@ const blankRealNImplied = () => {
prev: { reals: [], implieds: [] },
};
};
const [BC_FOLDER_NOTE, BC_FOLDER_NOTE_SUBFOLDER, BC_TAG_NOTE, BC_TAG_NOTE_FIELD, BC_TAG_NOTE_EXACT, BC_LINK_NOTE, BC_TRAVERSE_NOTE, BC_REGEX_NOTE, BC_REGEX_NOTE_FIELD, BC_IGNORE_DENDRON, BC_HIDE_TRAIL, BC_ORDER,] = [
const [BC_FOLDER_NOTE, BC_FOLDER_NOTE_RECURSIVE, BC_TAG_NOTE, BC_TAG_NOTE_FIELD, BC_TAG_NOTE_EXACT, BC_LINK_NOTE, BC_TRAVERSE_NOTE, BC_REGEX_NOTE, BC_REGEX_NOTE_FIELD, BC_IGNORE_DENDRON, BC_HIDE_TRAIL, BC_ORDER,] = [
"BC-folder-note",
"BC-folder-note-subfolder",
"BC-folder-note-recursive",
"BC-tag-note",
"BC-tag-note-field",
"BC-tag-note-exact",
Expand All @@ -3482,8 +3482,8 @@ const BC_FIELDS_INFO = [
alt: true,
},
{
field: BC_FOLDER_NOTE_SUBFOLDER,
desc: "This folder note should take notes in the same folder as it, _and_ notes in subfolders of it.",
field: BC_FOLDER_NOTE_RECURSIVE,
desc: "Recursively add notes in subfolders to the foldernote of _that_ subfolder.",
after: ": true",
alt: false,
},
Expand Down Expand Up @@ -5012,7 +5012,7 @@ const getBaseFromMDPath = (path) => {
return splitSlash;
};
const getDVBasename = (file) => file.basename || file.name;
const getFolder = (file) => { var _a;
const getFolderName = (file) => { var _a;
//@ts-ignore
return ((_a = file === null || file === void 0 ? void 0 : file.parent) === null || _a === void 0 ? void 0 : _a.name) || file.folder; };
const dropFolder = (path) => path.split("/").last().split(".").slice(0, -1).join(".");
Expand Down Expand Up @@ -24160,7 +24160,6 @@ class BCStore extends obsidian.Component {
const edges = [];
const nodesListS = new Set(allNodes.map((node) => this.asString(node)).filter((s) => s));
newNodes.forEach((node) => {
console.log({ node });
this.graph.forEachOutEdge(this.asString(node), (key, attr, source, target) => {
if (nodesListS.has(target)) {
edges.push({
Expand Down Expand Up @@ -25572,19 +25571,68 @@ function addDendronNotesToGraph(plugin, frontms, mainG) {
}
}

function addFolderNotesToGraph(settings, eligableAlts, frontms, mainG) {
const getSubsFromFolder = (folder) => {
const otherNotes = [], subFolders = [];
folder.children.forEach((tAbstract) => {
if (tAbstract instanceof obsidian.TFile) {
otherNotes.push(tAbstract);
}
else
subFolders.push(tAbstract);
});
return { otherNotes, subFolders };
};
// export function addFolderNotesToGraph(
// settings: BCSettings,
// folderNotes: dvFrontmatterCache[],
// frontms: dvFrontmatterCache[],
// mainG: MultiGraph
// ) {
// const { userHiers } = settings;
// const fields = getFields(userHiers);
// folderNotes.forEach((altFile) => {
// const { file } = altFile;
// const basename = getDVBasename(file);
// const topFolder = getFolderName(file);
// const recursive = altFile[BC_FOLDER_NOTE_RECURSIVE];
// const targets = frontms
// .map((ff) => ff.file)
// .filter(
// (other) =>
// getFolderName(other) === topFolder && other.path !== file.path
// )
// .map(getDVBasename);
// const field = altFile[BC_FOLDER_NOTE] as string;
// if (typeof field !== "string" || !fields.includes(field)) return;
// targets.forEach((target) => {
// // This is getting the order of the folder note, not the source pointing up to it
// const sourceOrder = getSourceOrder(altFile);
// const targetOrder = getTargetOrder(frontms, basename);
// populateMain(
// settings,
// mainG,
// basename,
// field,
// target,
// sourceOrder,
// targetOrder,
// true
// );
// });
// });
// }
function addFolderNotesToGraph(plugin, folderNotes, frontms, mainG) {
const { settings, app } = plugin;
const { userHiers } = settings;
const fields = getFields(userHiers);
eligableAlts.forEach((altFile) => {
folderNotes.forEach((altFile) => {
const { file } = altFile;
const basename = getDVBasename(file);
const folder = getFolder(file);
const subfolders = altFile[BC_FOLDER_NOTE_SUBFOLDER];
const topFolderName = getFolderName(file);
const topFolder = app.vault.getAbstractFileByPath(topFolderName);
const targets = frontms
.map((ff) => ff.file)
.filter((other) => (subfolders
? getFolder(other).includes(folder)
: getFolder(other) === folder) && other.path !== file.path)
.filter((other) => getFolderName(other) === topFolderName && other.path !== file.path)
.map(getDVBasename);
const field = altFile[BC_FOLDER_NOTE];
if (typeof field !== "string" || !fields.includes(field))
Expand All @@ -25595,6 +25643,35 @@ function addFolderNotesToGraph(settings, eligableAlts, frontms, mainG) {
const targetOrder = getTargetOrder(frontms, basename);
populateMain(settings, mainG, basename, field, target, sourceOrder, targetOrder, true);
});
if (altFile[BC_FOLDER_NOTE_RECURSIVE]) {
const { subFolders } = getSubsFromFolder(topFolder);
const folderQueue = [...subFolders];
console.log({ startingQueue: folderQueue.slice() });
let prevFolderNote = topFolderName;
let currFolder = folderQueue.shift();
while (currFolder !== undefined) {
const { otherNotes, subFolders } = getSubsFromFolder(currFolder);
const folderNote = currFolder.name;
const targets = otherNotes.map(getDVBasename);
// if (!isInVault(app, folderNote, folderNote)) continue;
const sourceOrder = 9999; // getSourceOrder(altFile);
const targetOrder = 9999; // getTargetOrder(frontms, basename);
populateMain(settings, mainG, prevFolderNote, field, folderNote, sourceOrder, targetOrder, true);
targets.forEach((target) => {
if (target === folderNote)
return;
console.log("adding", folderNote, "→", target);
const sourceOrder = 9999; // getSourceOrder(altFile);
const targetOrder = 9999; // getTargetOrder(frontms, basename);
populateMain(settings, mainG, folderNote, field, target, sourceOrder, targetOrder, true);
});
folderQueue.push(...subFolders);
currFolder = folderQueue.shift();
prevFolderNote = folderNote;
}
}
// First add otherNotes to graph
// Then iterate subFolders doing the same
});
}

Expand Down Expand Up @@ -26054,7 +26131,7 @@ async function initGraphs(plugin) {
db.end2G();
// !SECTION Hierarchy Notes
db.start1G("Alternative Hierarchies");
addFolderNotesToGraph(settings, eligableAlts[BC_FOLDER_NOTE], frontms, mainG);
addFolderNotesToGraph(plugin, eligableAlts[BC_FOLDER_NOTE], frontms, mainG);
addTagNotesToGraph(plugin, eligableAlts[BC_TAG_NOTE], frontms, mainG);
addLinkNotesToGraph(plugin, eligableAlts[BC_LINK_NOTE], frontms, mainG);
addRegexNotesToGraph(plugin, eligableAlts[BC_REGEX_NOTE], frontms, mainG);
Expand Down
132 changes: 122 additions & 10 deletions src/AlternativeHierarchies/FolderNotes.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,85 @@
import type { MultiGraph } from "graphology";
import { BC_FOLDER_NOTE, BC_FOLDER_NOTE_SUBFOLDER } from "../constants";
import { TFile, TFolder } from "obsidian";
import { BC_FOLDER_NOTE, BC_FOLDER_NOTE_RECURSIVE } from "../constants";
import { getSourceOrder, getTargetOrder, populateMain } from "../graphUtils";
import type { BCSettings, dvFrontmatterCache } from "../interfaces";
import { getDVBasename, getFields, getFolder } from "../sharedFunctions";
import type BCPlugin from "../main";
import { getDVBasename, getFields, getFolderName } from "../sharedFunctions";

const getSubsFromFolder = (folder: TFolder) => {
const otherNotes: TFile[] = [],
subFolders: TFolder[] = [];
folder.children.forEach((tAbstract) => {
if (tAbstract instanceof TFile) {
otherNotes.push(tAbstract);
} else subFolders.push(tAbstract as TFolder);
});
return { otherNotes, subFolders };
};

// export function addFolderNotesToGraph(
// settings: BCSettings,
// folderNotes: dvFrontmatterCache[],
// frontms: dvFrontmatterCache[],
// mainG: MultiGraph
// ) {
// const { userHiers } = settings;
// const fields = getFields(userHiers);
// folderNotes.forEach((altFile) => {
// const { file } = altFile;
// const basename = getDVBasename(file);
// const topFolder = getFolderName(file);
// const recursive = altFile[BC_FOLDER_NOTE_RECURSIVE];

// const targets = frontms
// .map((ff) => ff.file)
// .filter(
// (other) =>
// getFolderName(other) === topFolder && other.path !== file.path
// )
// .map(getDVBasename);

// const field = altFile[BC_FOLDER_NOTE] as string;
// if (typeof field !== "string" || !fields.includes(field)) return;

// targets.forEach((target) => {
// // This is getting the order of the folder note, not the source pointing up to it
// const sourceOrder = getSourceOrder(altFile);
// const targetOrder = getTargetOrder(frontms, basename);
// populateMain(
// settings,
// mainG,
// basename,
// field,
// target,
// sourceOrder,
// targetOrder,
// true
// );
// });
// });
// }

export function addFolderNotesToGraph(
settings: BCSettings,
eligableAlts: dvFrontmatterCache[],
plugin: BCPlugin,
folderNotes: dvFrontmatterCache[],
frontms: dvFrontmatterCache[],
mainG: MultiGraph
) {
const { settings, app } = plugin;
const { userHiers } = settings;
const fields = getFields(userHiers);
eligableAlts.forEach((altFile) => {
folderNotes.forEach((altFile) => {
const { file } = altFile;
const basename = getDVBasename(file);
const folder = getFolder(file);
const subfolders = altFile[BC_FOLDER_NOTE_SUBFOLDER];
const topFolderName = getFolderName(file);
const topFolder = app.vault.getAbstractFileByPath(topFolderName) as TFolder;

const targets = frontms
.map((ff) => ff.file)
.filter(
(other) =>
(subfolders
? getFolder(other).includes(folder)
: getFolder(other) === folder) && other.path !== file.path
getFolderName(other) === topFolderName && other.path !== file.path
)
.map(getDVBasename);

Expand All @@ -46,5 +101,62 @@ export function addFolderNotesToGraph(
true
);
});

if (altFile[BC_FOLDER_NOTE_RECURSIVE]) {
const { subFolders } = getSubsFromFolder(topFolder);
const folderQueue: TFolder[] = [...subFolders];
console.log({ startingQueue: folderQueue.slice() });

let prevFolderNote = topFolderName;
let currFolder = folderQueue.shift();
while (currFolder !== undefined) {
const { otherNotes, subFolders } = getSubsFromFolder(currFolder);

const folderNote = currFolder.name;
const targets = otherNotes.map(getDVBasename);

// if (!isInVault(app, folderNote, folderNote)) continue;

const sourceOrder = 9999; // getSourceOrder(altFile);
const targetOrder = 9999; // getTargetOrder(frontms, basename);

populateMain(
settings,
mainG,
prevFolderNote,
field,
folderNote,
sourceOrder,
targetOrder,
true
);

targets.forEach((target) => {
if (target === folderNote) return;
console.log("adding", folderNote, "→", target);
const sourceOrder = 9999; // getSourceOrder(altFile);
const targetOrder = 9999; // getTargetOrder(frontms, basename);

populateMain(
settings,
mainG,
folderNote,
field,
target,
sourceOrder,
targetOrder,
true
);
});

folderQueue.push(...subFolders);
currFolder = folderQueue.shift();
prevFolderNote = folderNote;
}
}

// First add otherNotes to graph

// Then iterate subFolders doing the same
});
}
1 change: 0 additions & 1 deletion src/Codeblocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,6 @@ class BCStore extends Component implements ICoreDataStore {
allNodes.map((node) => this.asString(node)).filter((s) => s)
);
newNodes.forEach((node) => {
console.log({ node });
this.graph.forEachOutEdge(
this.asString(node),
(key, attr, source, target) => {
Expand Down
8 changes: 4 additions & 4 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export const blankRealNImplied = () => {

export const [
BC_FOLDER_NOTE,
BC_FOLDER_NOTE_SUBFOLDER,
BC_FOLDER_NOTE_RECURSIVE,
BC_TAG_NOTE,
BC_TAG_NOTE_FIELD,
BC_TAG_NOTE_EXACT,
Expand All @@ -134,7 +134,7 @@ export const [
BC_ORDER,
] = [
"BC-folder-note",
"BC-folder-note-subfolder",
"BC-folder-note-recursive",
"BC-tag-note",
"BC-tag-note-field",
"BC-tag-note-exact",
Expand All @@ -155,8 +155,8 @@ export const BC_FIELDS_INFO = [
alt: true,
},
{
field: BC_FOLDER_NOTE_SUBFOLDER,
desc: "This folder note should take notes in the same folder as it, _and_ notes in subfolders of it.",
field: BC_FOLDER_NOTE_RECURSIVE,
desc: "Recursively add notes in subfolders to the foldernote of _that_ subfolder.",
after: ": true",
alt: false,
},
Expand Down
7 changes: 1 addition & 6 deletions src/refreshIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,12 +287,7 @@ export async function initGraphs(plugin: BCPlugin): Promise<MultiGraph> {
// !SECTION Hierarchy Notes
db.start1G("Alternative Hierarchies");

addFolderNotesToGraph(
settings,
eligableAlts[BC_FOLDER_NOTE],
frontms,
mainG
);
addFolderNotesToGraph(plugin, eligableAlts[BC_FOLDER_NOTE], frontms, mainG);
addTagNotesToGraph(plugin, eligableAlts[BC_TAG_NOTE], frontms, mainG);
addLinkNotesToGraph(plugin, eligableAlts[BC_LINK_NOTE], frontms, mainG);
addRegexNotesToGraph(plugin, eligableAlts[BC_REGEX_NOTE], frontms, mainG);
Expand Down
2 changes: 1 addition & 1 deletion src/sharedFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export const getBaseFromMDPath = (path: string) => {
};

export const getDVBasename = (file: TFile) => file.basename || file.name;
export const getFolder = (file: TFile): string =>
export const getFolderName = (file: TFile): string =>
//@ts-ignore
file?.parent?.name || file.folder;

Expand Down

0 comments on commit d1c3584

Please sign in to comment.