mirror of
https://github.com/shoelace-style/webawesome.git
synced 2026-01-12 04:09:12 +00:00
25 lines
746 B
JavaScript
25 lines
746 B
JavaScript
/**
|
|
* @typedef {object} Replacement
|
|
* @property {string | RegExp} pattern
|
|
* @property {string} replacement
|
|
*/
|
|
|
|
/**
|
|
* @typedef {Array<Replacement>} Replacements
|
|
*/
|
|
|
|
/**
|
|
* @param {Document} content
|
|
* @param {Replacements} replacements
|
|
*/
|
|
module.exports = function (content, replacements) {
|
|
/** This seems trivial, but by assigning to a string first, THEN using innerHTML after iterating over every replacement, we reduce the calculations of JSDOM. At the time of writing benchmarks show a reduction from 9seconds to 3 seconds by doing so. */
|
|
let html = content.body.innerHTML;
|
|
|
|
replacements.forEach(replacement => {
|
|
html = html.replaceAll(replacement.pattern, replacement.replacement);
|
|
});
|
|
|
|
content.body.innerHTML = html;
|
|
};
|