const d=function(t){return encodeURIComponent(String(t).trim().toLowerCase().replace(/\s+/g,"-"))},h={includeLevel:[1,2],containerClass:"table-of-contents",slugify:d,markerPattern:/^\[\[toc\]\]/im,listType:"ul",format:function(t,o){return o.renderInline(t)},forceFullToc:!1,containerHeaderHtml:void 0,containerFooterHtml:void 0,transformLink:void 0};function g(t,o,n){const r=[];let e=null;return o.forEach(c=>{if(c.type==="heading_open"){const l=x(c),s=parseInt(c.tag.toLowerCase().replace("h",""),10);t.indexOf(s)>=0&&(e={level:s,text:null,anchor:l||null})}else if(e&&c.type==="inline"){const l=c.children.filter(s=>s.type==="text"||s.type==="code_inline").reduce((s,i)=>s+i.content,"");e.text=l,e.anchor||(e.anchor=n.slugify(l,c.content))}else c.type==="heading_close"&&(e&&r.push(e),e=null)}),r}function x(t){if(t&&t.attrs&&t.attrs.length>0){const o=t.attrs.find(n=>Array.isArray(n)&&n.length>=2?n[0]==="id":!1);if(o&&Array.isArray(o)&&o.length>=2){const[n,r]=o;return r}}return null}function y(t){return Math.min(...t.map(o=>o.level))}function a(t,o,n,r){const e={level:t,text:o,anchor:n,children:[],parent:r};return r.children.push(e),e}function m(t){const o={level:y(t)-1,anchor:null,text:null,children:[],parent:null};let n=o,r=n;return t.forEach(e=>{if(e.level>r.level)Array.from({length:e.level-r.level}).forEach(c=>{n=r,r=a(e.level,null,null,n)}),r.text=e.text,r.anchor=e.anchor;else if(e.level===r.level)r=a(e.level,e.text,e.anchor,n);else if(e.level<r.level){for(let c=0;c<r.level-e.level;c++)n=n.parent;r=a(e.level,e.text,e.anchor,n)}}),o}function p(t,o,n){return"<"+o.listType+">"+t.children.map(r=>{let e="<li>",c=r.anchor;o&&o.transformLink&&(c=o.transformLink(c));let l=r.text?o.format(r.text,n,c):null;return e+=c?`<a href="#${c}">${l}</a>`:l||"",e+(r.children.length>0?p(r,o,n):"")+"</li>"}).join("")+"</"+o.listType+">"}var H=function(t,o){const n=Object.assign({},h,o),r=n.markerPattern;let e;function c(l,s){let i,u;if(l.src.charCodeAt(l.pos)!==91||s||(u=r.exec(l.src.substr(l.pos)),u=u?u.filter(function(v){return v}):[],u.length<1))return!1;i=l.push("toc_open","toc",1),i.markup="[[toc]]",i=l.push("toc_body","",0),i=l.push("toc_close","toc",-1);var f=l.src.indexOf(`
`,l.pos);return f!==-1?l.pos=f:l.pos=l.pos+l.posMax+1,!0}t.renderer.rules.toc_open=function(l,s){var i='<div class="'+n.containerClass+'">';return n.containerHeaderHtml&&(i+=n.containerHeaderHtml),i},t.renderer.rules.toc_close=function(l,s){var i="";return n.containerFooterHtml&&(i=n.containerFooterHtml),i+"</div>"},t.renderer.rules.toc_body=function(l,s){if(n.forceFullToc)throw"forceFullToc was removed in version 0.5.0. For more information, see https://github.com/Oktavilla/markdown-it-table-of-contents/pull/41";{const i=g(n.includeLevel,e.tokens,n),u=m(i);return p(u,n,t)}},t.core.ruler.push("grab_state",function(l){e=l}),t.inline.ruler.after("emphasis","toc",c)};export{H as m};
