Skip to content

Instantly share code, notes, and snippets.

@alltom
Last active January 27, 2022 03:47
Show Gist options
  • Save alltom/c358fdaf07b202eedf95d6c9c5446338 to your computer and use it in GitHub Desktop.
Save alltom/c358fdaf07b202eedf95d6c9c5446338 to your computer and use it in GitHub Desktop.
"Tested" with Trix 0.9.4
var sanitizeHtml = require('sanitize-html');
function localSanitizeHtml(html) {
let allowedAttributes = JSON.parse(JSON.stringify(sanitizeHtml.defaults.allowedAttributes));
allowedAttributes.a = (allowedAttributes.a || []).concat(['data-trix-attachment', 'rel']);
allowedAttributes.figure = (allowedAttributes.figure || []).concat(['class']);
allowedAttributes.figcaption = (allowedAttributes.figcaption || []).concat(['class']);
allowedAttributes.img = (allowedAttributes.img || []).concat(['width', 'height']);
allowedAttributes.span = (allowedAttributes.span || []).concat(['class']);
return sanitizeHtml(html, {
allowedTags: sanitizeHtml.defaults.allowedTags.concat(['img', 'figure', 'figcaption', 'span', 'del']),
allowedAttributes: allowedAttributes,
transformTags: {
a: function(tagName, attribs) {
attribs.target = '_blank';
attribs.rel = 'noreferrer';
return { tagName: tagName, attribs: attribs };
},
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment