UNPKG

prevent-widows

Version:

Prevent widows from appearing in a string.

47 lines (46 loc) 1.63 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const __1 = __importDefault(require("..")); const defaultPosthtmlOptions = { attrName: "prevent-widows", attrRemove: true, }; const posthtml = (customPosthtmlOptions = defaultPosthtmlOptions, preventWidowsOptions) => { const posthtmlOptions = Object.assign(Object.assign({}, defaultPosthtmlOptions), customPosthtmlOptions); function processNodes(nodes) { return nodes.map((node) => { if (typeof node == "object") { if (node.content) { node.content = processNodes(node.content); } return node; } if (typeof node === "string" && /<!--/g.test(node)) { return node; } return (0, __1.default)(node, preventWidowsOptions); }); } return (tree) => { const attrMatch = { [posthtmlOptions.attrName]: "", }; tree.match({ attrs: attrMatch }, (node) => { if (posthtmlOptions.attrRemove) { delete node.attrs[posthtmlOptions.attrName]; } else { node.attrs[posthtmlOptions.attrName] = posthtmlOptions.attrName; } // Apply the content if (node.content) { node.content = processNodes(node.content); } return node; }); }; }; exports.default = posthtml;