1 import DOMPurify from 'dompurify';
3 const addRelNoopenerAndTargetBlank = (node: Element) => {
4 if (node.tagName === 'A') {
5 node.setAttribute('rel', 'noopener noreferrer');
6 node.setAttribute('target', '_blank');
10 export const restrictedCalendarSanitize = (source: string) => {
11 DOMPurify.clearConfig();
12 DOMPurify.addHook('afterSanitizeAttributes', addRelNoopenerAndTargetBlank);
14 const sanitizedContent = DOMPurify.sanitize(source, {
15 ALLOWED_TAGS: ['a', 'b', 'em', 'br', 'i', 'u', 'ul', 'ol', 'li', 'span', 'p'],
16 ALLOWED_ATTR: ['href'],
19 DOMPurify.removeHook('afterSanitizeAttributes');
21 return sanitizedContent;
24 export const stripAllTags = (source: string) => {
25 const html = restrictedCalendarSanitize(source);
26 const div = document.createElement('DIV');
27 div.style.whiteSpace = 'pre-wrap';
29 div.querySelectorAll('a').forEach((element) => {
30 element.innerText = element.href || element.innerText;
32 // Append it to force a layout pass so that innerText returns newlines
33 document.body.appendChild(div);
34 const result = div.innerText;
35 document.body.removeChild(div);