Skip to content

Commit

Permalink
feat(NamingNotes): 🚧 Progress
Browse files Browse the repository at this point in the history
  • Loading branch information
SkepticMystic committed Jan 7, 2022
1 parent f465bca commit 342d747
Show file tree
Hide file tree
Showing 6 changed files with 403 additions and 19 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"DownView",
"DendronNote",
"Threading",
"RegexNote"
"RegexNote",
"NamingNotes"
]
}
194 changes: 183 additions & 11 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -21040,7 +21040,7 @@ const BC_FIELDS_INFO = [
},
{
field: BC_TAG_NOTE,
desc: "Set this note as a Breadcrumbs tag-note. All other notes with this tag will be added to the graph in the direction you specify with `BC-tag-note-field: fieldName`",
desc: "Set this note as a Breadcrumbs tag-note. All other notes with this tag will be added to the graph using the default fieldName specified in `Settings > Alternative Hierarchies > Tag Notes > Default Field`, or using the fieldName you specify with `BC-tag-note-field: fieldName`",
after: ": '#",
alt: true,
},
Expand Down Expand Up @@ -21070,7 +21070,7 @@ const BC_FIELDS_INFO = [
},
{
field: BC_REGEX_NOTE,
desc: "Set this note as a Breadcrumbs regex-note. The value of this field is a regular expression (of the form '/regex/flags'). All note names that match the regex will be added to the BC graph using the default fieldName specified in `Settings > Alternative Hierarchies > Regex Note > Default Field`, or using the fieldName you specify in 'BC-regex-note-field'.",
desc: "Set this note as a Breadcrumbs regex-note. The value of this field is a regular expression (of the form '/regex/flags'). All note names that match the regex will be added to the BC graph using the default fieldName specified in `Settings > Alternative Hierarchies > Regex Notes > Default Field`, or using the fieldName you specify in 'BC-regex-note-field'.",
after: ": '/",
alt: true,
},
Expand Down Expand Up @@ -21119,6 +21119,10 @@ const DEFAULT_SETTINGS = {
hierarchyNotes: [""],
HNUpField: "",
indexNotes: [""],
namingSystemField: "",
namingSystemRegex: "",
namingSystemSplit: ".",
namingSystemEndsWithDelimiter: false,
refreshOnNoteChange: false,
useAllMetadata: true,
openMatrixOnLoad: true,
Expand Down Expand Up @@ -22625,6 +22629,20 @@ function iterateHiers(userHiers, fn) {
});
});
});
}
function strToRegex(input) {
const match = input.match(regNFlags);
if (!match)
return null;
const [, innerRegex, flags] = match;
try {
const regex = new RegExp(innerRegex, flags);
return regex;
}
catch (e) {
console.log(e);
return null;
}
}

