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
8.2 KiB

3 years ago
var B=Object.create;var h=Object.defineProperty,W=Object.defineProperties,V=Object.getOwnPropertyDescriptor,F=Object.getOwnPropertyDescriptors,$=Object.getOwnPropertyNames,A=Object.getOwnPropertySymbols,Y=Object.getPrototypeOf,v=Object.prototype.hasOwnProperty,X=Object.prototype.propertyIsEnumerable;var L=(n,e,t)=>e in n?h(n,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):n[e]=t,m=(n,e)=>{for(var t in e||(e={}))v.call(e,t)&&L(n,t,e[t]);if(A)for(var t of A(e))X.call(e,t)&&L(n,t,e[t]);return n},u=(n,e)=>W(n,F(e)),D=n=>h(n,"__esModule",{value:!0});var z=(n,e)=>{D(n);for(var t in e)h(n,t,{get:e[t],enumerable:!0})},q=(n,e,t)=>{if(e&&typeof e=="object"||typeof e=="function")for(let s of $(e))!v.call(n,s)&&s!=="default"&&h(n,s,{get:()=>e[s],enumerable:!(t=V(e,s))||t.enumerable});return n},c=n=>q(D(h(n!=null?B(Y(n)):{},"default",n&&n.__esModule&&"default"in n?{get:()=>n.default,enumerable:!0}:{value:n,enumerable:!0})),n);var a=(n,e,t)=>new Promise((s,i)=>{var r=l=>{try{b(t.next(l))}catch(S){i(S)}},p=l=>{try{b(t.throw(l))}catch(S){i(S)}},b=l=>l.done?s(l.value):Promise.resolve(l.value).then(r,p);b((t=t.apply(n,e)).next())});z(exports,{default:()=>M});var j=c(require("obsidian"));var H=c(require("obsidian"));function x(n,e){let t=Object.assign({},e,n);return Object.keys(t).reduce((s,i)=>{let r=n[i],p=typeof r=="undefined"||r===null;return u(m({},s),{[i]:p?e[i]:r})},{})}function P(n,e){try{let t=(0,H.parseYaml)(n);return x(t,e)}catch(t){return e}}var d={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}`,K=Object.keys(d);var T=c(require("obsidian"));var O=class{constructor(e){this.cached=e}get level(){return this.cached.level}get rawHeading(){return this.cached.heading}get isLink(){return/\[\[(.*?)\]\]/.test(this.cached.heading)}get href(){return this.isLink?`#${this.parseMarkdownLink(this.rawHeading).split("|").join(" ")}`:null}get markdownHref(){if(!this.isLink)return`[[#${this.rawHeading}]]`;let t=this.parseMarkdownLink(this.rawHeading).split("|");return t.length>1?`[[#${t.join(" ")}|${t[1]}]]`:`[[#${t[0]}]]`}parseMarkdownLink(e){let[,t]=e.match(/\[\[(.*?)]\]/)||[];return t}};function G(n,e){let t=n[0].level;return n.map(s=>{let i=e.style==="number"&&"1."||"-";return`${new Array(Math.max(0,s.level-t)).fill(" ").join("")}${i} ${s.markdownHref}`}).join(`
`)}function y(n,e){if(!(n==null?void 0:n.headings))return"";let{headings:t}=n,s=t.filter(i=>!!i&&i.level>=e.min_depth&&i.level<=e.max_depth);return s.length?G(s.map(i=>new O(i)),e):""}var _=class extends T.MarkdownRenderChild{constructor(e,t,s,i){super(i);this.app=e;this.config=t;this.filePath=s;this.container=i;this.onSettingsChangeHandler=e=>{this.render(x(this.config,e))};this.onFileChangeHandler=e=>{e.deleted||e.path!==this.filePath||this.render()}}onload(){return a(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 a(this,null,function*(){this.container.empty(),this.container.classList.add(g);let t=y(this.app.metadataCache.getCache(this.filePath),e||this.config);yield T.MarkdownRenderer.renderMarkdown(t,this.container,this.filePath,this)})}};var o=c(require("obsidian"));var w=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(s=>s.addOptions({bullet:"Bullet",number:"Number"}).setValue(this.plugin.settings.style).onChange(i=>a(this,null,function*(){this.plugin.settings.style=i,yield this.plugin.saveSettings()}))),new o.Setting(e).setName("Minimum Header Depth").setDesc("The default minimum header depth to render").addSlider(s=>s.setLimits(1,6,1).setValue(this.plugin.settings.min_depth).setDynamicTooltip().onChange(i=>a(this,null,function*(){i>this.plugin.settings.max_depth?new o.Notice("Min Depth is higher than Max Depth"):(this.plugin.settings.min_depth=i,yield this.plugin.saveSettings())}))),new o.Setting(e).setName("Maximum Header Depth").setDesc("The default maximum header depth to render").addSlider(s=>s.setLimits(1,6,1).setValue(this.plugin.settings.max_depth).setDynamicTooltip().onChange(i=>a(this,null,function*(){i<this.plugin.settings.min_depth?new o.Notice("Max Depth is higher than Min Depth"):(this.plugin.settings.max_depth=i,yield this.plugin.saveSettings())})));let t=new o.Setting(e).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(s=>s.addOptions(Object.keys(d).reduce((i,r)=>{let p=d[r];return u(m({},i),{[r]:p})},{})).setDisabled(this.plugin.settings.supportAllMatchers).setValue(this.plugin.settings.externalStyle).onChange(i=>a(this,null,function*(){this.plugin.settings.externalStyle=i,yield this.plugin.saveSettings()})));new o.Setting(e).setName("Support all external renderers").setDesc("Cannot be used in conjunction with individual renderers").addToggle(s=>s.setValue(this.plugin.settings.supportAllMatchers).onChange(i=>a(this,null,function*(){this.plugin.settings.supportAllMatchers=i,t.setDisabled(i),yield this.plugin.saveSettings()})))}};var C=c(require("obsidian"));var f=class extends C.MarkdownRenderChild{constructor(e,t,s,i,r){super(i);this.app=e;this.settings=t;this.filePath=s;this.match=r;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(i=>i.textContent.toLowerCase().includes(t.toLowerCase()))||null}onload(){return a(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 a(this,null,function*(){let e=y(this.app.metadataCache.getCache(this.filePath),this.settings),t=document.createElement("div");t.classList.add(g),yield C.MarkdownRenderer.renderMarkdown(e,t,this.filePath,this),this.match.style.display="none";let s=this.containerEl.querySelector(R);s&&this.containerEl.removeChild(s),this.match.parentNode.appendCh