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.

4 lines
8.5 KiB

var B=Object.create;var h=Object.defineProperty,V=Object.defineProperties,W=Object.getOwnPropertyDescriptor,F=Object.getOwnPropertyDescriptors,$=Object.getOwnPropertyNames,A=Object.getOwnPropertySymbols,Y=Object.getPrototypeOf,v=Object.prototype.hasOwnProperty,X=Object.prototype.propertyIsEnumerable;var L=(n,t,e)=>t in n?h(n,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):n[t]=e,m=(n,t)=>{for(var e in t||(t={}))v.call(t,e)&&L(n,e,t[e]);if(A)for(var e of A(t))X.call(t,e)&&L(n,e,t[e]);return n},u=(n,t)=>V(n,F(t)),D=n=>h(n,"__esModule",{value:!0});var z=(n,t)=>{D(n);for(var e in t)h(n,e,{get:t[e],enumerable:!0})},q=(n,t,e)=>{if(t&&typeof t=="object"||typeof t=="function")for(let s of $(t))!v.call(n,s)&&s!=="default"&&h(n,s,{get:()=>t[s],enumerable:!(e=W(t,s))||e.enumerable});return n},p=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,t,e)=>new Promise((s,i)=>{var r=l=>{try{b(e.next(l))}catch(S){i(S)}},c=l=>{try{b(e.throw(l))}catch(S){i(S)}},b=l=>l.done?s(l.value):Promise.resolve(l.value).then(r,c);b((e=e.apply(n,t)).next())});z(exports,{default:()=>M});var j=p(require("obsidian"));var H=p(require("obsidian"));function x(n,t){let e=Object.assign({},t,n);return Object.keys(e).reduce((s,i)=>{let r=n[i],c=typeof r=="undefined"||r===null;return u(m({},s),{[i]:c?t[i]:r})},{})}function P(n,t){try{let e=(0,H.parseYaml)(n);return x(e,t)}catch(e){return t}}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 y=p(require("obsidian"));var O=class{constructor(t){this.cached=t}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 e=this.parseMarkdownLink(this.rawHeading).split("|");return e.length>1?`[[#${e.join(" ")}|${e[1]}]]`:`[[#${e[0]}]]`}parseMarkdownLink(t){let[,e]=t.match(/\[\[(.*?)]\]/)||[];return e}};function G(n,t){let e=n[0].level,s=t.title?`${t.title}
`:"";return s+=n.map(i=>{let r=t.style==="number"&&"1."||"-";return`${new Array(Math.max(0,i.level-e)).fill(" ").join("")}${r} ${i.markdownHref}`}).join(`
`),s}function T(n,t){if(!(n==null?void 0:n.headings))return"";let{headings:e}=n,s=e.filter(i=>!!i&&i.level>=t.min_depth&&i.level<=t.max_depth);return s.length?G(s.map(i=>new O(i)),t):""}var _=class extends y.MarkdownRenderChild{constructor(t,e,s,i){super(i);this.app=t;this.config=e;this.filePath=s;this.container=i;this.onSettingsChangeHandler=t=>{this.render(x(this.config,t))};this.onFileChangeHandler=t=>{t.deleted||t.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(t){return a(this,null,function*(){this.container.empty(),this.container.classList.add(g);let e=T(this.app.metadataCache.getCache(this.filePath),t||this.config);yield y.MarkdownRenderer.renderMarkdown(e,this.container,this.filePath,this)})}};var o=p(require("obsidian"));var w=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(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(t).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(t).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())}))),new o.Setting(t).setName("Title").setDesc("The title of the table of contents, supports simple markdown such as ## Contents or **Contents**").addText(s=>s.setPlaceholder("## Table of Contents").setValue(this.plugin.settings.title).onChange(i=>a(this,null,function*(){this.plugin.settings.title=i,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(s=>s.addOptions(Object.keys(d).reduce((i,r)=>{let c=d[r];return u(m({},i),{[r]:c})},{})).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(t).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,e.setDisabled(i),yield this.plugin.saveSettings()})))}};var C=p(require("obsidian"));var f=class extends C.MarkdownRenderChild{constructor(t,e,s,i,r){super(i);this.app=t;this.settings=e;this.filePath=s;this.match=r;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(i=>i.textContent.toLowerCase().includes(e.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 t=T(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 s=this.containerEl.querySelector(R);s&&this.containerEl.removeChild(s),this.match.parentNode.appendChild(e)})}};var I=p(require("obsidian")),E={"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/]"}},k=class extends I.FuzzySuggestModal{constructor(t,e){super(t);this.app=t,this.plugin=e,this.setPlaceholder("Type name of table of contents type...")}getItems(){return this.plugin.settings.supportAllMatchers?Object.keys(E):this.plugin.settings.externalStyle!=="None"?["code-block",this.plugin.settings.externalStyle]:["code-block"]}getItemText(t){let e=Object.keys(E).find(s=>s===t);return E[e].label}onChooseItem(t){this.callback(E[t].value)}start(t){this.callback=t,this.open()}};var M=class extends j.Plugin{constructor(){super(...arguments);this.onload=()=>a(this,null,function*(){yield this.loadSettings(),this.addSettingTab(new w(this.app,this)),this.addCommand({id:"dynamic-toc-insert-command",name:"Insert Table of Contents",editorCallback:t=>{new k(this.app,this).start(s=>{t.setCursor(t.getCursor().line,0),t.replaceSelection(s)})}}),this.registerMarkdownCodeBlockProcessor("toc",(t,e,s)=>{let i=P(t,this.settings);s.addChild(new _(this.app,i,s.sourcePath,e))}),this.registerMarkdownPostProcessor((t,e)=>{let s=this.settings.supportAllMatchers===!0?K:[this.settings.externalStyle];for(let i of s){if(!i||i==="None")continue;let r=f.findMatch(t,d[i]);!(r==null?void 0:r.parentNode)||e.addChild(new f(this.app,this.settings,e.sourcePath,t,r))}})});this.loadSettings=()=>a(this,null,function*(){this.settings=Object.assign({},N,yield this.loadData())});this.saveSettings=()=>a(this,null,function*(){yield this.saveData(this.settings),this.app.metadataCache.trigger("dynamic-toc:settings",this.settings)})}};0&&(module.exports={});