@ministryofjustice/hmpps-digital-prison-reporting-frontend
Version:
The Digital Prison Reporting Frontend contains templates and code to help display data effectively in UI applications.
35 lines (32 loc) • 1.22 kB
JavaScript
import sanitizeHtml from 'sanitize-html';
const sanitiseHtml = (text) => {
return sanitizeHtml(text, {
allowedTags: ['p', 'strong', 'em', 'ul', 'ol', 'li', 'a', 'br', 'div', 'h1', 'h2', 'h3', 'h4'],
allowedAttributes: {
'*': ['class'],
a: ['href', 'target', 'rel'],
},
allowedSchemes: ['https', 'mailto'],
allowProtocolRelative: false,
transformTags: {
a: (tagName, attribs) => {
const newAttribs = { ...attribs };
if (newAttribs['target'] && !['_blank', '_self'].includes(newAttribs['target'])) {
delete newAttribs['target'];
}
if (newAttribs['target'] === '_blank') {
const rel = new Set((newAttribs['rel'] ?? '').split(/\s+/).filter(Boolean));
rel.add('noopener');
rel.add('noreferrer');
newAttribs['rel'] = [...rel].join(' ');
}
return {
tagName,
attribs: newAttribs,
};
},
},
});
};
export { sanitiseHtml };
//# sourceMappingURL=sanitizeHtml.js.map