FFLOW 0.4.0: {{date}}/{{time}} tokens with moment formats in step values
This commit is contained in:
1 parent
157e5e018a
commit
f5992e4539
8 files changed
+68
-20
No files matched your search
+17
-8
@@ -1,5 +1,6 @@
|
||||
import { App, MarkdownView, TFile } from "obsidian";
|
||||
import type { Flow, FlowEffect } from "./settings";
|
||||
import { matchesTemplate, renderTemplate } from "./templates";
|
||||
|
||||
export type Frontmatter = Record<string, unknown>;
|
||||
|
||||
@@ -24,8 +25,9 @@ export function setOpenValue(fm: Frontmatter, property: string, value: string, a
|
||||
/**
|
||||
* Puts the note on step `value` of an ordered flow:
|
||||
* 1. sets the flow's property;
|
||||
* 2. undoes the effects of every other step (list items removed, scalars removed only if unchanged since the flow wrote them);
|
||||
* 3. applies the new step's effects (list items added, scalars replaced).
|
||||
* 2. undoes the effects of every other step (list items removed, scalars removed only if they still hold what the flow wrote —
|
||||
* for `{{date}}` values, any date in that format);
|
||||
* 3. applies the new step's effects (list items added, scalars replaced), rendering date/time tokens.
|
||||
*/
|
||||
export function setFlowStep(app: App, fm: Frontmatter, flow: Flow, value: string): void {
|
||||
fm[flow.property] = value;
|
||||
@@ -45,11 +47,11 @@ export function setFlowStep(app: App, fm: Frontmatter, flow: Flow, value: string
|
||||
for (const effect of step.effects.filter(usable)) {
|
||||
const p = effect.property;
|
||||
if (isList(p)) {
|
||||
const remove = new Set(listItems(effect.value));
|
||||
const kept = toList(fm[p], p).filter((item) => !remove.has(item));
|
||||
const templates = listItems(effect.value);
|
||||
const kept = toList(fm[p], p).filter((item) => !templates.some((t) => effectMatches(t, item)));
|
||||
if (kept.length) fm[p] = kept;
|
||||
else delete fm[p];
|
||||
} else if (p in fm && String(fm[p]) === String(parseScalar(effect.value))) {
|
||||
} else if (p in fm && effectMatches(effect.value, fm[p])) {
|
||||
delete fm[p];
|
||||
}
|
||||
}
|
||||
@@ -59,10 +61,10 @@ export function setFlowStep(app: App, fm: Frontmatter, flow: Flow, value: string
|
||||
const p = effect.property;
|
||||
if (isList(p)) {
|
||||
const list = toList(fm[p], p);
|
||||
for (const item of listItems(effect.value)) if (!list.includes(item)) list.push(item);
|
||||
for (const item of listItems(effect.value).map(renderTemplate)) if (!list.includes(item)) list.push(item);
|
||||
if (list.length) fm[p] = list;
|
||||
} else {
|
||||
fm[p] = parseScalar(effect.value);
|
||||
fm[p] = parseScalar(renderTemplate(effect.value));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -84,13 +86,20 @@ function toList(raw: unknown, property: string): string[] {
|
||||
return property === "cssclasses" ? String(raw).split(/[\s,]+/).filter(Boolean) : [String(raw)];
|
||||
}
|
||||
|
||||
/** Comma-separated items; commas inside `{{…}}` (e.g. a date format) don't split. */
|
||||
function listItems(value: string): string[] {
|
||||
return value
|
||||
.split(",")
|
||||
.split(/,(?![^{]*\}\})/)
|
||||
.map((v) => v.trim())
|
||||
.filter(Boolean);
|
||||
}
|
||||
|
||||
/** Whether a value in the note is what `template` wrote: same typed value, or a date/time rendered from it. */
|
||||
function effectMatches(template: string, actual: unknown): boolean {
|
||||
const t = template.trim();
|
||||
return String(parseScalar(t)) === String(actual) || matchesTemplate(t, String(actual));
|
||||
}
|
||||
|
||||
/** "true"/"false" become booleans and plain numbers become numbers, so checkbox and number properties stay typed. */
|
||||
function parseScalar(value: string): string | number | boolean | null {
|
||||
const v = value.trim();
|
||||
|
||||
@@ -179,6 +179,10 @@ export class FflowSettingTab extends PluginSettingTab {
|
||||
flow.steps.push({ value: "", effects: [] });
|
||||
await this.saveAndRedraw();
|
||||
});
|
||||
body.createDiv({
|
||||
cls: "fflow-hint",
|
||||
text: "Values: true/false and numbers are typed. Lists take a, b. {{date}} and {{time}} insert the current date and time; set a format with {{date:DD/MM/YYYY}} or {{time:HH:mm:ss}} (moment.js).",
|
||||
});
|
||||
}
|
||||
|
||||
private renderStep(parent: HTMLElement, flow: Flow, step: FlowStep, i: number, updateHeader: () => void): void {
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
import { moment } from "obsidian";
|
||||
|
||||
/** `{{date}}`, `{{time}}`, `{{date:FORMAT}}`, `{{time:FORMAT}}` — same syntax as Obsidian's core Templates plugin. */
|
||||
const TOKEN_SOURCE = String.raw`\{\{(date|time)(?::([^}]*))?\}\}`;
|
||||
const DEFAULT_FORMAT: Record<string, string> = { date: "YYYY-MM-DD", time: "HH:mm" };
|
||||
|
||||
const formatOf = (kind: string, format: string | undefined) => format?.trim() || DEFAULT_FORMAT[kind];
|
||||
|
||||
/** Replaces date/time tokens with the current moment, formatted with moment.js. */
|
||||
export function renderTemplate(template: string): string {
|
||||
const now = moment();
|
||||
return template.replace(new RegExp(TOKEN_SOURCE, "g"), (_token, kind: string, format?: string) => now.format(formatOf(kind, format)));
|
||||
}
|
||||
|
||||
/**
|
||||
* True when `actual` could have been produced by `template` at some moment: the literal text matches
|
||||
* and every token's slot parses strictly with its format. Lets FFLOW recognize a date it wrote earlier.
|
||||
*/
|
||||
export function matchesTemplate(template: string, actual: string): boolean {
|
||||
const token = new RegExp(TOKEN_SOURCE, "g");
|
||||
const formats: string[] = [];
|
||||
let pattern = "^";
|
||||
let last = 0;
|
||||
for (let m = token.exec(template); m; m = token.exec(template)) {
|
||||
pattern += escapeRegExp(template.slice(last, m.index)) + "(.+?)";
|
||||
formats.push(formatOf(m[1], m[2]));
|
||||
last = m.index + m[0].length;
|
||||
}
|
||||
if (formats.length === 0) return template === actual;
|
||||
const match = new RegExp(pattern + escapeRegExp(template.slice(last)) + "$").exec(actual);
|
||||
return match !== null && formats.every((format, i) => moment(match[i + 1], format, true).isValid());
|
||||
}
|
||||
|
||||
function escapeRegExp(text: string): string {
|
||||
return text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
||||
}
|
||||
Reference in new issue
Block a user