Skip to content

Commit

Permalink
feat(DataviewNotes): ✨ Dataview Notes! Use BC-dataview-note: query
Browse files Browse the repository at this point in the history
…to run any valid Dataview from-query and add Breadcrumbs to all matching notes
  • Loading branch information
SkepticMystic committed Jan 29, 2022
1 parent 63f062e commit bc2681a
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"NamingNotes",
"Codeblock",
"TreeView",
"impliedRelations"
"impliedRelations",
"DataviewNotes"
]
}
61 changes: 61 additions & 0 deletions src/AlternativeHierarchies/DataviewNotes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import type { MultiGraph } from "graphology";
import { Notice } from "obsidian";
import { BC_DV_NOTE, BC_DV_NOTE_FIELD, DATAVIEW_MISSING } from "../constants";
import type { dvFrontmatterCache } from "../interfaces";
import type BCPlugin from "../main";
import {
getSourceOrder,
getTargetOrder,
populateMain,
} from "../Utils/graphUtils";
import { getFields } from "../Utils/HierUtils";
import { getDVApi, getDVBasename } from "../Utils/ObsidianUtils";

export function addDataviewNotesToGraph(
plugin: BCPlugin,
eligableAlts: dvFrontmatterCache[],
frontms: dvFrontmatterCache[],
mainG: MultiGraph
) {
const { settings } = plugin;
const { userHiers, dataviewNoteField } = settings;
const dv = getDVApi(plugin);
if (!dv) {
new Notice(DATAVIEW_MISSING);
}

const fields = getFields(userHiers);

eligableAlts.forEach((altFile) => {
const basename = getDVBasename(altFile.file);
const query = altFile[BC_DV_NOTE] as string;

let field =
(altFile[BC_DV_NOTE_FIELD] as string) ?? (dataviewNoteField || fields[0]);

let targets: dvFrontmatterCache[] = [];
try {
targets = dv.pages(query).values;
} catch (er) {
new Notice(`${query} is not a valid Dataview from-query`);
console.log(er);
}

for (const target of targets) {
const targetBN = getDVBasename(target.file);
const sourceOrder = getSourceOrder(altFile);
const targetOrder = getTargetOrder(frontms, targetBN);

populateMain(
settings,
mainG,
basename,
field,
targetBN,
sourceOrder,
targetOrder,
true
);
}
});
}
2 changes: 2 additions & 0 deletions src/Settings/BreadcrumbsSettingTab.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { App, PluginSettingTab, Setting } from "obsidian";
import { addDataviewSettings } from "./DataviewNoteSettings";
import KoFi from "../Components/KoFi.svelte";
import type BCPlugin from "../main";
import { addCreateIndexSettings } from "./CreateIndexSettings";
Expand Down Expand Up @@ -113,6 +114,7 @@ export class BCSettingTab extends PluginSettingTab {
addHierarchyNoteSettings(plugin, alternativeHierarchyDetails);
addCSVSettings(plugin, alternativeHierarchyDetails);
addDendronSettings(plugin, alternativeHierarchyDetails);
addDataviewSettings(plugin, alternativeHierarchyDetails);

const cmdsDetails = details("Commands", containerEl);
addWriteBCsSettings(plugin, cmdsDetails);
Expand Down
33 changes: 33 additions & 0 deletions src/Settings/DataviewNoteSettings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { DropdownComponent, Setting } from "obsidian";
import type BCPlugin from "../main";
import { refreshIndex } from "../refreshIndex";
import { getFields } from "../Utils/HierUtils";
import { fragWithHTML, subDetails } from "./BreadcrumbsSettingTab";

export function addDataviewSettings(
plugin: BCPlugin,
alternativeHierarchyDetails: HTMLDetailsElement
) {
const { settings } = plugin;
const { userHiers } = settings;
const fields = getFields(userHiers);
const dvDetails = subDetails("Dataview Notes", alternativeHierarchyDetails);

new Setting(dvDetails)
.setName("Default Tag Note Field")
.setDesc(
fragWithHTML(
"By default, Dataview notes use the first field in your hierarchies (usually an <code>↑</code> field). Choose a different one to use by default, without having to specify <code>BC-dataview-note-field: {field}</code>.</br>If you don't want to choose a default, select the blank option at the bottom of the list."
)
)
.addDropdown((dd: DropdownComponent) => {
const options = {};
fields.forEach((field) => (options[field] = field));
dd.addOptions(Object.assign(options, { "": "" }));
dd.onChange(async (field) => {
settings.dataviewNoteField = field;
await plugin.saveSettings();
await refreshIndex(plugin);
});
});
}
22 changes: 22 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ export const [
BC_TRAVERSE_NOTE,
BC_REGEX_NOTE,
BC_REGEX_NOTE_FIELD,
BC_DV_NOTE,
BC_DV_NOTE_FIELD,
BC_IGNORE_DENDRON,
BC_HIDE_TRAIL,
BC_ORDER,
Expand All @@ -156,6 +158,8 @@ export const [
"BC-traverse-note",
"BC-regex-note",
"BC-regex-note-field",
"BC-dataview-note",
"BC-dataview-note-field",
"BC-ignore-dendron",
"BC-hide-trail",
"BC-order",
Expand Down Expand Up @@ -225,6 +229,20 @@ export const BC_FIELDS_INFO = [
afterInline: ":: ",
alt: false,
},
{
field: BC_DV_NOTE,
desc: "Set this note as a Breadcrumbs Dataview-note. The value of this field is a Dataview `from` query. All notes that match the query will be added to the BC graph using the default fieldName specified in `Settings > Alternative Hierarchies > Dataview Notes > Default Field`, or using the fieldName you specify in 'BC-dataview-note-field'.",
afterYaml: ": '",
afterInline: ":: ",
alt: true,
},
{
field: BC_DV_NOTE_FIELD,
desc: "Manually choose the field for this Dataview-note to use",
afterYaml: ": ",
afterInline: ":: ",
alt: false,
},
{
field: BC_IGNORE_DENDRON,
desc: "Tells Breadcrumbs to not treat this note as a dendron note (only useful if the note name has you dendron splitter in it, usually a period `.`).",
Expand Down Expand Up @@ -262,13 +280,17 @@ export const ILLEGAL_FILENAME_CHARS = [
"|",
];

export const DATAVIEW_MISSING =
"The Dataview plugin must be installed for this to work";

export const DEFAULT_SETTINGS: BCSettings = {
addDendronNotes: false,
aliasesInIndex: false,
alphaSortAsc: true,
altLinkFields: [],
CSVPaths: "",
dateFormat: "YYYY-MM-DD",
dataviewNoteField: "up",
debugMode: "WARN",
defaultView: true,
dendronNoteDelimiter: ".",
Expand Down
2 changes: 2 additions & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export interface BCSettings {
altLinkFields: string[];
CSVPaths: string;
dvWaitTime: number;
dataviewNoteField: string;
debugMode: DebugLevel;
defaultView: boolean;
dendronNoteDelimiter: string;
Expand Down Expand Up @@ -275,6 +276,7 @@ declare module "obsidian" {
dataview: {
api: {
page: (page: string) => dvFrontmatterCache;
pages: (query: string) => { values: dvFrontmatterCache[] };
pagePaths: (query?: string) => { values: string[] };
};
};
Expand Down
3 changes: 3 additions & 0 deletions src/refreshIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { debug, error } from "loglevel";
import { normalizePath, Notice, Pos, TFile, TFolder } from "obsidian";
import { wait } from "obsidian-community-lib";
import { addCSVCrumbs, getCSVRows } from "./AlternativeHierarchies/CSVCrumbs";
import { addDataviewNotesToGraph } from "./AlternativeHierarchies/DataviewNotes";
import { addDendronNotesToGraph } from "./AlternativeHierarchies/DendronNotes";
import { addFolderNotesToGraph } from "./AlternativeHierarchies/FolderNotes";
import {
Expand All @@ -19,6 +20,7 @@ import { addTagNotesToGraph } from "./AlternativeHierarchies/TagNotes";
import { addTraverseNotesToGraph } from "./AlternativeHierarchies/TraverseNotes";
import {
BC_ALTS,
BC_DV_NOTE,
BC_FOLDER_NOTE,
BC_I_AUNT,
BC_I_COUSIN,
Expand Down Expand Up @@ -283,6 +285,7 @@ export async function buildMainG(plugin: BCPlugin): Promise<MultiGraph> {
buildObsGraph(app)
);
addDendronNotesToGraph(plugin, frontms, mainG);
addDataviewNotesToGraph(plugin, eligableAlts[BC_DV_NOTE], frontms, mainG);

db.end1G();

Expand Down

0 comments on commit bc2681a

Please sign in to comment.