FFLOW 0.1.0: trigger symbols set frontmatter properties

This commit is contained in:
olivier committed 2026-10-25 16:02:10 -04:00
commit 021777203b
15 files changed
+1189

No files matched your search

+7
View File
@@ -0,0 +1,7 @@
/*
THIS IS A GENERATED/BUNDLED FILE BY ESBUILD
*/
var x=Object.defineProperty;var N=Object.getOwnPropertyDescriptor;var R=Object.getOwnPropertyNames;var A=Object.prototype.hasOwnProperty;var D=(n,s)=>{for(var e in s)x(n,e,{get:s[e],enumerable:!0})},P=(n,s,e,t)=>{if(s&&typeof s=="object"||typeof s=="function")for(let i of R(s))!A.call(n,i)&&i!==e&&x(n,i,{get:()=>s[i],enumerable:!(t=N(s,i))||t.enumerable});return n};var V=n=>P(x({},"__esModule",{value:!0}),n);var B={};D(B,{default:()=>S});module.exports=V(B);var F=require("obsidian");var T={rules:[{symbol:"@",property:"type",valueMode:"single",source:"open",values:[]}]},h=["#","[","/",":"];function m(n,s,e){return[...n].length!==1?"Use exactly one character.":/[\p{L}\p{N}\s]/u.test(n)?"Letters, digits and spaces are not allowed.":h.includes(n)?`"${n}" is reserved (${h.join(" ")}).`:s.some((t,i)=>i!==e&&t.symbol===n)?`"${n}" is already used by another rule.`:null}function C(n,s,e){return n.property.trim()!==""&&m(n.symbol,s,e)===null}var c=require("obsidian");var y=class extends c.PluginSettingTab{constructor(e,t){super(e,t);this.plugin=t}display(){let{containerEl:e}=this;e.empty(),e.createEl("p",{cls:"setting-item-description",text:`Type a trigger symbol at the start of a word to pick a value. The typed text is removed and the value is written to the note's frontmatter. Reserved symbols: ${h.join(" ")}`}),this.plugin.settings.rules.forEach((t,i)=>this.renderRule(t,i)),new c.Setting(e).addButton(t=>t.setButtonText("Add trigger").setCta().onClick(async()=>{this.plugin.settings.rules.push({symbol:"",property:"",valueMode:"single",source:"open",values:[]}),await this.plugin.saveSettings(),this.display()}))}renderRule(e,t){let i=this.plugin.settings.rules,r=()=>this.plugin.saveSettings(),o=this.containerEl.createDiv({cls:"fflow-rule"});new c.Setting(o).setHeading().setName(e.symbol&&e.property?`${e.symbol} \u2192 ${e.property}`:"New trigger").addExtraButton(l=>l.setIcon("trash").setTooltip("Delete trigger").onClick(async()=>{i.splice(t,1),await r(),this.display()}));let a=new c.Setting(o).setName("Symbol").setDesc("One character, not a letter or digit."),g=a.descEl.createDiv({cls:"fflow-error"}),p=l=>{var u;return g.setText((u=m(l,i,t))!=null?u:"")};a.addText(l=>{l.setPlaceholder("@").setValue(e.symbol),l.inputEl.addClass("fflow-symbol"),l.onChange(async u=>{p(u),!m(u,i,t)&&(e.symbol=u,await r())})}),e.symbol&&p(e.symbol),new c.Setting(o).setName("Property").setDesc("Frontmatter property to set.").addText(l=>l.setPlaceholder("type").setValue(e.property).onChange(async u=>{e.property=u.trim(),await r()})),new c.Setting(o).setName("Value mode").setDesc("Single replaces the current value. List adds to the existing values.").addDropdown(l=>l.addOptions({single:"Single value",list:"List"}).setValue(e.valueMode).onChange(async u=>{e.valueMode=u,await r()})),new c.Setting(o).setName("Suggestions").setDesc("Open: every value already used in the vault; new values are allowed. Finite: only the values listed below.").addDropdown(l=>l.addOptions({open:"Open (from vault)",finite:"Finite list"}).setValue(e.source).onChange(async u=>{e.source=u,await r(),this.display()})),e.source==="finite"&&new c.Setting(o).setName("Values").setDesc("One value per line, in the order they should appear.").addTextArea(l=>{l.setValue(e.values.join(`
`)).onChange(async u=>{e.values=u.split(`
`).map(d=>d.trim()).filter(Boolean),await r()}),l.inputEl.rows=6})}};var f=require("obsidian");var v=class{constructor(s){this.app=s;this.cache=new Map}invalidate(){this.cache.clear()}get(s){var t,i,r;let e=this.cache.get(s);if(e)return e;e=new Map;for(let o of this.app.vault.getMarkdownFiles()){let a=(i=(t=this.app.metadataCache.getFileCache(o))==null?void 0:t.frontmatter)==null?void 0:i[s];for(let g of new Set(M(a)))e.set(g,((r=e.get(g))!=null?r:0)+1)}return this.cache.set(s,e),e}};function M(n){return Array.isArray(n)?n.flatMap(M):typeof n=="string"?n.trim()?[n.trim()]:[]:typeof n=="number"||typeof n=="boolean"?[String(n)]:[]}var E=n=>n!==void 0&&/\s/.test(n),w=class extends f.EditorSuggest{constructor(e,t,i){super(e);this.plugin=t;this.index=i;this.rule=null}onTrigger(e,t,i){let r=t.getLine(e.line).slice(0,e.ch),o=r.length;for(;o>0&&!E(r[o-1]);)o--;let a=r.slice(o);if(!a)return null;let g=this.plugin.settings.rules,p=g.find((l,u)=>l.symbol===a[0]&&C(l,g,u));return!p||O(t,e.line)||k(t,e.line,r.slice(0,o))?null:(this.rule=p,{start:{line:e.line,ch:o},end:e,query:a.slice(1)})}getSuggestions(e){let t=this.rule;if(!t)return[];let i=e.query.toLowerCase(),r=t.source==="finite"?t.values.map(a=>({value:a,isNew:!1})):[...this.index.get(t.property)].sort((a,g)=>g[1]-a[1]||a[0].localeCompare(g[0])).map(([a,g])=>({value:a,count:g,isNew:!1})),o=r.filter(a=>a.value.toLowerCase().includes(i));return o.sort((a,g)=>+!a.value.toLowerCase().startsWith(i)-+!g.value.toLowerCase().startsWith(i)),t.source==="open"&&i&&!r.some(a=>a.value.toLowerCase()===i)&&o.push({value:e.query,isNew:!0}),o}renderSuggestion(e,t){if(t.addClass("fflow-suggestion"),e.isNew){t.createSpan({cls:"fflow-new",text:"New: "}),t.createSpan({text:e.value});return}t.createSpan({text:e.value}),e.count!==void 0&&t.createSpan({cls:"fflow-count",text:String(e.count)})}selectSuggestion(e,t){let i=this.context,r=this.rule;!i||!r||this.apply(i,r,e.value)}async apply(e,t,i){let{editor:r,file:o,start:a}=e,g=r.getLine(a.line),p=e.end.ch;for(;p<g.length&&!E(g[p]);)p++;E(g[p])&&p++,r.replaceRange("",a,{line:a.line,ch:p}),r.setCursor(a);try{let l=this.app.workspace.getActiveViewOfType(f.MarkdownView);(l==null?void 0:l.file)===o&&await l.save(),await this.app.fileManager.processFrontMatter(o,u=>{if(t.valueMode==="single"){u[t.property]=i;return}let d=u[t.property],b=d==null?[]:Array.isArray(d)?d:[d];b.some(L=>String(L)===i)||b.push(i),u[t.property]=b})}catch(l){console.error("FFLOW: could not update frontmatter",l),new f.Notice(`FFLOW: could not set "${t.property}". See console.`)}}};function O(n,s){if(n.getLine(0)!=="---")return!1;for(let e=1;e<n.lineCount();e++)if(n.getLine(e)==="---")return s<=e;return!1}function k(n,s,e){var i,r;let t=0;for(let o=0;o<s;o++)/^\s*(```|~~~)/.test(n.getLine(o))&&t++;return t%2===1?!0:((r=(i=e.match(/`/g))==null?void 0:i.length)!=null?r:0)%2===1}var S=class extends F.Plugin{async onload(){await this.loadSettings();let s=new v(this.app);this.registerEvent(this.app.metadataCache.on("changed",()=>s.invalidate())),this.registerEvent(this.app.metadataCache.on("deleted",()=>s.invalidate())),this.registerEditorSuggest(new w(this.app,this,s)),this.addSettingTab(new y(this.app,this))}async loadSettings(){let s=await this.loadData();this.settings={...T,...s}}async saveSettings(){await this.saveData(this.settings)}};