/* node_modules\svelte-icons\components\IconBase.svelte generated by Svelte v3.35.0 */
Expand Down Expand Up @@ -25082,6 +25100,7 @@ class BCSettingTab extends require$$0.PluginSettingTab {
const { settings } = plugin;
containerEl.empty();
containerEl.createEl("h2", { text: "Settings for Breadcrumbs plugin" });
const fields = getFields(settings.userHiers);
const details = (text, parent = containerEl) => parent.createEl("details", {}, (d) => d.createEl("summary", { text }));
const subDetails = (text, parent) => parent
.createDiv({
Expand Down Expand Up @@ -25606,6 +25625,60 @@ class BCSettingTab extends require$$0.PluginSettingTab {
await plugin.refreshIndex();
});
});
const noSystemDetails = subDetails("Naming System", alternativeHierarchyDetails);
new require$$0.Setting(noSystemDetails)
.setName("Naming System Regex")
.setDesc(fragWithHTML("If you name your notes using the Johnny Decimal System or a related system, enter a regular expression matching the longest possible naming system you use. The regex should only match the naming system part of the name, not the actual note title.</br> For example, if you use the Johnny Decimal System, you might use <code>/^\\d\\.\\d\\.\\w/g</code> to match the note named <code>1.2.a Cars</code>.</br>If you don't want to choose a default, select the blank option at the bottom of the list."))
.addText((text) => {
text.setValue(settings.namingSystemRegex);
text.inputEl.onblur = async () => {
const value = text.getValue();
if (value === "" || strToRegex(value)) {
settings.namingSystemRegex = value;
await plugin.saveSettings();
await plugin.refreshIndex();
}
else {
new require$$0.Notice("Invalid Regex");
}
};
});
new require$$0.Setting(noSystemDetails)
.setName("Naming System Delimiter")
.setDesc(fragWithHTML("What character do you use to split up your naming convention? For example, if you use <code>1.2.a.b</code>, then your delimiter is a period (<code>.</code>)."))
.addText((text) => {
text.setValue(settings.namingSystemSplit);
text.inputEl.onblur = async () => {
const value = text.getValue();
settings.namingSystemSplit = value;
await plugin.saveSettings();
await plugin.refreshIndex();
};
});
new require$$0.Setting(noSystemDetails)
.setName("Naming System Field")
.setDesc("Which field should Breadcrumbs use for Naming System notes?")
.addDropdown((dd) => {
fields.forEach((field) => {
dd.addOption(field, field);
});
dd.setValue(settings.namingSystemField);
dd.onChange(async (value) => {
settings.namingSystemField = value;
await plugin.saveSettings();
await plugin.refreshIndex();
});
});
new require$$0.Setting(noSystemDetails)
.setName("Naming System Ends with Delimiter")
.setDesc(fragWithHTML("Does your naming convention end with the delimiter? For example, <code>1.2. Note</code> does end with the delimiter, but <code>1.2 Note</code> does not.</br>For matching purposes, it is highly recommended to name your notes with the delimiter on the end. Only turn this setting on if you do name your notes this way, but know that the results may not be as accurate if you don't."))
.addToggle((tog) => tog
.setValue(settings.namingSystemEndsWithDelimiter)
.onChange(async (value) => {
settings.namingSystemEndsWithDelimiter = value;
await plugin.saveSettings();
await plugin.refreshIndex();
}));
const hierarchyNoteDetails = subDetails("Hierarchy Notes", alternativeHierarchyDetails);
new require$$0.Setting(hierarchyNoteDetails)
.setName("Hierarchy Note(s)")
Expand Down Expand Up @@ -25701,7 +25774,6 @@ class BCSettingTab extends require$$0.PluginSettingTab {
await plugin.saveSettings();
await plugin.getActiveTYPEView(MATRIX_VIEW).draw();
}));
const fields = getFields(settings.userHiers);
if (!fields.includes(settings.dendronNoteField)) {
settings.dendronNoteField = fields[0];
await plugin.saveSettings();
Expand Down Expand Up @@ -51760,7 +51832,7 @@ class BCPlugin extends require$$0.Plugin {
...((_a = tags === null || tags === void 0 ? void 0 : tags.map((t) => t.tag.slice(1))) !== null && _a !== void 0 ? _a : []),
...[...((_b = frontmatter === null || frontmatter === void 0 ? void 0 : frontmatter.tags) !== null && _b !== void 0 ? _b : [])].flat(),
...[...((_c = frontmatter === null || frontmatter === void 0 ? void 0 : frontmatter.tag) !== null && _c !== void 0 ? _c : [])].flat(),
].map((t) => (withHash ? "#" : "") + t.toLowerCase());
].map((t) => (!t.startsWith("#") && withHash ? "#" : "") + t.toLowerCase());
};
this.getTargetOrder = (frontms, target) => {
var _a, _b;
Expand Down Expand Up @@ -52567,10 +52639,8 @@ class BCPlugin extends require$$0.Plugin {
eligableAlts.forEach((altFile) => {
const regexNoteFile = altFile.file;
const regexNoteBasename = getDVBasename(regexNoteFile);
const outerRegex = altFile[BC_REGEX_NOTE];
const [_, innerRegex, flags] = outerRegex.match(regNFlags);
const regex = new RegExp(innerRegex, flags);
loglevel.info({ innerRegex, regex });
const regex = strToRegex(altFile[BC_REGEX_NOTE]);
loglevel.info({ regex });
let field = altFile[BC_REGEX_NOTE_FIELD];
if (typeof field !== "string" || !fields.includes(field))
field = regexNoteField || fields[0];
Expand All @@ -52587,6 +52657,109 @@ class BCPlugin extends require$$0.Plugin {
}
});
}
addNamingSystemNotesToGraph(frontms, mainG) {
const { namingSystemRegex, namingSystemSplit, namingSystemField, namingSystemEndsWithDelimiter, userHiers, } = this.settings;
const regex = strToRegex(namingSystemRegex);
if (!regex)
return;
const field = namingSystemField || getFields(userHiers)[0];
// const visited: string[] = [];
// const deepestMatches = frontms.filter((page) => {
// const basename = getDVBasename(page.file);
// return regex.test(basename);
// });
function splitRegex(regex, split) {
const { source } = regex;
const parts = source.split(split);
const sliced = parts
.slice(0, -1)
.map((p) => (p.endsWith("\\") ? p.slice(0, -1) : p));
let joined = sliced.join("\\" + split);
joined = joined.startsWith("^") ? joined : "^" + joined;
// joined =
// joined +
// (namingSystemEndsWithDelimiter ? "\\" + namingSystemSplit : "");
return sliced.length ? new RegExp(joined) : null;
}
function getUp(current) {
var _a;
let currReg = splitRegex(regex, namingSystemSplit);
let up = current.match(currReg);
while (!up || up[0] === current) {
if (!currReg)
break;
currReg = splitRegex(currReg, namingSystemSplit);
if (!currReg)
break;
up = current.match(currReg);
}
console.log({ currReg });
return (_a = up === null || up === void 0 ? void 0 : up[0]) !== null && _a !== void 0 ? _a : null;
}
frontms.forEach((page) => {
const sourceBN = getDVBasename(page.file);
const upSystem = getUp(sourceBN);
if (!upSystem)
return;
console.log(sourceBN, "↑", upSystem);
const upFm = frontms.find((fm) => {
const basename = getDVBasename(fm.file);
const start = upSystem + (namingSystemEndsWithDelimiter ? namingSystemSplit : "");
return (basename !== sourceBN &&
(basename === start || basename.startsWith(start + " ")));
});
if (!upFm)
return;
const upName = getDVBasename(upFm.file);
if (upName === sourceBN)
return;
const sourceOrder = this.getSourceOrder(page);
const targetOrder = this.getTargetOrder(frontms, upName);
this.populateMain(mainG, sourceBN, field, upName, sourceOrder, targetOrder, true);
});
// deepestMatches.forEach((deepest) => {
// console.log(deepest.file.name);
// const basename = getDVBasename(deepest.file);
// const allSplits: string[] = [];
// let nextSplit = splitName(basename, namingSystemSplit);
// while (nextSplit) {
// allSplits.push(nextSplit);
// nextSplit = splitName(nextSplit, namingSystemSplit);
// }
// console.log({ allSplits });
// let current: dvFrontmatterCache = deepest;
// for (const split of allSplits) {
// const up = frontms.find((page) => {
// const basename = getDVBasename(page.file);
// return (
// !visited.includes(basename) &&
// // For the final split, the naming system part likely won't have any delimiters in it. This means that alot more false positives will match
// // e.g. if system is `\d\.\d\.`, and the final split is `1`, then something like `1 of my favourites snacks` might match before `1 Index`.
// // The setting `namingSystemEndsWithDelimiter` tries to account for this
// basename.startsWith(
// split + (namingSystemEndsWithDelimiter ? namingSystemSplit : "")
// )
// );
// });
// if (!up) continue;
// const upName = getDVBasename(up.file);
// visited.push(upName);
// console.log("up:", upName);
// const sourceOrder = this.getSourceOrder(current);
// const targetOrder = this.getTargetOrder(frontms, upName);
// this.populateMain(
// mainG,
// getDVBasename(current.file),
// field,
// upName,
// sourceOrder,
// targetOrder,
// true
// );
// current = up;
// }
// });
}
addTraverseNotesToGraph(traverseNotes, frontms, mainG, obsG) {
const { userHiers } = this.settings;
traverseNotes.forEach((altFile) => {
Expand Down Expand Up @@ -52665,9 +52838,7 @@ class BCPlugin extends require$$0.Plugin {
const useCSV = settings.CSVPaths !== "";
const CSVRows = useCSV ? await this.getCSVRows() : [];
const eligableAlts = {};
BC_ALTS.forEach((alt) => {
eligableAlts[alt] = [];
});
BC_ALTS.forEach((alt) => (eligableAlts[alt] = []));
function noticeIfBroken(frontm) {
const basename = getDVBasename(frontm.file);
// @ts-ignore
Expand Down Expand Up @@ -52739,6 +52910,7 @@ class BCPlugin extends require$$0.Plugin {
this.addTagNotesToGraph(eligableAlts[BC_TAG_NOTE], frontms, mainG);
this.addLinkNotesToGraph(eligableAlts[BC_LINK_NOTE], frontms, mainG);
this.addRegexNotesToGraph(eligableAlts[BC_REGEX_NOTE], frontms, mainG);
// this.addNamingSystemNotesToGraph(frontms, mainG);
this.addTraverseNotesToGraph(eligableAlts[BC_TRAVERSE_NOTE], frontms, mainG, this.buildObsGraph());
this.addDendronNotesToGraph(frontms, mainG);
db.end1G();
Expand Down
78 changes: 75 additions & 3 deletions src/BreadcrumbsSettingTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
import type { DebugLevel, Relations, visTypes } from "./interfaces";
import type BCPlugin from "./main";
import MatrixView from "./MatrixView";
import { getFields, splitAndTrim } from "./sharedFunctions";
import { getFields, splitAndTrim, strToRegex } from "./sharedFunctions";

const fragWithHTML = (html: string) =>
createFragment((frag) => (frag.createDiv().innerHTML = html));
Expand All @@ -44,6 +44,7 @@ export class BCSettingTab extends PluginSettingTab {
const { settings } = plugin;
containerEl.empty();
containerEl.createEl("h2", { text: "Settings for Breadcrumbs plugin" });
const fields = getFields(settings.userHiers);

const details = (text: string, parent = containerEl) =>
parent.createEl("details", {}, (d) => d.createEl("summary", { text }));
Expand Down Expand Up @@ -770,6 +771,79 @@ export class BCSettingTab extends PluginSettingTab {
});
});

const noSystemDetails = subDetails(
"Naming System",
alternativeHierarchyDetails
);

new Setting(noSystemDetails)
.setName("Naming System Regex")
.setDesc(
fragWithHTML(
"If you name your notes using the Johnny Decimal System or a related system, enter a regular expression matching the longest possible naming system you use. The regex should only match the naming system part of the name, not the actual note title.</br> For example, if you use the Johnny Decimal System, you might use <code>/^\\d\\.\\d\\.\\w/g</code> to match the note named <code>1.2.a Cars</code>.</br>If you don't want to choose a default, select the blank option at the bottom of the list."
)
)
.addText((text) => {
text.setValue(settings.namingSystemRegex);
text.inputEl.onblur = async () => {
const value = text.getValue();
if (value === "" || strToRegex(value)) {
settings.namingSystemRegex = value;
await plugin.saveSettings();
await plugin.refreshIndex();
} else {
new Notice("Invalid Regex");
}
};
});
new Setting(noSystemDetails)
.setName("Naming System Delimiter")
.setDesc(
fragWithHTML(
"What character do you use to split up your naming convention? For example, if you use <code>1.2.a.b</code>, then your delimiter is a period (<code>.</code>)."
)
)
.addText((text) => {
text.setValue(settings.namingSystemSplit);
text.inputEl.onblur = async () => {
const value = text.getValue();
settings.namingSystemSplit = value;
await plugin.saveSettings();
await plugin.refreshIndex();
};
});

new Setting(noSystemDetails)
.setName("Naming System Field")
.setDesc("Which field should Breadcrumbs use for Naming System notes?")
.addDropdown((dd) => {
fields.forEach((field) => {
dd.addOption(field, field);
});
dd.setValue(settings.namingSystemField);
dd.onChange(async (value) => {
settings.namingSystemField = value;
await plugin.saveSettings();
await plugin.refreshIndex();
});
});
new Setting(noSystemDetails)
.setName("Naming System Ends with Delimiter")
.setDesc(
fragWithHTML(
"Does your naming convention end with the delimiter? For example, <code>1.2. Note</code> does end with the delimiter, but <code>1.2 Note</code> does not.</br>For matching purposes, it is highly recommended to name your notes with the delimiter on the end. Only turn this setting on if you do name your notes this way, but know that the results may not be as accurate if you don't."
)
)
.addToggle((tog) =>
tog
.setValue(settings.namingSystemEndsWithDelimiter)
.onChange(async (value) => {
settings.namingSystemEndsWithDelimiter = value;
await plugin.saveSettings();
await plugin.refreshIndex();
})
);

const hierarchyNoteDetails = subDetails(
"Hierarchy Notes",
alternativeHierarchyDetails
Expand Down Expand Up @@ -897,8 +971,6 @@ export class BCSettingTab extends PluginSettingTab {
})
);

const fields = getFields(settings.userHiers);

if (!fields.includes(settings.dendronNoteField)) {
settings.dendronNoteField = fields[0];
await plugin.saveSettings();
Expand Down
Loading

0 comments on commit 342d747

Please sign in to comment.