25 lines
17 KiB
JavaScript
25 lines
17 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 I=Object.create;var P=Object.defineProperty;var W=Object.getOwnPropertyDescriptor;var C=Object.getOwnPropertyNames;var A=Object.getPrototypeOf,$=Object.prototype.hasOwnProperty;var k=p=>P(p,"__esModule",{value:!0});var x=(p,t)=>{k(p);for(var e in t)P(p,e,{get:t[e],enumerable:!0})},L=(p,t,e)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of C(t))!$.call(p,i)&&i!=="default"&&P(p,i,{get:()=>t[i],enumerable:!(e=W(t,i))||e.enumerable});return p},v=p=>L(k(P(p!=null?I(A(p)):{},"default",p&&p.__esModule&&"default"in p?{get:()=>p.default,enumerable:!0}:{value:p,enumerable:!0})),p);var l=(p,t,e)=>new Promise((i,n)=>{var s=h=>{try{d(e.next(h))}catch(o){n(o)}},r=h=>{try{d(e.throw(h))}catch(o){n(o)}},d=h=>h.done?i(h.value):Promise.resolve(h.value).then(s,r);d((e=e.apply(p,t)).next())});x(exports,{default:()=>w});var a=v(require("obsidian")),u;(function(i){i.InsideFolder="INSIDE_FOLDER",i.OutsideFolder="OUTSIDE_FOLDER",i.CustomFilename="CUSTOM_FILENAME"})(u||(u={}));var f;(function(e){e.Waypoint="waypoint",e.Landmark="landmark"})(f||(f={}));var b={waypointFlag:"%% Waypoint %%",landmarkFlag:"%% Landmark %%",stopScanAtFolderNotes:!1,showFolderNotes:!1,showNonMarkdownFiles:!1,foldersOnTop:!0,debugLogging:!1,useWikiLinks:!0,useFrontMatterTitle:!1,showEnclosingNote:!1,folderNoteType:u.InsideFolder,folderNoteFilename:"index",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*(){if(t&&this.ignorePath(t.path))return;this.log("Modification on "+t.name),this.log("Scanning for "+e+" flags...");let i=yield this.getWaypointFlag(e),s=(yield this.app.vault.cachedRead(t)).split(`
|
|
`);for(let r=0;r<s.length;r++)if(s[r].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,n]=yield this.locateParentPoint(t,e);n!==null&&(this.updateWaypoint(n,i),this.updateParentPoint(n.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 e=this.app.workspace.getActiveFile().parent;this.settings.folderNoteType===u.InsideFolder&&(e=e==null?void 0:e.parent);let[,i]=yield this.locateParentPoint(e,u.InsideFolder===this.settings.folderNoteType);this.app.workspace.activeLeaf.openFile(i)})}),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 S(this.app,this))})}onunload(){}isFolderNote(t){return this.settings.folderNoteType===u.InsideFolder?t.basename==t.parent.name:this.settings.folderNoteType===u.OutsideFolder?t.parent?this.app.vault.getAbstractFileByPath(this.getCleanParentPath(t)+t.basename)instanceof a.TFolder:!1:this.settings.folderNoteType===u.CustomFilename?t.basename==this.settings.folderNoteFilename:!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 s=(yield this.app.vault.read(t)).split(`
|
|
`),r=-1,d=yield this.getWaypointFlag(i);for(let h=0;h<s.length;h++)s[h].trim().includes(d)&&(r=h);if(r===-1){console.error("Error: No "+i+" flag found while trying to print error.");return}s.splice(r,1,e),yield this.app.vault.modify(t,s.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||this.settings.folderNoteType===u.CustomFilename)i=yield this.getFileTreeRepresentation(t.parent,t.parent,0,!0);else{let m=this.app.vault.getAbstractFileByPath(this.getCleanParentPath(t)+t.basename);m instanceof a.TFolder&&(i=yield this.getFileTreeRepresentation(t.parent,m,0,!0))}let[n,s]=yield this.getWaypointBounds(e),r=`${n}
|
|
${i}
|
|
|
|
${s}`;if(n===null||s===null){console.error("Error: Waypoint bounds not found, unable to continue.");return}let d=yield this.getWaypointFlag(e),o=(yield this.app.vault.read(t)).split(`
|
|
`),g=-1,c=-1,N,T=!1;for(let m=0;m<o.length;m++){let y=o[m].trim();if(g===-1&&(y.includes(d)||y.includes(n))){N=y.startsWith(">"),T=y.includes(d),g=m;continue}if(g!==-1&&y.includes(s)){c=m;break}}if(g===-1){console.error("Error: No "+e+" found while trying to update "+t.path);return}this.log(e+" found at "+g+" to "+c),N&&(T&&(r=(e===f.Landmark?`[!landmark]
|
|
`:`[!waypoint]
|
|
`)+r),r=r.split(`
|
|
`).map(E=>`>${E}`).join(`
|
|
`)),o.splice(g,c!==-1?c-g+1:1,r),yield this.app.vault.modify(t,o.join(`
|
|
`))})}getTitleFromFrontMatter(t){var e,i;if(this.settings.useFrontMatterTitle){let n=(i=(e=this.app.metadataCache)==null?void 0:e.getFileCache(t))==null?void 0:i.frontmatter;if((n==null?void 0:n.hasOwnProperty("title"))&&typeof n.title=="string")return this.log(`Found frontmatter title for ${t.path}: ${n.title}`),n.title}}getFileTreeRepresentation(t,e,i,n=!1){return l(this,null,function*(){let r=(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 o=this.getTitleFromFrontMatter(e);if(e.extension=="md"||e.extension=="base"){let g=e.extension==="base";if(this.settings.useWikiLinks){let c=g?e.basename+".base":e.basename,N=g&&!o?e.basename:o;return N?`${r} [[${c}|${N}]]`:`${r} [[${c}]]`}return o?`${r} [${o}](${this.getEncodedUri(t,e)})`:`${r} [${e.basename}](${this.getEncodedUri(t,e)})`}return this.settings.showNonMarkdownFiles?this.settings.useWikiLinks?`${r} [[${e.name}]]`:`${r} [${e.name}](${this.getEncodedUri(t,e)})`:null}let d="";if(!n||this.settings.showEnclosingNote){d=`${r} **${e.name}**`;let o;if(this.settings.folderNoteType===u.InsideFolder?o=this.app.vault.getAbstractFileByPath(e.path+"/"+e.name+".md"):e.parent&&(o=this.app.vault.getAbstractFileByPath(e.parent.path+"/"+e.name+".md")),o instanceof a.TFile){let g=this.getTitleFromFrontMatter(o);if(this.settings.useWikiLinks?g?d=`${r} **[[${o.basename}|${g}]]**`:d=`${r} **[[${o.basename}]]**`:g?d=`${r} **[${g}](${this.getEncodedUri(t,o)})**`:d=`${r} **[${o.basename}](${this.getEncodedUri(t,o)})**`,!n){if(this.settings.stopScanAtFolderNotes)return d;let c=yield this.app.vault.cachedRead(o);if(c.includes(F.BEGIN_WAYPOINT)||c.includes(this.settings.waypointFlag))return d}}}if(!e.children||e.children.length==0)return`${r} **${e.name}**`;let h=e.children;if(h=h.sort((o,g)=>o.name.localeCompare(g.name,void 0,{numeric:!0,sensitivity:"base"})),!this.settings.showFolderNotes)if(this.settings.folderNoteType===u.InsideFolder)h=h.filter(o=>(this.settings.showFolderNotes||o.name!==e.name+".md")&&!this.ignorePath(o.path));else{let o=new Set;for(let g of h)g instanceof a.TFolder&&o.add(g.name+".md");h=h.filter(g=>(g instanceof a.TFolder||!o.has(g.name))&&!this.ignorePath(g.path))}if(h.length>0){let o=n&&!this.settings.showEnclosingNote?i:i+1;this.settings.foldersOnTop&&h.sort((g,c)=>g instanceof a.TFolder&&!(c instanceof a.TFolder)?-1:1),d+=(d===""?"":`
|
|
`)+(yield Promise.all(h.map(g=>this.getFileTreeRepresentation(t,g,o)))).filter(Boolean).join(`
|
|
`)}return d})}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 n=new RegExp(i);t.match(n)&&(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 n;if(this.settings.folderNoteType===u.InsideFolder?n=this.app.vault.getAbstractFileByPath(i.path+"/"+i.name+".md"):this.settings.folderNoteType===u.CustomFilename?n=this.app.vault.getAbstractFileByPath(i.path+"/"+this.settings.folderNoteFilename+".md"):i.parent&&(n=this.app.vault.getAbstractFileByPath(this.getCleanParentPath(i)+i.name+".md")),n instanceof a.TFile){this.log("Found folder note: "+n.path);let s=yield this.app.vault.cachedRead(n);if(s.includes(F.BEGIN_WAYPOINT)||s.includes(this.settings.waypointFlag))return this.log("Found parent waypoint!"),[f.Waypoint,n];if(s.includes(F.BEGIN_LANDMARK)||s.includes(this.settings.landmarkFlag))return this.log("Found parent landmark!"),[f.Landmark,n]}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({},b,yield this.loadData())})}saveSettings(){return l(this,null,function*(){yield this.saveData(this.settings)})}},w=F;w.BEGIN_WAYPOINT="%% Begin Waypoint %%",w.END_WAYPOINT="%% End Waypoint %%",w.BEGIN_LANDMARK="%% Begin Landmark %%",w.END_LANDMARK="%% End Landmark %%";var S=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(n=>n.addOption(u.InsideFolder,"Folder Name Inside").addOption(u.OutsideFolder,"Folder Name Outside").addOption(u.CustomFilename,"Custom Filename").setValue(this.plugin.settings.folderNoteType).onChange(s=>l(this,null,function*(){this.plugin.settings.folderNoteType=s,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(n=>n.setValue(this.plugin.settings.showFolderNotes).onChange(s=>l(this,null,function*(){this.plugin.settings.showFolderNotes=s,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(n=>n.setValue(this.plugin.settings.showNonMarkdownFiles).onChange(s=>l(this,null,function*(){this.plugin.settings.showNonMarkdownFiles=s,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(n=>n.setValue(this.plugin.settings.showEnclosingNote).onChange(s=>l(this,null,function*(){this.plugin.settings.showEnclosingNote=s,yield this.plugin.saveSettings()}))),new a.Setting(t).setName("Folders on Top").setDesc("If enabled, folders will be listed at the top in the generated waypoints.").addToggle(n=>n.setValue(this.plugin.settings.foldersOnTop).onChange(s=>l(this,null,function*(){this.plugin.settings.foldersOnTop=s,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(n=>n.setValue(this.plugin.settings.stopScanAtFolderNotes).onChange(s=>l(this,null,function*(){this.plugin.settings.stopScanAtFolderNotes=s,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(n=>n.setValue(this.plugin.settings.useWikiLinks).onChange(s=>l(this,null,function*(){this.plugin.settings.useWikiLinks=s,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(n=>n.setValue(this.plugin.settings.useFrontMatterTitle).onChange(s=>l(this,null,function*(){this.plugin.settings.useFrontMatterTitle=s,yield this.plugin.saveSettings()}))),new a.Setting(t).setName("Use Spaces for Indentation").setDesc("If enabled, the waypoint list will be indented with spaces rather than with tabs.").addToggle(n=>n.setValue(this.plugin.settings.useSpaces).onChange(s=>l(this,null,function*(){this.plugin.settings.useSpaces=s,yield this.plugin.saveSettings()}))),new a.Setting(t).setName("Number of Spaces for Indentation").setDesc("If spaces are used for indentation, this is the number of spaces that will be used per indentation level.").addText(n=>n.setPlaceholder("2").setValue(""+this.plugin.settings.numSpaces).onChange(s=>l(this,null,function*(){let r=parseInt(s,10);isNaN(r)||(this.plugin.settings.numSpaces=r,yield this.plugin.saveSettings())}))),new a.Setting(t).setName("Waypoint Flag").setDesc("Text flag that triggers waypoint generation in a folder note. Must be surrounded by double-percent signs.").addText(n=>n.setPlaceholder(b.waypointFlag).setValue(this.plugin.settings.waypointFlag).onChange(s=>l(this,null,function*(){s&&s.startsWith("%%")&&s.endsWith("%%")&&s!=="%%"&&s!=="%%%"&&s!=="%%%%"?this.plugin.settings.waypointFlag=s:(this.plugin.settings.waypointFlag=b.waypointFlag,console.error("Error: Waypoint flag must be surrounded by double-percent signs.")),yield this.plugin.saveSettings()}))),new a.Setting(t).setName("Landmark Flag").setDesc("Text flag that triggers landmark generation in a folder note. Must be surrounded by double-percent signs.").addText(n=>n.setPlaceholder(b.landmarkFlag).setValue(this.plugin.settings.landmarkFlag).onChange(s=>l(this,null,function*(){s&&s.startsWith("%%")&&s.endsWith("%%")&&s!=="%%"&&s!=="%%%"&&s!=="%%%%"?this.plugin.settings.landmarkFlag=s:(this.plugin.settings.landmarkFlag=b.landmarkFlag,console.error("Error: Landmark flag must be surrounded by double-percent signs.")),yield this.plugin.saveSettings()}))),new a.Setting(t).setName("Ignored Files/Folders").setDesc("Regex list of files or folders to ignore while making indices. Enter only one regex per line.").addTextArea(n=>n.setPlaceholder(b.ignorePaths.join(`
|
|
`)).setValue(this.plugin.settings.ignorePaths.join(`
|
|
`)).onChange(s=>l(this,null,function*(){let r=s.trim().split(`
|
|
`).map(d=>this.getNormalizedPath(d));this.plugin.settings.ignorePaths=r,yield this.plugin.saveSettings()}))),new a.Setting(t).setName("Custom Folder Note Filename").setDesc("The filename of the folder note. Only used if the folder note style is set to Custom Filename.").addText(n=>n.setValue(this.plugin.settings.folderNoteFilename).onChange(s=>l(this,null,function*(){this.plugin.settings.folderNoteFilename=s,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)}};
|
|
|
|
/* nosourcemap */ |