Skip to content

Commit

Permalink
feat(FolderNote): ✨ BC-folder-note-subfolders links to notes in _subf…
Browse files Browse the repository at this point in the history
…olders_ of the current note using the given field
  • Loading branch information
SkepticMystic committed Jan 31, 2022
1 parent 99f4f58 commit 41d6ee5
Show file tree
Hide file tree
Showing 8 changed files with 201 additions and 7,068 deletions.
6,562 changes: 30 additions & 6,532 deletions main.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,6 @@
"obsidian-community-lib": "^1.2.0",
"svelte": "3.35.0",
"svelte-icons": "^2.1.0",
"yaml": "2.0.0-10"
"yaml": "^2.0.0-10"
}
}
42 changes: 38 additions & 4 deletions src/AlternativeHierarchies/FolderNotes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import type { MultiGraph } from "graphology";
import { TFile, TFolder } from "obsidian";
import { BC_FOLDER_NOTE, BC_FOLDER_NOTE_RECURSIVE } from "../constants";
import {
BC_FOLDER_NOTE,
BC_FOLDER_NOTE_RECURSIVE,
BC_FOLDER_NOTE_SUBFOLDERS,
} from "../constants";
import type { dvFrontmatterCache } from "../interfaces";
import type BCPlugin from "../main";
import {
Expand All @@ -15,9 +19,8 @@ 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);
if (tAbstract instanceof TFile) otherNotes.push(tAbstract);
else subFolders.push(tAbstract as TFolder);
});
return { otherNotes, subFolders };
};
Expand All @@ -31,6 +34,7 @@ export function addFolderNotesToGraph(
const { settings, app } = plugin;
const { userHiers } = settings;
const fields = getFields(userHiers);

folderNotes.forEach((altFile) => {
const { file } = altFile;
const basename = getDVBasename(file);
Expand Down Expand Up @@ -64,6 +68,36 @@ export function addFolderNotesToGraph(
);
});

if (altFile[BC_FOLDER_NOTE_SUBFOLDERS]) {
const subfolderField = altFile[BC_FOLDER_NOTE_SUBFOLDERS] as string;
if (
typeof subfolderField !== "string" ||
!fields.includes(subfolderField)
)
return;

const { subFolders } = getSubsFromFolder(topFolder);

subFolders.forEach((subFolder) => {
subFolder.children.forEach((child) => {
if (child instanceof TFile) {
const childBasename = getDVBasename(child);

populateMain(
settings,
mainG,
basename,
subfolderField,
childBasename,
9999,
9999,
true
);
}
});
});
}

if (altFile[BC_FOLDER_NOTE_RECURSIVE]) {
const { subFolders } = getSubsFromFolder(topFolder);
const folderQueue: TFolder[] = [...subFolders];
Expand Down
13 changes: 9 additions & 4 deletions src/Utils/ObsidianUtils.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { info } from "loglevel";
import type { App, FrontMatterCache, TFile } from "obsidian";
import {
App,
FrontMatterCache,
parseYaml,
stringifyYaml,
TFile,
} from "obsidian";
import {
isInVault,
wait,
waitForResolvedLinks,
} from "obsidian-community-lib/dist/utils";
import { parse, stringify } from "yaml";
import type { MetaeditApi } from "../interfaces";
import type BCPlugin from "../main";
import { splitAndTrim } from "./generalUtils";
Expand Down Expand Up @@ -76,7 +81,7 @@ export function changeYaml(yaml: string, key: string, newVal: string): string {
if (yaml === "") {
return `${key}: ['${newVal}']`;
} else {
const parsed: { [key: string]: any } = parse(yaml);
const parsed: { [key: string]: any } = parseYaml(yaml);
const value = parsed[key];
if (value === undefined) {
parsed[key] = newVal;
Expand All @@ -90,7 +95,7 @@ export function changeYaml(yaml: string, key: string, newVal: string): string {
parsed[key] = [...value, newVal];
}
// else if (other types of values...)
return stringify(parsed);
return stringifyYaml(parsed);
}
}

Expand Down
6 changes: 2 additions & 4 deletions src/Utils/graphUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,11 +290,9 @@ export function populateMain(
field,
});
if (fillOpp) {
const oppDir = getOppDir(dir);
const oppField = getOppFields(userHiers, field, dir)[0];
addEdgeIfNot(mainG, target, source, {
dir: oppDir,
field: oppField,
dir: getOppDir(dir),
field: getOppFields(userHiers, field, dir)[0],
});
}
}
Expand Down
9 changes: 9 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ export const [

export const [
BC_FOLDER_NOTE,
BC_FOLDER_NOTE_SUBFOLDERS,
BC_FOLDER_NOTE_RECURSIVE,
BC_TAG_NOTE,
BC_TAG_NOTE_FIELD,
Expand All @@ -150,6 +151,7 @@ export const [
BC_ORDER,
] = [
"BC-folder-note",
"BC-folder-note-subfolders",
"BC-folder-note-recursive",
"BC-tag-note",
"BC-tag-note-field",
Expand All @@ -173,6 +175,13 @@ export const BC_FIELDS_INFO = [
afterInline: ":: ",
alt: true,
},
{
field: BC_FOLDER_NOTE_SUBFOLDERS,
desc: "Link to notes in subfolders with the given field.",
afterYaml: ": ",
afterInline: ":: ",
alt: false,
},
{
field: BC_FOLDER_NOTE_RECURSIVE,
desc: "Recursively add notes in subfolders to the foldernote of _that_ subfolder.",
Expand Down
2 changes: 1 addition & 1 deletion src/refreshIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ export async function buildMainG(plugin: BCPlugin): Promise<MultiGraph> {
db.start2G("addFrontmatterToGraph");
frontms.forEach((page) => {
BC_ALTS.forEach((alt) => {
if (page[alt]) eligableAlts[alt].push(page);
if (page[alt] !== undefined) eligableAlts[alt].push(page);
});

const basename = getDVBasename(page.file);
Expand Down
Loading

0 comments on commit 41d6ee5

Please sign in to comment.