UNPKG

@apihawk/help-center-sdk

Version:

The ApiHawk Help Center SDK

31 lines (30 loc) 1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const cheerio = require("cheerio"); const transliteration_1 = require("transliteration"); function setMissingIdAttributes(html) { if (typeof html !== 'string' || !html.length) { return ''; } // @ts-ignore const $ = cheerio.load(html, { xml: { withDomLvl1: false } }); const titles = Array.from($('h1, h2, h3, h4, h5, h6')); titles.forEach((title) => { if (!$(title).attr('id')) { $(title).attr('id', transliteration_1.slugify(transliteration_1.transliterate($(title).text()))); } }); // fixes cheerio generating self-closing <iframe> tags // which aren't valid by specification return $.html({ decodeEntities: false }).replace(/<iframe(.*?)\/>/g, (match, m) => { if (m.match(/</)) { return match; } return `<iframe${m}></iframe>`; }); } exports.default = setMissingIdAttributes;