Created
September 20, 2023 16:45
-
-
Save noahlearner/7adb62b91fc5703c9a52395c60bd3b4f to your computer and use it in GitHub Desktop.
sanitizeHTML snippet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* horseman-config data-type-string */ | |
const sanitizeHtml = (await import('https://cdn.skypack.dev/sanitize-html')).default | |
let output = sanitizeHtml(document.querySelector('main').innerHTML, { | |
allowedTags: [ | |
'address', | |
'article', | |
'aside', | |
'footer', | |
'header', | |
'h1', | |
'h2', | |
'h3', | |
'h4', | |
'h5', | |
'h6', | |
'hgroup', | |
'main', | |
'nav', | |
'section', | |
'blockquote', | |
'dd', | |
'div', | |
'dl', | |
'dt', | |
'figcaption', | |
'figure', | |
'hr', | |
'li', | |
'main', | |
'ol', | |
'p', | |
'pre', | |
'ul', | |
'a', | |
'abbr', | |
'b', | |
'bdi', | |
'bdo', | |
'br', | |
'cite', | |
'code', | |
'data', | |
'dfn', | |
'em', | |
'i', | |
'kbd', | |
'mark', | |
'q', | |
'rb', | |
'rp', | |
'rt', | |
'rtc', | |
'ruby', | |
's', | |
'samp', | |
'small', | |
'strong', | |
'sub', | |
'sup', | |
'time', | |
'u', | |
'var', | |
'wbr', | |
'caption', | |
'col', | |
'colgroup', | |
'table', | |
'tbody', | |
'td', | |
'tfoot', | |
'th', | |
'thead', | |
'tr', | |
], | |
disallowedTagsMode: 'discard', | |
allowedAttributes: { | |
a: ['href', 'name', 'target'], | |
// We don't currently allow img itself by default, but | |
// these attributes would make sense if we did. | |
img: ['src', 'srcset', 'alt', 'title', 'width', 'height', 'loading'], | |
}, | |
// Lots of these won't come up by default because we don't allow them | |
selfClosing: ['img', 'br', 'hr', 'area', 'base', 'basefont', 'input', 'link', 'meta'], | |
// URL schemes we permit | |
allowedSchemes: ['http', 'https', 'ftp', 'mailto', 'tel'], | |
allowedSchemesByTag: {}, | |
allowedSchemesAppliedToAttributes: ['href', 'src', 'cite'], | |
allowProtocolRelative: true, | |
enforceHtmlBoundary: false, | |
}); | |
return output.replace(/ |\t/g, "").replace(/\n{2,}/g,"\n").trim(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment