sanitize-replace-html
v1.0.0
Published
sanitize-html with replaceTags hook
Readme
sanitize-replace-html
Wrapper around sanitize-html that adds a replaceTags hook, to programmatically replace html elements and their children with a different text.
Essentially, it can turn <a href="https://example.com">Example Link</a> into Example Link (https://example.com) during your html sanitization flow.
transformTags isn't sufficient because it gets executed right after the opening tag, at which point the (textual) content of the element and it's children hasn't been processed yet. With discard as the default disallowedTagsMode you lose the link in href, and while transformTags does have access to the attributes, there is no way to add/modify the link text.
License, donations
GPL-3.0. If you want to support my work, you can:
Example
sanitizeReplaceHtml('<a href="https://example.com">Example Link</a>', {
replaceTags: {
'a': (frame) => {
return `${frame.text} (${frame.attribs.href})`;
}
}
});Changelog
1.0.0 (December 20, 2024)
- Initial release, after https://github.com/apostrophecms/sanitize-html/issues/685
