UNPKG

mume-with-litvis

Version:

Fork of mume with added http://litvis.org/

35 lines 1.22 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const uslug = require("uslug"); class HeadingIdGenerator { constructor() { this.table = {}; } generateId(heading) { const replacement = (match, capture) => { const sanitized = capture .replace(/[!"#$%&'()*+,./:;<=>?@[\\]^`{|}~]/g, "") .replace(/^\s/, "") .replace(/\s$/, "") .replace(/`/g, "~"); return ((capture.match(/^\s+$/) ? "~" : sanitized) + (match.endsWith(" ") && !sanitized.endsWith("~") ? "~" : "")); }; heading = heading .trim() .replace(/~|。/g, "") // sanitize .replace(/``(.+?)``\s?/g, replacement) .replace(/`(.*?)`\s?/g, replacement); let slug = uslug(heading.replace(/\s/g, "~")).replace(/~/g, "-"); if (this.table[slug] >= 0) { this.table[slug] = this.table[slug] + 1; slug += "-" + this.table[slug]; } else { this.table[slug] = 0; } return slug; } } exports.default = HeadingIdGenerator; //# sourceMappingURL=heading-id-generator.js.map