Files
cours/.obsidian/plugins/default-template/main.js
T

8 lines
5.5 KiB
JavaScript

/*
THIS IS A GENERATED/BUNDLED FILE BY ESBUILD
if you want to view the source, please visit the github repository of this plugin
*/
var m=Object.defineProperty;var T=Object.getOwnPropertyDescriptor;var w=Object.getOwnPropertyNames;var P=Object.prototype.hasOwnProperty;var v=(d,r)=>{for(var e in r)m(d,e,{get:r[e],enumerable:!0})},y=(d,r,e,o)=>{if(r&&typeof r=="object"||typeof r=="function")for(let i of w(r))!P.call(d,i)&&i!==e&&m(d,i,{get:()=>r[i],enumerable:!(o=T(r,i))||o.enumerable});return d};var F=d=>y(m({},"__esModule",{value:!0}),d);var C={};v(C,{default:()=>c});module.exports=F(C);var t=require("obsidian"),S={defaultTemplate:"",folderTemplates:{},ignorePaths:[]},c=class extends t.Plugin{async onload(){await this.loadSettings(),this.app.workspace.onLayoutReady(()=>{this.registerEvent(this.app.vault.on("create",async e=>{if(!(e instanceof t.TFile)||e.extension!=="md")return;let o=(0,t.normalizePath)(e.path);if(this.settings.ignorePaths.some(n=>{let s=(0,t.normalizePath)(n);return o===s||o.startsWith(s+"/")}))return;let a=(n=>{let s=n.split("/").slice(0,-1);for(;s.length>0;){let h=s.join("/"),p=this.settings.folderTemplates[h];if(p){let l=this.app.vault.getAbstractFileByPath(p);if(l instanceof t.TFile)return l}s.pop()}if(this.settings.defaultTemplate){let h=this.app.vault.getAbstractFileByPath(this.settings.defaultTemplate);if(h instanceof t.TFile)return h}})(e.path);if(a)try{let n=await this.app.vault.read(a);await this.app.vault.process(e,s=>s.trim().length!==0?s:n.replace(/\{\{date(?::([^}]+))?\}\}/g,(p,l)=>l?(0,t.moment)().format(l):(0,t.moment)().format("YYYY-MM-DD")).replace(/\{\{time(?::([^}]+))?\}\}/g,(p,l)=>l?(0,t.moment)().format(l):(0,t.moment)().format("HH:mm")).replace(/\{\{title\}\}/g,e.basename))}catch(n){new t.Notice(`Default Template: Template file "${a.path}" not found or cannot be read.`)}}))}),this.addSettingTab(new f(this.app,this))}onunload(){}async loadSettings(){this.settings=Object.assign({},S,await this.loadData())}async saveSettings(){await this.saveData(this.settings)}},u=class extends t.AbstractInputSuggest{constructor(e,o,i){super(e,o);this.inputEl=o;this.getSuggestionsFn=i}getSuggestions(e){return this.getSuggestionsFn(this.app.vault,e.toLowerCase())}renderSuggestion(e,o){o.createDiv({text:e.path})}selectSuggestion(e){this.inputEl.value=e.path,this.inputEl.trigger("input"),this.close()}},f=class extends t.PluginSettingTab{constructor(e,o){super(e,o);this.plugin=o}display(){let{containerEl:e}=this;e.empty(),new t.Setting(e).setName("Default template file").setDesc("Select a template file to apply to new empty notes").addText(i=>{i.setPlaceholder("path/to/template.md").setValue(this.plugin.settings.defaultTemplate).onChange(async g=>{this.plugin.settings.defaultTemplate=(0,t.normalizePath)(g),await this.plugin.saveSettings()}),new u(this.app,i.inputEl,(g,a)=>g.getMarkdownFiles().filter(n=>n.path.toLowerCase().includes(a)))}),new t.Setting(e).setName("Folder templates").setHeading(),e.createEl("p",{text:"Override the default template for specific folders",cls:"setting-item-description"});let o=Object.entries(this.plugin.settings.folderTemplates);for(let[i,g]of o){let a=i;new t.Setting(e).setName(`Folder: ${i}`).addText(n=>{n.setPlaceholder("Folder/path").setValue(i).onChange(async h=>{let p=(0,t.normalizePath)(h);p!==a&&(delete this.plugin.settings.folderTemplates[a],p&&(this.plugin.settings.folderTemplates[p]=g),a=p,await this.plugin.saveSettings())});let s=Object.keys(this.plugin.settings.folderTemplates);new u(this.app,n.inputEl,(h,p)=>h.getAllLoadedFiles().filter(l=>l instanceof t.TFolder).filter(l=>!s.includes(l.path)||l.path===i).filter(l=>l.path.toLowerCase().includes(p)))}).addText(n=>{n.setPlaceholder("path/to/template.md").setValue(g).onChange(async s=>{this.plugin.settings.folderTemplates[a]=(0,t.normalizePath)(s),await this.plugin.saveSettings()}),new u(this.app,n.inputEl,(s,h)=>s.getMarkdownFiles().filter(p=>p.path.toLowerCase().includes(h)))}).addExtraButton(n=>n.setIcon("trash").setTooltip("Delete folder template").onClick(async()=>{delete this.plugin.settings.folderTemplates[i],await this.plugin.saveSettings(),this.display()}))}new t.Setting(e).addButton(i=>i.setButtonText("Add folder template").setCta().onClick(async()=>{this.plugin.settings.folderTemplates[""]="",await this.plugin.saveSettings(),this.display()})),new t.Setting(e).setName("Ignore paths").setHeading(),e.createEl("p",{text:"Folders where templates will not be applied",cls:"setting-item-description"});for(let i of this.plugin.settings.ignorePaths)new t.Setting(e).setName(i||"").addText(g=>{g.setPlaceholder("Folder/path").setValue(i).onChange(async a=>{let n=(0,t.normalizePath)(a),s=this.plugin.settings.ignorePaths.indexOf(i);n?!this.plugin.settings.ignorePaths.includes(n)||this.plugin.settings.ignorePaths[s]===n?(this.plugin.settings.ignorePaths[s]=n,await this.plugin.saveSettings()):new t.Notice("Path already ignored"):(this.plugin.settings.ignorePaths.splice(s,1),await this.plugin.saveSettings(),this.display())}),new u(this.app,g.inputEl,(a,n)=>a.getAllLoadedFiles().filter(s=>s instanceof t.TFolder).filter(s=>s.path&&s.path!=="/").filter(s=>s.path.toLowerCase().includes(n)))}).addExtraButton(g=>g.setIcon("trash").setTooltip("Remove").onClick(async()=>{let a=this.plugin.settings.ignorePaths.indexOf(i);this.plugin.settings.ignorePaths.splice(a,1),await this.plugin.saveSettings(),this.display()}));new t.Setting(e).addButton(i=>i.setButtonText("Add ignore path").setCta().onClick(async()=>{this.plugin.settings.ignorePaths.includes("")||(this.plugin.settings.ignorePaths.push(""),await this.plugin.saveSettings(),this.display())}))}};
/* nosourcemap */