var B=Object.create;var m=Object.defineProperty,W=Object.defineProperties,j=Object.getOwnPropertyDescriptor,V=Object.getOwnPropertyDescriptors,F=Object.getOwnPropertyNames,A=Object.getOwnPropertySymbols,Y=Object.getPrototypeOf,k=Object.prototype.hasOwnProperty,X=Object.prototype.propertyIsEnumerable;var D=(n,e,t)=>e in n?m(n,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):n[e]=t,u=(n,e)=>{for(var t in e||(e={}))k.call(e,t)&&D(n,t,e[t]);if(A)for(var t of A(e))X.call(e,t)&&D(n,t,e[t]);return n},y=(n,e)=>W(n,V(e)),L=n=>m(n,"__esModule",{value:!0});var $=(n,e)=>{L(n);for(var t in e)m(n,t,{get:e[t],enumerable:!0})},z=(n,e,t)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of F(e))!k.call(n,i)&&i!=="default"&&m(n,i,{get:()=>e[i],enumerable:!(t=j(e,i))||t.enumerable});return n},c=n=>z(L(m(n!=null?B(Y(n)):{},"default",n&&n.__esModule&&"default"in n?{get:()=>n.default,enumerable:!0}:{value:n,enumerable:!0})),n);var r=(n,e,t)=>new Promise((i,s)=>{var a=l=>{try{h(t.next(l))}catch(b){s(b)}},d=l=>{try{h(t.throw(l))}catch(b){s(b)}},h=l=>l.done?i(l.value):Promise.resolve(l.value).then(a,d);h((t=t.apply(n,e)).next())});$(exports,{default:()=>w});var I=c(require("obsidian"));var v=c(require("obsidian"));function O(n,e){let t=Object.assign({},e,n);return Object.keys(t).reduce((i,s)=>{let a=n[s],d=typeof a=="undefined"||a===null;return y(u({},i),{[s]:d?e[s]:a})},{})}function P(n,e){try{let t=(0,v.parseYaml)(n);return O(t,e)}catch(t){return e}}var p={None:"",TOC:"[TOC]",_TOC_:"__TOC__",AzureWiki:"_TOC_",DevonThink:"{{toc}}",TheBrain:"[/toc/]"};var N={style:"bullet",min_depth:2,max_depth:6,externalStyle:"None",supportAllMatchers:!1},g="dynamic-toc",R=`.${g}`,H=Object.keys(p);var C=c(require("obsidian"));function q(n){return n.replace(/[[\]]/g,"")}function T(n,e){if(!(n==null?void 0:n.headings))return"";let{headings:t}=n,i=t.filter(a=>!!a&&a.level>=e.min_depth&&a.level<=e.max_depth);if(!i.length)return"";let s=i[0].level;return i.map(a=>{let d=e.style==="number"&&"1."||"-",h=new Array(Math.max(0,a.level-s)).fill(" ").join(""),l=q(a.heading);return`${h}${d} [[#${l}|${l}]]`}).join(` `)}var _=class extends C.MarkdownRenderChild{constructor(e,t,i,s){super(s);this.app=e;this.config=t;this.filePath=i;this.container=s;this.onSettingsChangeHandler=e=>{this.render(O(this.config,e))};this.onFileChangeHandler=e=>{e.deleted||e.path!==this.filePath||this.render()}}onload(){return r(this,null,function*(){yield this.render(),this.registerEvent(this.app.metadataCache.on("dynamic-toc:settings",this.onSettingsChangeHandler)),this.registerEvent(this.app.metadataCache.on("changed",this.onFileChangeHandler))})}render(e){return r(this,null,function*(){this.container.empty(),this.container.classList.add(g);let t=T(this.app.metadataCache.getCache(this.filePath),e||this.config);yield C.MarkdownRenderer.renderMarkdown(t,this.container,this.filePath,this)})}};var o=c(require("obsidian"));var x=class extends o.PluginSettingTab{constructor(e,t){super(e,t);this.plugin=t}display(){let{containerEl:e}=this;e.empty(),e.createEl("h2",{text:"Dynamic Table of Contents Settings"}),new o.Setting(e).setName("List Style").setDesc("The table indication").addDropdown(i=>i.addOptions({bullet:"Bullet",number:"Number"}).setValue(this.plugin.settings.style).onChange(s=>r(this,null,function*(){this.plugin.settings.style=s,yield this.plugin.saveSettings()}))),new o.Setting(e).setName("Minimum Header Depth").setDesc("The default minimum header depth to render").addSlider(i=>i.setLimits(1,6,1).setValue(this.plugin.settings.min_depth).setDynamicTooltip().onChange(s=>r(this,null,function*(){s>this.plugin.settings.max_depth?new o.Notice("Min Depth is higher than Max Depth"):(this.plugin.settings.min_depth=s,yield this.plugin.saveSettings())}))),new o.Setting(e).setName("Maximum Header Depth").setDesc("The default maximum header depth to render").addSlider(i=>i.setLimits(1,6,1).setValue(this.plugin.settings.max_depth).setDynamicTooltip().onChange(s=>r(this,null,function*(){si.addOptions(Object.keys(p).reduce((s,a)=>{let d=p[a];return y(u({},s),{[a]:d})},{})).setDisabled(this.plugin.settings.supportAllMatchers).setValue(this.plugin.settings.externalStyle).onChange(s=>r(this,null,function*(){this.plugin.settings.externalStyle=s,yield this.plugin.saveSettings()})));new o.Setting(e).setName("Support all external renderers").setDesc("Cannot be used in conjunction with individual renderers").addToggle(i=>i.setValue(this.plugin.settings.supportAllMatchers).onChange(s=>r(this,null,function*(){this.plugin.settings.supportAllMatchers=s,t.setDisabled(s),yield this.plugin.saveSettings()})))}};var f=c(require("obsidian"));var E=class extends f.MarkdownRenderChild{constructor(e,t,i,s,a){super(s);this.app=e;this.settings=t;this.filePath=i;this.match=a;this.onSettingsChangeHandler=()=>{this.render()};this.onFileChangeHandler=e=>{e.deleted||e.path!==this.filePath||this.render()}}static findMatch(e,t){return Array.from(e.querySelectorAll("p, span, a")).find(s=>s.textContent.toLowerCase().includes(t.toLowerCase()))||null}onload(){return r(this,null,function*(){this.render(),this.registerEvent(this.app.metadataCache.on("dynamic-toc:settings",this.onSettingsChangeHandler)),this.registerEvent(this.app.metadataCache.on("changed",this.onFileChangeHandler))})}render(){return r(this,null,function*(){let e=T(this.app.metadataCache.getCache(this.filePath),this.settings),t=document.createElement("div");t.classList.add(g),yield f.MarkdownRenderer.renderMarkdown(e,t,this.filePath,this),this.match.style.display="none";let i=this.containerEl.querySelector(R);i&&this.containerEl.removeChild(i),this.match.parentNode.appendChild(t)})}};var K=c(require("obsidian")),S={"code-block":{value:"```toc\n```",label:"Code block"},TOC:{value:"[TOC]",label:"[TOC]"},_TOC_:{label:"__TOC__",value:"[[__TOC__]]"},AzureWiki:{label:"_TOC_",value:"[[_TOC_]]"},DevonThink:{label:"{{toc}}",value:"{{toc}}"},TheBrain:{label:"[/toc/]",value:"[/toc/]"}},M=class extends K.FuzzySuggestModal{constructor(e,t){super(e);this.app=e,this.plugin=t,this.setPlaceholder("Type name of table of contents type...")}getItems(){return this.plugin.settings.supportAllMatchers?Object.keys(S):this.plugin.settings.externalStyle!=="None"?["code-block",this.plugin.settings.externalStyle]:["code-block"]}getItemText(e){let t=Object.keys(S).find(i=>i===e);return S[t].label}onChooseItem(e){this.callback(S[e].value)}start(e){this.callback=e,this.open()}};var w=class extends I.Plugin{constructor(){super(...arguments);this.onload=()=>r(this,null,function*(){yield this.loadSettings(),console.log("Dynamic TOC Loaded"),this.addSettingTab(new x(this.app,this)),this.addCommand({id:"dynamic-toc-insert-command",name:"Insert Table of Contents",editorCallback:e=>{new M(this.app,this).start(i=>{e.setCursor(e.getCursor().line,0),e.replaceSelection(i)})}}),this.registerMarkdownCodeBlockProcessor("toc",(e,t,i)=>{let s=P(e,this.settings);i.addChild(new _(this.app,s,i.sourcePath,t))}),this.registerMarkdownPostProcessor((e,t)=>{let i=this.settings.supportAllMatchers===!0?H:[this.settings.externalStyle];for(let s of i){if(!s||s==="None")continue;let a=E.findMatch(e,p[s]);!(a==null?void 0:a.parentNode)||t.addChild(new E(this.app,this.settings,t.sourcePath,e,a))}})});this.loadSettings=()=>r(this,null,function*(){this.settings=Object.assign({},N,yield this.loadData())});this.saveSettings=()=>r(this,null,function*(){yield this.saveData(this.settings),this.app.metadataCache.trigger("dynamic-toc:settings",this.settings)})}};0&&(module.exports={});