You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3 lines
6.9 KiB

var B=Object.create;var h=Object.defineProperty,V=Object.defineProperties,W=Object.getOwnPropertyDescriptor,j=Object.getOwnPropertyDescriptors,F=Object.getOwnPropertyNames,w=Object.getOwnPropertySymbols,I=Object.getPrototypeOf,A=Object.prototype.hasOwnProperty,K=Object.prototype.propertyIsEnumerable;var D=(n,t,e)=>t in n?h(n,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):n[t]=e,u=(n,t)=>{for(var e in t||(t={}))A.call(t,e)&&D(n,e,t[e]);if(w)for(var e of w(t))K.call(t,e)&&D(n,e,t[e]);return n},T=(n,t)=>V(n,j(t)),L=n=>h(n,"__esModule",{value:!0});var Y=(n,t)=>{L(n);for(var e in t)h(n,e,{get:t[e],enumerable:!0})},$=(n,t,e)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of F(t))!A.call(n,i)&&i!=="default"&&h(n,i,{get:()=>t[i],enumerable:!(e=W(t,i))||e.enumerable});return n},m=n=>$(L(h(n!=null?B(I(n)):{},"default",n&&n.__esModule&&"default"in n?{get:()=>n.default,enumerable:!0}:{value:n,enumerable:!0})),n);var r=(n,t,e)=>new Promise((i,s)=>{var a=l=>{try{c(e.next(l))}catch(S){s(S)}},p=l=>{try{c(e.throw(l))}catch(S){s(S)}},c=l=>l.done?i(l.value):Promise.resolve(l.value).then(a,p);c((e=e.apply(n,t)).next())});Y(exports,{default:()=>b});var H=m(require("obsidian"));var M=m(require("obsidian"));function O(n,t){let e=Object.assign({},t,n);return Object.keys(e).reduce((i,s)=>{let a=n[s],p=typeof a=="undefined"||a===null;return T(u({},i),{[s]:p?t[s]:a})},{})}function R(n,t){try{let e=(0,M.parseYaml)(n);return O(e,t)}catch(e){return t}}var d={None:"",TOC:"[TOC]",_TOC_:"__TOC__",DevonThink:"{{toc}}",TheBrain:"[/toc/]"};var v={style:"bullet",min_depth:2,max_depth:6,externalStyle:"",supportAllMatchers:!1},g="dynamic-toc",P=`.${g}`,N=Object.keys(d);var f=m(require("obsidian"));function X(n){return n.replace(/[[\]]/g,"")}function y(n,t){if(!(n==null?void 0:n.headings))return"";let{headings:e}=n,i=e.filter(a=>!!a&&a.level>=t.min_depth&&a.level<=t.max_depth);if(!i.length)return"";let s=i[0].level;return i.map(a=>{let p=t.style==="number"&&"1."||"-",c=new Array(Math.max(0,a.level-s)).fill(" ").join(""),l=X(a.heading);return`${c}${p} [[#${l}|${l}]]`}).join(`
`)}function k(n){let t=null;return{start:()=>{t=new Date().getTime()},stop:()=>{!t||console.log(`Dynamic TOC ${n} completed in ${new Date().getTime()-t}ms.`)}}}var x=class extends f.MarkdownRenderChild{constructor(t,e,i,s){super(s);this.app=t;this.config=e;this.filePath=i;this.container=s;this.onSettingsChangeHandler=t=>{this.render(O(this.config,t))};this.onFileChangeHandler=t=>{t.deleted||t.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(t){return r(this,null,function*(){let e=k("codeblock renderer");e.start(),this.container.empty(),this.container.classList.add(g);let i=y(this.app.metadataCache.getCache(this.filePath),t||this.config);yield f.MarkdownRenderer.renderMarkdown(i,this.container,this.filePath,this),e.stop()})}};var o=m(require("obsidian"));var _=class extends o.PluginSettingTab{constructor(t,e){super(t,e);this.plugin=e}display(){let{containerEl:t}=this;t.empty(),t.createEl("h2",{text:"Dynamic Table of Contents Settings"}),new o.Setting(t).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(t).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(t).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*(){s<this.plugin.settings.min_depth?new o.Notice("Max Depth is higher than Min Depth"):(this.plugin.settings.max_depth=s,yield this.plugin.saveSettings())})));let e=new o.Setting(t).setName("External rendering support").setDesc("Different markdown viewers provided Table of Contents support such as [TOC] or [[_TOC_]]. You may need to restart Obsidian for this to take effect.").addDropdown(i=>i.addOptions(Object.keys(d).reduce((s,a)=>{let p=d[a];return T(u({},s),{[a]:p})},{})).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(t).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,e.setDisabled(s),yield this.plugin.saveSettings()})))}};var C=m(require("obsidian"));var E=class extends C.MarkdownRenderChild{constructor(t,e,i,s,a){super(s);this.app=t;this.settings=e;this.filePath=i;this.match=a;this.onSettingsChangeHandler=()=>{this.render()};this.onFileChangeHandler=t=>{t.deleted||t.path!==this.filePath||this.render()}}static findMatch(t,e){return Array.from(t.querySelectorAll("p, span, a")).find(s=>s.textContent.toLowerCase().includes(e.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 t=y(this.app.metadataCache.getCache(this.filePath),this.settings),e=document.createElement("div");e.classList.add(g),yield C.MarkdownRenderer.renderMarkdown(t,e,this.filePath,this),this.match.style.display="none";let i=this.containerEl.querySelector(P);i&&this.containerEl.removeChild(i),this.match.parentNode.appendChild(e)})}};var b=class extends H.Plugin{constructor(){super(...arguments);this.onload=()=>r(this,null,function*(){yield this.loadSettings(),console.log("Dynamic TOC Loaded"),this.addSettingTab(new _(this.app,this)),this.registerMarkdownCodeBlockProcessor("toc",(t,e,i)=>{let s=R(t,this.settings);i.addChild(new x(this.app,s,i.sourcePath,e))}),this.registerMarkdownPostProcessor((t,e)=>{let i=this.settings.supportAllMatchers===!0?N:[d[this.settings.externalStyle]];for(let s of i){if(!s)continue;let a=E.findMatch(t,s);!(a==null?void 0:a.parentNode)||e.addChild(new E(this.app,this.settings,e.sourcePath,t,a))}})});this.loadSettings=()=>r(this,null,function*(){this.settings=Object.assign({},v,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={});