UNPKG

generatoc

Version:

Automatically generate table of content from heading of HTML document

2 lines (1 loc) 4.99 kB
var generatoc=function(){"use strict";function e(e){return e[e.length-1]}function t(e){return+e.substr(1)}function n(e){var t={top:0,left:0};if(!e.getClientRects().length)return t;if("none"===window.getComputedStyle(e).display)return t;t=e.getBoundingClientRect();var n=e.ownerDocument.documentElement;return{top:t.top+window.pageYOffset-n.clientTop,left:t.left+window.pageXOffset-n.clientLeft}}function r(e,t,n,l){var o={index:l,level:null,ele:null,children:[]};return e<=0?(o.level=n,o.ele=t):(o.level=n-e,o.children=[r(--e,t,n,l)]),o}var l,o="",i="",c="#toc",a=0,u=7,d=!1,f=[],s=null;function v(){var e,t=window.pageYOffset||document.documentElement.scrollTop||document.body.scrollTop||0;document.body.offsetHeight,document.body.scrollHeight;window.requestAnimationFrame((function(){var r=null,o=0,i=null;if(l.forEach((function(e,l){var i=Math.abs(n(e.nextElementSibling?e.nextElementSibling:e).top-t-a);if(!(null==r||i<r))return!1;r=i,o=l})),l[o]){i=l[o].innerText;var c=document.querySelector('a[data-toc-index="'+o+'"]');if(c&&(e=c.closest("ul"))&&(g(e),y(e),s)){if("boolean"!=typeof s){var u=s.replacePattern,d=s.replacement;s.readableSpace?i=i.replace(/\s/g,"-"):u instanceof RegExp&&void 0!==d&&(i=i.replace(u,d))}window.location.replace("#"+i)}}}))}function p(e){!function(e,t,n){if(void 0===t&&(t=0),void 0===n&&(n=2),!(e===t||n<1)){var r=e;!function e(){r+=(t-r)/n,Math.abs(t-r)<1?window.scrollTo(0,t):(window.scrollTo(0,r),requestAnimationFrame(e))}()}}(document.documentElement.scrollTop||document.body.scrollTop,n(l[+e]).top-a,u)}function h(e){e.id!==c.substr(1)&&(Array.prototype.forEach.call(e.children,(function(e){"UL"===e.tagName&&(e.style.transform="scaleY(1)",e.style.maxHeight="200px")})),h(e.parentElement))}function m(e){if(e&&e.children&&0!==e.children.length)return"UL"===e.tagName?(Array.prototype.forEach.call(e.children,(function(e){"UL"===e.tagName&&(e.style.transform="scaleY(1)",e.style.maxHeight="200px")})),m(e.children[0])):void 0}function y(e){Array.prototype.includes.call(e.classList,"active")||e.querySelector("li").classList.add("active")}function g(e){if(e){var t="UL"===e.tagName?e:e.closest("ul");t&&(!function(e){Array.prototype.forEach.call(e.children,(function(e){e.querySelector("li").classList.remove("active");var t=e.querySelectorAll("ul");t&&Array.prototype.forEach.call(t,(function(e){e&&(e.querySelector("li").classList.remove("active"),e.style.transform="scaleY(0)",e.style.maxHeight="0px")}))}))}(document.querySelector(c)),m(t.children[1]),h(e),y(e))}}function w(e){var t=document.createElement("ul");if(e.ele){var n=function(e,t){var n=document.createElement("li");n.setAttribute("style","cursor: pointer;");var r=document.createElement("a");return r.setAttribute("data-toc-index",t.toString()),r.innerHTML=e||"",n.appendChild(r),n}(e.ele.textContent,e.index);t.append(n)}return e.children.length>0&&e.children.forEach((function(e){t.append(w(e))})),t}function E(n,l,o,i){var c=t(n.localName),a=l?t(l.localName):0,u={index:i,level:c,ele:null,children:[]};if(c===a)u.ele=n,u.level=c,function(t){if(0===t.length)return t;for(var n=e(t),r=t;0!==n.children.length;)r=n.children,n=e(n.children);return r}(o).push(u);else if(c>a){var d=c-a;(function(t){for(var n=t;0!==n.length;)n=e(n).children;return n})(o).push(r(d-1,n,c,i))}else u.ele=n,function(t,n,r){for(var l=e(n),o={index:r,level:null,ele:null,children:n};l.level!==t&&(o=l,void 0!==(l=e(l.children))););return o}(c,o,i).children.push(u)}function x(e){var t=e.target;if("A"===t.tagName){p(t.getAttribute("data-toc-index")||"");var n=t.closest("ul");n&&function(e){g(e)}(n)}}function b(){var e=document.querySelector(c);null!==e&&f[0]&&(f[0].index=-1,Array.prototype.forEach.call(f[0].children,(function(t){e.appendChild(w(t))})),e.addEventListener("click",x),l.length>0&&window.addEventListener("scroll",function(e,t){void 0===t&&(t=500);var n=null,r=!0;return function(){for(var l=this,o=[],i=0;i<arguments.length;i++)o[i]=arguments[i];if(r)return e.apply(this,o),r=!1;n||(n=setTimeout((function(){clearTimeout(n),n=null,e.apply(l,o)}),t))}}(v),!1))}var A={init:function(e){var t=e.content,n=e.heading,r=void 0===n?["h2","h3","h4","h5"]:n,h=e.selector,m=void 0===h?"#toc":h,y=e.scrollHistory,g=void 0===y?null:y,w=e.scrollOffset,x=void 0===w?0:w,A=e.duration,S=void 0===A?7:A,L=e.fold,T=void 0!==L&&L;c=m,i=r.join(","),o=t,s=g,a=x,u=S,d=T;var q,H,C,N=document.querySelector(o);N&&((l=N.querySelectorAll(i)).forEach((function(e,t){E(e,0===t?null:l[t-1],f,t)})),b(),T&&v(),"boolean"!=typeof s&&(null==s?void 0:s.scrollToAfterMounted)&&(q=window.location.hash.replace("#",""),H=decodeURIComponent(q),-1!==(C=Array.prototype.findIndex.call(l,(function(e){return e.innerText===H})))&&p(C.toString())))},destroy:function(){var e=document.querySelector(c);e&&(e.removeEventListener("click",x),f=[],e.innerHTML="",window.removeEventListener("scroll",v))},refresh:function(){A.destroy(),A.init({content:o,heading:i.split(","),selector:c,scrollOffset:a,duration:u,fold:d})}};return A}();