template-obsidian-vault/.obsidian/plugins/waypoint/main.js

24 lines
15 KiB
JavaScript
Raw Normal View History

/*
THIS IS A GENERATED/BUNDLED FILE BY ESBUILD
if you want to view the source, please visit the github repository of this plugin
*/
var I=Object.create;var P=Object.defineProperty;var T=Object.getOwnPropertyDescriptor;var A=Object.getOwnPropertyNames;var C=Object.getPrototypeOf,x=Object.prototype.hasOwnProperty;var S=h=>P(h,"__esModule",{value:!0});var L=(h,t)=>{S(h);for(var e in t)P(h,e,{get:t[e],enumerable:!0})},$=(h,t,e)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of A(t))!x.call(h,i)&&i!=="default"&&P(h,i,{get:()=>t[i],enumerable:!(e=T(t,i))||e.enumerable});return h},v=h=>$(S(P(h!=null?I(C(h)):{},"default",h&&h.__esModule&&"default"in h?{get:()=>h.default,enumerable:!0}:{value:h,enumerable:!0})),h);var l=(h,t,e)=>new Promise((i,s)=>{var n=g=>{try{p(e.next(g))}catch(c){s(c)}},o=g=>{try{p(e.throw(g))}catch(c){s(c)}},p=g=>g.done?i(g.value):Promise.resolve(g.value).then(n,o);p((e=e.apply(h,t)).next())});L(exports,{default:()=>y});var a=v(require("obsidian")),u;(function(e){e.InsideFolder="INSIDE_FOLDER",e.OutsideFolder="OUTSIDE_FOLDER"})(u||(u={}));var f;(function(e){e.Waypoint="waypoint",e.Landmark="landmark"})(f||(f={}));var N={waypointFlag:"%% Waypoint %%",landmarkFlag:"%% Landmark %%",stopScanAtFolderNotes:!1,showFolderNotes:!1,showNonMarkdownFiles:!1,debugLogging:!1,useWikiLinks:!0,useFrontMatterTitle:!1,showEnclosingNote:!1,folderNoteType:u.InsideFolder,ignorePaths:["_attachments"],useSpaces:!1,numSpaces:2},F=class extends a.Plugin{constructor(){super(...arguments);this.foldersWithChanges=new Set;this.detectFlags=t=>l(this,null,function*(){this.detectFlag(t,f.Waypoint),this.detectFlag(t,f.Landmark)});this.detectFlag=(t,e)=>l(this,null,function*(){this.log("Modification on "+t.name),this.log("Scanning for "+e+" flags...");let i=yield this.getWaypointFlag(e),n=(yield this.app.vault.cachedRead(t)).split(`
`);for(let o=0;o<n.length;o++)if(n[o].trim().includes(i))if(this.isFolderNote(t)){this.log("Found "+e+" flag in folder note!"),yield this.updateWaypoint(t,e),yield this.updateParentPoint(t.parent,this.settings.folderNoteType===u.OutsideFolder);return}else if(t.parent.isRoot()){this.log("Found "+e+" flag in root folder."),this.printError(t,"%% Error: Cannot create a "+e+" in the root folder of your vault. For more information, check the instructions [here](https://github.com/IdreesInc/Waypoint) %%",e);return}else{this.log("Found "+e+" flag in invalid note."),this.printError(t,"%% Error: Cannot create a "+e+" in a note that's not the folder note. For more information, check the instructions [here](https://github.com/IdreesInc/Waypoint) %%",e);return}this.log("No "+e+" flags found.")});this.updateChangedFolders=()=>l(this,null,function*(){this.log("Updating changed folders..."),this.foldersWithChanges.forEach(t=>{this.log("Updating "+t.path),this.updateParentPoint(t,!0)}),this.foldersWithChanges.clear()});this.scheduleUpdate=(0,a.debounce)(this.updateChangedFolders.bind(this),500,!0);this.updateParentPoint=(t,e)=>l(this,null,function*(){let[i,s]=yield this.locateParentPoint(t,e);s!==null&&(this.updateWaypoint(s,i),this.updateParentPoint(s.parent,!1))})}onload(){return l(this,null,function*(){yield this.loadSettings(),this.addCommand({id:"go_to_parent_waypoint",name:"Go to parent Waypoint",callback:()=>l(this,null,function*(){let t=this.app.workspace.getActiveFile(),[,e]=yield this.locateParentPoint(t,!1);this.app.workspace.activeLeaf.openFile(e)})}),this.app.workspace.onLayoutReady(()=>l(this,null,function*(){this.registerEvent(this.app.vault.on("create",t=>{this.log("create "+t.name),this.foldersWithChanges.add(t.parent),this.scheduleUpdate()})),this.registerEvent(this.app.vault.on("delete",t=>{this.log("delete "+t.name);let e=this.getParentFolder(t.path);e!==null&&(this.foldersWithChanges.add(e),this.scheduleUpdate())})),this.registerEvent(this.app.vault.on("rename",(t,e)=>{this.log("rename "+t.name),this.foldersWithChanges.add(t.parent);let i=this.getParentFolder(e);i!==null&&this.foldersWithChanges.add(i),this.scheduleUpdate()})),this.registerEvent(this.app.vault.on("modify",this.detectFlags))})),this.addSettingTab(new W(this.app,this))})}onunload(){}isFolderNote(t){return this.settings.folderNoteType===u.InsideFolder?t.basename==t.parent.name:t.parent?this.app.vault.getAbstractFileByPath(this.getCleanParentPath(t)+t.basename)instanceof a.TFolder:!1}getCleanParentPath(t){return t.parent instanceof a.TFolder&&t.parent.isRoot()?"":t.parent.path+"/"}printError(t,e,i){return l(this,null,function*(){this.log("Creating "+i+" error in "+t.path);let n=(yield this.app.vault.read(t)).split(`
`),o=-1,p=yield this.getWaypointFlag(i);for(let g=0;g<n.length;g++)n[g].trim().includes(p)&&(o=g);if(o===-1){console.error("Error: No "+i+" flag found while trying to print error.");return}n.splice(o,1,e),yield this.app.vault.modify(t,n.join(`
`))})}getWaypointBounds(t){return l(this,null,function*(){return t===f.Waypoint?[F.BEGIN_WAYPOINT,F.END_WAYPOINT]:t===f.Landmark?[F.BEGIN_LANDMARK,F.END_LANDMARK]:[null,null]})}getWaypointFlag(t){return l(this,null,function*(){return t===f.Waypoint?this.settings.waypointFlag:t===f.Landmark?this.settings.landmarkFlag:(console.error("Error: Invalid waypoint type: "+t),null)})}updateWaypoint(t,e){return l(this,null,function*(){this.log("Updating "+e+" in "+t.path);let i;if(this.settings.folderNoteType===u.InsideFolder)i=yield this.getFileTreeRepresentation(t.parent,t.parent,0,!0);else{let w=this.app.vault.getAbstractFileByPath(this.getCleanParentPath(t)+t.basename);w instanceof a.TFolder&&(i=yield this.getFileTreeRepresentation(t.parent,w,0,!0))}let[s,n]=yield this.getWaypointBounds(e),o=`${s}
${i}
${n}`;if(s===null||n===null){console.error("Error: Waypoint bounds not found, unable to continue.");return}let p=yield this.getWaypointFlag(e),c=(yield this.app.vault.read(t)).split(`
`),m=-1,r=-1,d,E=!1;for(let w=0;w<c.length;w++){let b=c[w].trim();if(m===-1&&(b.includes(p)||b.includes(s))){d=b.startsWith(">"),E=b.includes(p),m=w;continue}if(m!==-1&&b===n){r=w;break}}if(m===-1){console.error("Error: No "+e+" found while trying to update "+t.path);return}this.log(e+" found at "+m+" to "+r),d&&(E&&(o=(e===f.Landmark?`[!landmark]
`:`[!waypoint]
`)+o),o=o.split(`
`).map(k=>`>${k}`).join(`
`)),c.splice(m,r!==-1?r-m+1:1,o),yield this.app.vault.modify(t,c.join(`
`))})}getFileTreeRepresentation(t,e,i,s=!1){return l(this,null,function*(){var c,m;let o=(this.settings.useSpaces?" ".repeat(this.settings.numSpaces):" ").repeat(i)+"-";if(!(e instanceof a.TFile)&&!(e instanceof a.TFolder)||(this.log(e.path),this.ignorePath(e.path)))return null;if(e instanceof a.TFile){this.settings.debugLogging&&console.log(e);let r;if(this.settings.useFrontMatterTitle){let d=(m=(c=this.app.metadataCache)==null?void 0:c.getFileCache(e))==null?void 0:m.frontmatter;d&&d.hasOwnProperty("title")&&(r=d.title)}else r=null;return e.extension=="md"?this.settings.useWikiLinks?r?`${o} [[${e.basename}|${r}]]`:`${o} [[${e.basename}]]`:r?`${o} [${r}](${this.getEncodedUri(t,e)})`:`${o} [${e.basename}](${this.getEncodedUri(t,e)})`:this.settings.showNonMarkdownFiles?this.settings.useWikiLinks?`${o} [[${e.name}]]`:`${o} [${e.name}](${this.getEncodedUri(t,e)})`:null}let p="";if(!s||this.settings.showEnclosingNote){p=`${o} **${e.name}**`;let r;if(this.settings.folderNoteType===u.InsideFolder?r=this.app.vault.getAbstractFileByPath(e.path+"/"+e.name+".md"):e.parent&&(r=this.app.vault.getAbstractFileByPath(e.parent.path+"/"+e.name+".md")),r instanceof a.TFile&&(this.settings.useWikiLinks?p=`${o} **[[${r.basename}]]**`:p=`${o} **[${r.basename}](${this.getEncodedUri(t,r)})**`,!s)){if(this.settings.stopScanAtFolderNotes)return p;let d=yield this.app.vault.cachedRead(r);if(d.includes(F.BEGIN_WAYPOINT)||d.includes(this.settings.waypointFlag))return p}}if(!e.children||e.children.length==0)return`${o} **${e.name}**`;let g=e.children;if(g=g.sort((r,d)=>r.name.localeCompare(d.name,void 0,{numeric:!0,sensitivity:"base"})),!this.settings.showFolderNotes)if(this.settings.folderNoteType===u.InsideFolder)g=g.filter(r=>(this.settings.showFolderNotes||r.name!==e.name+".md")&&!this.ignorePath(r.path));else{let r=new Set;for(let d of g)d instanceof a.TFolder&&r.add(d.name+".md");g=g.filter(d=>(d instanceof a.TFolder||!r.has(d.name))&&!this.ignorePath(d.path))}if(g.length>0){let r=s&&!this.settings.showEnclosingNote?i:i+1;p+=(p===""?"":`
`)+(yield Promise.all(g.map(d=>this.getFileTreeRepresentation(t,d,r)))).filter(Boolean).join(`
`)}return p})}getEncodedUri(t,e){return t.isRoot()?`./${encodeURI(e.path)}`:`./${encodeURI(e.path.substring(t.path.length+1))}`}ignorePath(t){let e=!1;return this.settings.ignorePaths.forEach(i=>{if(i==="")return;let s=new RegExp(i);t.match(s)&&(this.log(`Ignoring path: ${t}`),e=!0)}),!!e}locateParentPoint(t,e){return l(this,null,function*(){this.log("Locating parent flag and file of "+t.name);let i=e?t:t.parent;for(;i;){let s;if(this.settings.folderNoteType===u.InsideFolder?s=this.app.vault.getAbstractFileByPath(i.path+"/"+i.name+".md"):i.parent&&(s=this.app.vault.getAbstractFileByPath(this.getCleanParentPath(i)+i.name+".md")),s instanceof a.TFile){this.log("Found folder note: "+s.path);let n=yield this.app.vault.cachedRead(s);if(n.includes(F.BEGIN_WAYPOINT)||n.includes(this.settings.waypointFlag))return this.log("Found parent waypoint!"),[f.Waypoint,s];if(n.includes(F.BEGIN_LANDMARK)||n.includes(this.settings.landmarkFlag))return this.log("Found parent landmark!"),[f.Landmark,s]}i=i.parent}return this.log("No parent flag found."),[null,null]})}getParentFolder(t){let e=this.app.vault.getAbstractFileByPath(t.split("/").slice(0,-1).join("/"));return e instanceof a.TFolder?e:null}log(t){this.settings.debugLogging&&console.log(t)}loadSettings(){return l(this,null,function*(){this.settings=Object.assign({},N,yield this.loadData())})}saveSettings(){return l(this,null,function*(){yield this.saveData(this.settings)})}},y=F;y.BEGIN_WAYPOINT="%% Begin Waypoint %%",y.END_WAYPOINT="%% End Waypoint %%",y.BEGIN_LANDMARK="%% Begin Landmark %%",y.END_LANDMARK="%% End Landmark %%";var W=class extends a.PluginSettingTab{constructor(t,e){super(t,e);this.plugin=e}display(){let{containerEl:t}=this;t.empty(),t.createEl("h2",{text:"Waypoint Settings"}),new a.Setting(this.containerEl).setName("Folder Note Style").setDesc("Select the style of folder note used.").addDropdown(s=>s.addOption(u.InsideFolder,"Folder Name Inside").addOption(u.OutsideFolder,"Folder Name Outside").setValue(this.plugin.settings.folderNoteType).onChange(n=>l(this,null,function*(){this.plugin.settings.folderNoteType=n,yield this.plugin.saveSettings()}))),new a.Setting(t).setName("Show Folder Notes").setDesc("If enabled, folder notes will be listed alongside other notes in the generated waypoints.").addToggle(s=>s.setValue(this.plugin.settings.showFolderNotes).onChange(n=>l(this,null,function*(){this.plugin.settings.showFolderNotes=n,yield this.plugin.saveSettings()}))),new a.Setting(t).setName("Show Non-Markdown Files").setDesc("If enabled, non-Markdown files will be listed alongside other notes in the generated waypoints.").addToggle(s=>s.setValue(this.plugin.settings.showNonMarkdownFiles).onChange(n=>l(this,null,function*(){this.plugin.settings.showNonMarkdownFiles=n,yield this.plugin.saveSettings()}))),new a.Setting(t).setName("Show Enclosing Note").setDesc("If enabled, the name of the folder note containing the waypoint will be listed at the top of the generated waypoints.").addToggle(s=>s.setValue(this.plugin.settings.showEnclosingNote).onChange(n=>l(this,null,function*(){this.plugin.settings.showEnclosingNote=n,yield this.plugin.saveSettings()}))),new a.Setting(t).setName("Stop Scan at Folder Notes").setDesc("If enabled, the waypoint generator will stop scanning nested folders when it encounters a folder note. Otherwise, it will only stop if the folder note contains a waypoint.").addToggle(s=>s.setValue(this.plugin.settings.stopScanAtFolderNotes).onChange(n=>l(this,null,function*(){this.plugin.settings.stopScanAtFolderNotes=n,yield this.plugin.saveSettings()}))),new a.Setting(t).setName("Use WikiLinks").setDesc("If enabled, links will be generated like [[My Page]] instead of [My Page](../Folder/My%Page.md).").addToggle(s=>s.setValue(this.plugin.settings.useWikiLinks).onChange(n=>l(this,null,function*(){this.plugin.settings.useWikiLinks=n,yield this.plugin.saveSettings()}))),new a.Setting(t).setName("Use Title Property").setDesc('If enabled, links will use the "title" frontmatter property for the displayed text (if it exists).').addToggle(s=>s.setValue(this.plugin.s
`)).setValue(this.plugin.settings.ignorePaths.join(`
`)).onChange(n=>l(this,null,function*(){let o=n.trim().split(`
`).map(p=>this.getNormalizedPath(p));this.plugin.settings.ignorePaths=o,yield this.plugin.saveSettings()})));let e=t.createEl("div",{cls:"setting-item"}),i=e.createDiv({cls:"setting-item-description"});i.createSpan({text:"For instructions on how to use this plugin, check out the README on "}),i.createEl("a",{attr:{href:"https://github.com/IdreesInc/Waypoint"},text:"GitHub"}),i.createSpan({text:" or get in touch with the author "}),i.createEl("a",{attr:{href:"https://github.com/IdreesInc"},text:"@IdreesInc"}),e.appendChild(i)}getNormalizedPath(t){return t.length==0?t:(0,a.normalizePath)(t)}};