From d1c358454286c29af419eac7656cf2b95069361f Mon Sep 17 00:00:00 2001 From: Ross Keenan Date: Sat, 15 Jan 2022 18:14:31 +0200 Subject: [PATCH] feat(FolderNote): :sparkles: `BC-folder-note-recursive: true` traverses the folder structure, adding nodes along the way --- main.js | 105 ++++++++++++++--- src/AlternativeHierarchies/FolderNotes.ts | 132 ++++++++++++++++++++-- src/Codeblocks.ts | 1 - src/constants.ts | 8 +- src/refreshIndex.ts | 7 +- src/sharedFunctions.ts | 2 +- 6 files changed, 219 insertions(+), 36 deletions(-) diff --git a/main.js b/main.js index c2a9efe3..eb996b10 100644 --- a/main.js +++ b/main.js @@ -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", @@ -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, }, @@ -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("."); @@ -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({ @@ -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)) @@ -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 }); } @@ -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); diff --git a/src/AlternativeHierarchies/FolderNotes.ts b/src/AlternativeHierarchies/FolderNotes.ts index 2ad28d6a..f2749ace 100644 --- a/src/AlternativeHierarchies/FolderNotes.ts +++ b/src/AlternativeHierarchies/FolderNotes.ts @@ -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); @@ -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 }); } diff --git a/src/Codeblocks.ts b/src/Codeblocks.ts index 934323d9..cf6cbcb6 100644 --- a/src/Codeblocks.ts +++ b/src/Codeblocks.ts @@ -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) => { diff --git a/src/constants.ts b/src/constants.ts index d90624d4..0197c12d 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -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, @@ -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", @@ -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, }, diff --git a/src/refreshIndex.ts b/src/refreshIndex.ts index 15ffcc65..bbe15ade 100644 --- a/src/refreshIndex.ts +++ b/src/refreshIndex.ts @@ -287,12 +287,7 @@ export async function initGraphs(plugin: BCPlugin): Promise { // !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); diff --git a/src/sharedFunctions.ts b/src/sharedFunctions.ts index e08a031e..86109ee8 100644 --- a/src/sharedFunctions.ts +++ b/src/sharedFunctions.ts @@ -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;