gristlabs_grist-core/app/client/ui/sanitizeHTML.ts
George Gevoian 292c894b93 (core) Add Markdown cell format
Summary:
Text columns can now display their values as Markdown-formatted text
by changing their cell format to "Markdown". A minimal subset of the
Markdown specification is currently supported.

Test Plan: Browser tests.

Reviewers: Spoffy, dsagal

Reviewed By: Spoffy, dsagal

Subscribers: dsagal, Spoffy

Differential Revision: https://phab.getgrist.com/D4326
2024-08-23 11:24:35 -04:00

37 lines
1.1 KiB
TypeScript

import createDOMPurifier from 'dompurify';
export function sanitizeHTML(source: string | Node): string {
return defaultPurifier.sanitize(source);
}
export function sanitizeTutorialHTML(source: string | Node): string {
return tutorialPurifier.sanitize(source, {
ADD_TAGS: ['iframe'],
ADD_ATTR: ['allowFullscreen'],
});
}
const defaultPurifier = createDOMPurifier();
defaultPurifier.addHook('uponSanitizeAttribute', handleSanitizeAttribute);
const tutorialPurifier = createDOMPurifier();
tutorialPurifier.addHook('uponSanitizeAttribute', handleSanitizeAttribute);
tutorialPurifier.addHook('uponSanitizeElement', handleSanitizeTutorialElement);
function handleSanitizeAttribute(node: Element) {
if (!('target' in node)) { return; }
node.setAttribute('target', '_blank');
}
function handleSanitizeTutorialElement(node: Element, data: createDOMPurifier.SanitizeElementHookEvent) {
if (data.tagName !== 'iframe') { return; }
const src = node.getAttribute('src');
if (src?.startsWith('https://www.youtube.com/embed/')) {
return;
}
node.parentNode?.removeChild(node);
}