UNPKG

mdtocs

Version:

Markdown table of contents generator.

3 lines (2 loc) 1.22 kB
!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";var e=/^```.*\n[\s\S]*?```$/gm,r=/^(#{1,6}[ \t].+)$|^(.+[\r\n][=-]{3,})$/gm,n=/^(#+)[ \t](.+)$|^(.+)[\r\n]([=-])/,u=/[^a-zA-Z0-9_-]/g,i=/\s/,o="-",f=" ".repeat(2);function a(t){var e=t.match(r);if(null===e)return[];var u={};return e.reduce((function(t,e){var r=function(t){var e=t.match(n);if(null===e)return[];var r,u,i=e[1],f=e[2],a=e[3],c=e[4];c?(u=c===o?2:1,r=a.trim()):(u=i.length,r=f.trim());if(!r)return[];return[u,r]}(e),i=r[0],f=r[1];return i&&f&&t.push({level:i,text:f,fragment:c(f,u)}),t}),[])}function c(t,e){var r=t.toLowerCase().split(i).join("-").replace(u,""),n=e[r];return n?(e[r]++,r+o+n):(e[r]=1,r)}t.mdtocs=function(t){return a(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((function(t,e){var r=e.level,n=e.text,u=e.fragment;return t+f.repeat(r-1)+"- "+function(t,e){return"["+t+"](#"+e+")"}(n,u)+"\n"}),"")}})); //# sourceMappingURL=mdtocs.min.js.map