mdtocs
Version:
Markdown table of contents generator.
3 lines (2 loc) • 1.2 kB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).mdtocs={})}(this,(function(t){"use strict";const e=/^```.*\n[\s\S]*?```$/gm,n=/^(#{1,6}[ \t].+)$|^(.+[\r\n][=-]{3,})$/gm,r=/^(#+)[ \t](.+)$|^(.+)[\r\n]([=-])/,o=/[^a-zA-Z0-9_-]/g,u=/\s/,i="-",c=" ".repeat(2);function s(t){const e=t.match(n);if(null===e)return[];const o={};return e.reduce(((t,e)=>{const[n,u]=function(t){const e=t.match(r);if(null===e)return[];const[,n,o,u,c]=e;let s,f;c?(f=c===i?2:1,s=u.trim()):(f=n.length,s=o.trim());if(!s)return[];return[f,s]}(e);return n&&u&&t.push({level:n,text:u,fragment:f(u,o)}),t}),[])}function f(t,e){const n=t.toLowerCase().split(u).join("-").replace(o,""),r=e[n];return r?(e[n]++,n+i+r):(e[n]=1,n)}t.mdtocs=function(t){return s(function(t){return t.replace(e,"").trim()}(function(t){if("string"!=typeof t)throw new TypeError("First argument must be a string");return t}(t))).reduce(((t,e)=>{const{level:n,text:r,fragment:o}=e;return t+c.repeat(n-1)+"- "+function(t,e){return"["+t+"](#"+e+")"}(r,o)+"\n"}),"")}}));
//# sourceMappingURL=mdtocs.min.js.map