FFLOW 0.4.0: {{date}}/{{time}} tokens with moment formats in step values

This commit is contained in:
olivier committed 2026-10-25 18:11:28 -04:00
1 parent 157e5e018a
commit f5992e4539
8 files changed
+68 -20

No files matched your search

+2
View File
@@ -33,6 +33,8 @@ Picking a step, by symbol or by hotkey:
List properties (`cssclasses`, `tags`, `aliases`, any property Obsidian types as a list, or one that already holds a list) take comma-separated items and are merged with what's there. Other properties are replaced; `true`/`false` and plain numbers are written typed. List properties (`cssclasses`, `tags`, `aliases`, any property Obsidian types as a list, or one that already holds a list) take comma-separated items and are merged with what's there. Other properties are replaced; `true`/`false` and plain numbers are written typed.
Date and time, as in Obsidian's core Templates: `{{date}}` (`YYYY-MM-DD`), `{{time}}` (`HH:mm`), or any [moment.js format](https://momentjs.com/docs/#/displaying/format/) with `{{date:DD/MM/YYYY}}`, `{{time:HH:mm:ss}}`, `{{date:YYYY-MM-DD[T]HH:mm}}`. Tokens can sit inside text (`done on {{date}}`). Leaving the step removes the value only if it still matches the template, i.e. a date the flow wrote.
A note without a value, or with a value outside the flow, enters at the first step. Properties change only through FFLOW, not when edited by hand. A note without a value, or with a value outside the flow, enters at the first step. Properties change only through FFLOW, not when edited by hand.
## Development ## Development
+6 -9
View File
@@ -1,20 +1,17 @@
---
status: done
cssclasses:
- st-done
archived: true
---
# FFLOW roadmap # FFLOW roadmap
## Next ## Next
- **Steps from a note.** Per ordered flow: take the steps from the plugin settings (current) or from a note in the vault (one value per line or list items). Nothing planned.
- **Dynamic effect values**, e.g. `completed: {{date}}` on a `done` step.
## Dropped
- Steps from a note (take an ordered flow's steps from a vault note instead of the settings).
## Done ## Done
- 0.4.0 — Date/time values in "also sets": `{{date}}`, `{{time}}`, `{{date:FORMAT}}`, `{{time:FORMAT}}` (moment.js). Leaving the step removes a date the flow wrote.
- 0.3.1 — Settings redesigned: collapsible flow cards with a step summary, one row of labelled fields, steps as a table with their "also sets" properties. - 0.3.1 — Settings redesigned: collapsible flow cards with a step summary, one row of labelled fields, steps as a table with their "also sets" properties.
- 0.3.0 — Everything is a flow. Ordered flows: steps with "also sets" effects on any property (cssclasses included), undone when leaving the step; symbol and next/previous hotkeys apply the same thing. Open flows: vault values, single or list. Old settings migrated. - 0.3.0 — Everything is a flow. Ordered flows: steps with "also sets" effects on any property (cssclasses included), undone when leaving the step; symbol and next/previous hotkeys apply the same thing. Open flows: vault values, single or list. Old settings migrated.
- 0.2.0 — Flows with per-step cssclasses and next/previous commands, separate from triggers. - 0.2.0 — Flows with per-step cssclasses and next/previous commands, separate from triggers.
- 0.1.0 — Triggers: symbol → property, open (vault) or fixed list, single value or list. - 0.1.0 — Triggers: symbol → property, open (vault) or fixed list, single value or list.
+1 -1
View File
@@ -2,4 +2,4 @@
THIS IS A GENERATED/BUNDLED FILE BY ESBUILD THIS IS A GENERATED/BUNDLED FILE BY ESBUILD
*/ */
var P=Object.defineProperty;var G=Object.getOwnPropertyDescriptor;var J=Object.getOwnPropertyNames;var Q=Object.prototype.hasOwnProperty;var X=(n,s)=>{for(var t in s)P(n,t,{get:s[t],enumerable:!0})},Z=(n,s,t,e)=>{if(s&&typeof s=="object"||typeof s=="function")for(let o of J(s))!Q.call(n,o)&&o!==t&&P(n,o,{get:()=>s[o],enumerable:!(e=G(s,o))||e.enumerable});return n};var ee=n=>Z(P({},"__esModule",{value:!0}),n);var re={};X(re,{default:()=>M});module.exports=ee(re);var U=require("obsidian");var I=require("obsidian");var H=require("obsidian");async function b(n,s,t){let e=n.workspace.getActiveViewOfType(H.MarkdownView);(e==null?void 0:e.file)===s&&await e.save(),await n.fileManager.processFrontMatter(s,t)}function K(n,s,t,e){if(!e){n[s]=t;return}let o=V(n[s],s);o.includes(t)||o.push(t),n[s]=o}function x(n,s,t,e){var p;s[t.property]=e;let o=t.steps.find(l=>l.value===e),r=new Map,i=l=>{let d=r.get(l);return d===void 0&&r.set(l,d=te(n,s,l)),d},a=l=>l.property!==""&&l.property!==t.property;for(let l of t.steps)if(l!==o)for(let d of l.effects.filter(a)){let c=d.property;if(i(c)){let m=new Set($(d.value)),g=V(s[c],c).filter(h=>!m.has(h));g.length?s[c]=g:delete s[c]}else c in s&&String(s[c])===String(j(d.value))&&delete s[c]}for(let l of(p=o==null?void 0:o.effects.filter(a))!=null?p:[]){let d=l.property;if(i(d)){let c=V(s[d],d);for(let m of $(l.value))c.includes(m)||c.push(m);c.length&&(s[d]=c)}else s[d]=j(l.value)}}function te(n,s,t){if(["cssclasses","tags","aliases"].includes(t)||Array.isArray(s[t]))return!0;let e=Reflect.get(n,"metadataTypeManager");if(!e||typeof e!="object"||!("getPropertyInfo"in e)||typeof e.getPropertyInfo!="function")return!1;let o=e.getPropertyInfo(t),r=o&&typeof o=="object"&&"widget"in o?o.widget:null;return r==="multitext"||r==="tags"||r==="aliases"}function V(n,s){return Array.isArray(n)?n.map(String):n==null||n===""?[]:s==="cssclasses"?String(n).split(/[\s,]+/).filter(Boolean):[String(n)]}function $(n){return n.split(",").map(s=>s.trim()).filter(Boolean)}function j(n){let s=n.trim();return s===""?null:s==="true"||s==="false"?s==="true":/^-?\d+(\.\d+)?$/.test(s)?Number(s):s}var C=class{constructor(s){this.plugin=s;this.registered=[]}sync(){for(let s of this.registered)this.plugin.removeCommand(s);this.registered=[];for(let s of this.plugin.settings.flows)s.kind==="ordered"&&(this.add(s,1,"next","next step"),this.add(s,-1,"previous","previous step"))}add(s,t,e,o){let r=`flow-${s.id}-${e}`;this.registered.push(r),this.plugin.addCommand({id:r,name:`${s.name||s.property||"Unnamed flow"}: ${o}`,checkCallback:i=>{let a=this.plugin.app.workspace.getActiveFile();return!a||a.extension!=="md"||!s.property||!s.steps.some(p=>p.value)?!1:(i||se(this.plugin,a,s,t),!0)}})}};async function se(n,s,t,e){let o=t.steps.filter(i=>i.value),r="";try{await b(n.app,s,i=>{let a=o.findIndex(l=>l.value===String(i[t.property])),p=a===-1?0:a+e;if(p<0||p>=o.length){if(!t.loop){r=`${t.property}: already at the ${e===1?"last":"first"} step`;return}p=(p+o.length)%o.length}x(n.app,i,t,o[p].value),r=`${t.property}: ${o[p].value}`}),new I.Notice(r)}catch(i){console.error("FFLOW: could not move flow step",i),new I.Notice(`FFLOW: could not set "${t.property}". See console.`)}}function v(n){return{id:Date.now().toString(36)+Math.random().toString(36).slice(2,6),name:"",symbol:"",property:"",kind:"ordered",valueMode:"single",loop:!1,steps:[],...n}}function ne(){return{flows:[v({name:"Type",symbol:"@",property:"type",kind:"open"})]}}var k=["#","[","/",":"];function S(n,s,t){return n===""?null:[...n].length!==1?"Use exactly one character.":/[\p{L}\p{N}\s]/u.test(n)?"Letters, digits and spaces are not allowed.":k.includes(n)?`"${n}" is reserved (${k.join(" ")}).`:s.some((e,o)=>o!==t&&e.symbol===n)?`"${n}" is already used by another flow.`:null}function W(n){var o,r;if(!n||typeof n!="object")return ne();if("flows"in n&&!("rules"in n)&&Array.isArray(n.flows))return{flows:n.flows.map(i=>v(i))};let s=n,t=(o=s.rules)!=null?o:[],e=((r=s.flows)!=null?r:[]).map(i=>{var a,p;return v({id:i.id,name:i.name,property:i.property,loop:i.loop,symbol:(p=(a=t.find(l=>l.source==="flow"&&l.flowId===i.id))==null?void 0:a.symbol)!=null?p:"",steps:i.steps.map(l=>({value:l.value,effects:l.cssclass.trim()?[{property:"cssclasses",value:l.cssclass.trim().split(/\s+/).join(", ")}]:[]}))})});for(let i of t)i.source==="open"&&e.push(v({name:i.property,symbol:i.symbol,property:i.property,kind:"open",valueMode:i.valueMode})),i.source==="finite"&&e.push(v({name:i.property,symbol:i.symbol,property:i.property,steps:i.values.map(a=>({value:a,effects:[]}))}));return{flows:e}}var u=require("obsidian");var E=class extends u.PluginSettingTab{constructor(t,e){super(t,e);this.plugin=e;this.expanded=new Set;this.save=()=>this.plugin.saveSettings()}async saveAndRedraw(){await this.save(),this.display()}display(){let{containerEl:t}=this,e=this.plugin.settings.flows;t.empty(),new u.Setting(t).setHeading().setName("Flows").setDesc("Type a flow's symbol at the start of a word to pLine truncated var P=Object.defineProperty;var ne=Object.getOwnPropertyDescriptor;var se=Object.getOwnPropertyNames;var oe=Object.prototype.hasOwnProperty;var ie=(s,n)=>{for(var t in n)P(s,t,{get:n[t],enumerable:!0})},re=(s,n,t,e)=>{if(n&&typeof n=="object"||typeof n=="function")for(let o of se(n))!oe.call(s,o)&&o!==t&&P(s,o,{get:()=>n[o],enumerable:!(e=ne(n,o))||e.enumerable});return s};var ae=s=>re(P({},"__esModule",{value:!0}),s);var ge={};ie(ge,{default:()=>L});module.exports=ae(ge);var Z=require("obsidian");var I=require("obsidian");var U=require("obsidian");var R=require("obsidian"),Y=String.raw`\{\{(date|time)(?::([^}]*))?\}\}`,le={date:"YYYY-MM-DD",time:"HH:mm"},K=(s,n)=>(n==null?void 0:n.trim())||le[s];function V(s){let n=(0,R.moment)();return s.replace(new RegExp(Y,"g"),(t,e,o)=>n.format(K(e,o)))}function _(s,n){let t=new RegExp(Y,"g"),e=[],o="^",a=0;for(let r=t.exec(s);r;r=t.exec(s))o+=j(s.slice(a,r.index))+"(.+?)",e.push(K(r[1],r[2])),a=r.index+r[0].length;if(e.length===0)return s===n;let i=new RegExp(o+j(s.slice(a))+"$").exec(n);return i!==null&&e.every((r,l)=>(0,R.moment)(i[l+1],r,!0).isValid())}function j(s){return s.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}async function x(s,n,t){let e=s.workspace.getActiveViewOfType(U.MarkdownView);(e==null?void 0:e.file)===n&&await e.save(),await s.fileManager.processFrontMatter(n,t)}function z(s,n,t,e){if(!e){s[n]=t;return}let o=O(s[n],n);o.includes(t)||o.push(t),s[n]=o}function b(s,n,t,e){var l;n[t.property]=e;let o=t.steps.find(p=>p.value===e),a=new Map,i=p=>{let d=a.get(p);return d===void 0&&a.set(p,d=pe(s,n,p)),d},r=p=>p.property!==""&&p.property!==t.property;for(let p of t.steps)if(p!==o)for(let d of p.effects.filter(r)){let c=d.property;if(i(c)){let h=W(d.value),g=O(n[c],c).filter(v=>!h.some(w=>q(w,v)));g.length?n[c]=g:delete n[c]}else c in n&&q(d.value,n[c])&&delete n[c]}for(let p of(l=o==null?void 0:o.effects.filter(r))!=null?l:[]){let d=p.property;if(i(d)){let c=O(n[d],d);for(let h of W(p.value).map(V))c.includes(h)||c.push(h);c.length&&(n[d]=c)}else n[d]=G(V(p.value))}}function pe(s,n,t){if(["cssclasses","tags","aliases"].includes(t)||Array.isArray(n[t]))return!0;let e=Reflect.get(s,"metadataTypeManager");if(!e||typeof e!="object"||!("getPropertyInfo"in e)||typeof e.getPropertyInfo!="function")return!1;let o=e.getPropertyInfo(t),a=o&&typeof o=="object"&&"widget"in o?o.widget:null;return a==="multitext"||a==="tags"||a==="aliases"}function O(s,n){return Array.isArray(s)?s.map(String):s==null||s===""?[]:n==="cssclasses"?String(s).split(/[\s,]+/).filter(Boolean):[String(s)]}function W(s){return s.split(/,(?![^{]*\}\})/).map(n=>n.trim()).filter(Boolean)}function q(s,n){let t=s.trim();return String(G(t))===String(n)||_(t,String(n))}function G(s){let n=s.trim();return n===""?null:n==="true"||n==="false"?n==="true":/^-?\d+(\.\d+)?$/.test(n)?Number(n):n}var C=class{constructor(n){this.plugin=n;this.registered=[]}sync(){for(let n of this.registered)this.plugin.removeCommand(n);this.registered=[];for(let n of this.plugin.settings.flows)n.kind==="ordered"&&(this.add(n,1,"next","next step"),this.add(n,-1,"previous","previous step"))}add(n,t,e,o){let a=`flow-${n.id}-${e}`;this.registered.push(a),this.plugin.addCommand({id:a,name:`${n.name||n.property||"Unnamed flow"}: ${o}`,checkCallback:i=>{let r=this.plugin.app.workspace.getActiveFile();return!r||r.extension!=="md"||!n.property||!n.steps.some(l=>l.value)?!1:(i||ce(this.plugin,r,n,t),!0)}})}};async function ce(s,n,t,e){let o=t.steps.filter(i=>i.value),a="";try{await x(s.app,n,i=>{let r=o.findIndex(p=>p.value===String(i[t.property])),l=r===-1?0:r+e;if(l<0||l>=o.length){if(!t.loop){a=`${t.property}: already at the ${e===1?"last":"first"} step`;return}l=(l+o.length)%o.length}b(s.app,i,t,o[l].value),a=`${t.property}: ${o[l].value}`}),new I.Notice(a)}catch(i){console.error("FFLOW: could not move flow step",i),new I.Notice(`FFLOW: could not set "${t.property}". See console.`)}}function y(s){return{id:Date.now().toString(36)+Math.random().toString(36).slice(2,6),name:"",symbol:"",property:"",kind:"ordered",valueMode:"single",loop:!1,steps:[],...s}}function ue(){return{flows:[y({name:"Type",symbol:"@",property:"type",kind:"open"})]}}var E=["#","[","/",":"];function S(s,n,t){return s===""?null:[...s].length!==1?"Use exactly one character.":/[\p{L}\p{N}\s]/u.test(s)?"Letters, digits and spaces are not allowed.":E.includes(s)?`"${s}" is reserved (${E.join(" ")}).`:n.some((e,o)=>o!==t&&e.symbol===s)?`"${s}" is already used by another flow.`:null}function J(s){var o,a;if(!s||typeof s!="object")return ue();if("flows"in s&&!("rules"in s)&&Array.isArray(s.flows))return{flows:s.flows.map(i=>y(i))};let n=s,t=(o=n.rules)!=null?o:[],e=((a=n.flows)!=null?a:[]).map(i=>{var r,l;return y({id:i.id,name:i.name,property:i.property,loop:i.loop,symbol:(l=(r=t.find(p=>p.source==="flow"&&p.flowId===i.id))==null?void 0:r.symbol)!=null?l:"",steps:i.steps.map(p=>({value:p.value,effects:p.cssclass.trim()?[{property:"cssclasses",value:p.cssclass.trim().split(/Line truncated
+1 -1
View File
@@ -1,7 +1,7 @@
{ {
"id": "fflow", "id": "fflow",
"name": "FFLOW", "name": "FFLOW",
"version": "0.3.1", "version": "0.4.0",
"minAppVersion": "1.11.0", "minAppVersion": "1.11.0",
"description": "Frontmatter Flow: type a trigger symbol (like @) to autocomplete and set a frontmatter property.", "description": "Frontmatter Flow: type a trigger symbol (like @) to autocomplete and set a frontmatter property.",
"author": "Olivier Legendre", "author": "Olivier Legendre",
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "fflow", "name": "fflow",
"version": "0.3.1", "version": "0.4.0",
"description": "Frontmatter Flow: set properties from the editor with trigger symbols.", "description": "Frontmatter Flow: set properties from the editor with trigger symbols.",
"main": "main.js", "main": "main.js",
"scripts": { "scripts": {
+17 -8
View File
@@ -1,5 +1,6 @@
import { App, MarkdownView, TFile } from "obsidian"; import { App, MarkdownView, TFile } from "obsidian";
import type { Flow, FlowEffect } from "./settings"; import type { Flow, FlowEffect } from "./settings";
import { matchesTemplate, renderTemplate } from "./templates";
export type Frontmatter = Record<string, unknown>; 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: * Puts the note on step `value` of an ordered flow:
* 1. sets the flow's property; * 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); * 2. undoes the effects of every other step (list items removed, scalars removed only if they still hold what the flow wrote —
* 3. applies the new step's effects (list items added, scalars replaced). * 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 { export function setFlowStep(app: App, fm: Frontmatter, flow: Flow, value: string): void {
fm[flow.property] = value; 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)) { for (const effect of step.effects.filter(usable)) {
const p = effect.property; const p = effect.property;
if (isList(p)) { if (isList(p)) {
const remove = new Set(listItems(effect.value)); const templates = listItems(effect.value);
const kept = toList(fm[p], p).filter((item) => !remove.has(item)); const kept = toList(fm[p], p).filter((item) => !templates.some((t) => effectMatches(t, item)));
if (kept.length) fm[p] = kept; if (kept.length) fm[p] = kept;
else delete fm[p]; 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]; delete fm[p];
} }
} }
@@ -59,10 +61,10 @@ export function setFlowStep(app: App, fm: Frontmatter, flow: Flow, value: string
const p = effect.property; const p = effect.property;
if (isList(p)) { if (isList(p)) {
const list = toList(fm[p], 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; if (list.length) fm[p] = list;
} else { } 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)]; 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[] { function listItems(value: string): string[] {
return value return value
.split(",") .split(/,(?![^{]*\}\})/)
.map((v) => v.trim()) .map((v) => v.trim())
.filter(Boolean); .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. */ /** "true"/"false" become booleans and plain numbers become numbers, so checkbox and number properties stay typed. */
function parseScalar(value: string): string | number | boolean | null { function parseScalar(value: string): string | number | boolean | null {
const v = value.trim(); const v = value.trim();
+4
View File
@@ -179,6 +179,10 @@ export class FflowSettingTab extends PluginSettingTab {
flow.steps.push({ value: "", effects: [] }); flow.steps.push({ value: "", effects: [] });
await this.saveAndRedraw(); 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 { private renderStep(parent: HTMLElement, flow: Flow, step: FlowStep, i: number, updateHeader: () => void): void {
+36
View File
@@ -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, "\\$&");
}