text: "use strict";class Moglog{constructor(t){this._defaultOptions={toc:"",tocIn:"replace",tocClass:"s-toc",contents:"",linkPrefix:"",header:"",htags:"h1,h2,h3,h4,h5,h6",isDebug:false,callback:null};let e=Object.assign(Object.assign({},this._defaultOptions),t);this.tocTarget=e.toc?e.toc:this._defaultOptions.toc;this.tocIn=e.tocIn?e.tocIn:this._defaultOptions.tocIn;this.contextTarget=e.contents?e.contents:this._defaultOptions.contents;this.anchorNamePrefix=e.linkPrefix?e.linkPrefix:this._defaultOptions.linkPrefix;this.prependHtml=e.header?e.header:this._defaultOptions.header;this.tocClassName=e.tocClass?e.tocClass:this._defaultOptions.tocClass;this.htags=e.htags?e.htags:this._defaultOptions.htags;this._isDebug=e.isDebug===true?e.isDebug:false;this._callback=typeof e.callback==="function"?e.callback:{}}log(t){if(this._isDebug)console.log(`[Moglog console] ${t}`)}callback(t){if(typeof this._callback==="function"){this._callback(t)}}build(){let t=document.body.querySelector(this.tocTarget);if(!t){this.log("toc target not found");this.callback(false);return false}let e=document.createElement("div");e.setAttribute("class",this.tocClassName);if(this.tocIn=="prepend"){this.prependEl(t,e)}else if(this.tocIn=="append"){this.appendEl(t,e)}else{t.innerHTML="";this.appendEl(t,e)}if(this._isDebug)e.innerHTML="...";let l;let s=document.body.querySelector(this.contextTarget);if(s!=null){l=s.querySelectorAll(this.htags)}else{this.log("context target not found");this.callback(false);return false}let c=[];Array.prototype.forEach.call(l,(t,e)=>{let l=t.tagName;let s=t.innerText;let i=this.anchorNamePrefix+s+"-"+e;let a=document.createElement("span");a.setAttribute("id",i);this.prependEl(t,a);let n={name:i,section:e+1,level:parseInt(l.slice(-1)),text:s};c.push(n)});e.innerHTML=this.prependHtml+this.buildTocString(c);this.callback(c)}buildTocString(t){let a=[0,0,0,0,0,0,0,0,0];let n=1;let c="";t.forEach((t,e)=>{let l=t.level;if(n>l){a.forEach((t,e)=>{if(e>l){a[e]=0}})}a[l]++;let s="";a.forEach((t,e)=>{if(e<=l&&e!=0){s+=t+"."}});let i=`<a href="#${t.name}"><span class="tocnumber">${s}</span> <span class="toctext">${t.text}</span></a>`;if(n<l){i=`<ul><li>${i}`}else if(n>l){i="</li>"+this.repeatString("</ul>",n-l)+`<li>${i}`}else if(e==0){i=`<li>${i}`}else{i=`</li><li>${i}`}c+=i;n=l});if(t.length>0){c="<ul>"+c+"</ul>"}return c}repeatString(t,e){let l="";while(e>0){l+=t;e--}return l}appendEl(t,e){t.insertAdjacentElement("beforeend",e)}prependEl(t,e){t.insertAdjacentElement("afterbegin",e)}}