36 lines
598 B
TypeScript
36 lines
598 B
TypeScript
"use client";
|
|
|
|
import DOMPurify from "dompurify";
|
|
|
|
export type SanitizeHtmlOptions = {
|
|
tags?: string[];
|
|
attributes?: string[];
|
|
};
|
|
|
|
export function sanitizeHtml(
|
|
input: string,
|
|
options?: SanitizeHtmlOptions,
|
|
): string {
|
|
if (!input) {
|
|
return "";
|
|
}
|
|
|
|
return DOMPurify.sanitize(
|
|
input,
|
|
{
|
|
USE_PROFILES: {
|
|
html: true,
|
|
},
|
|
ADD_TAGS: options?.tags ?? [],
|
|
ADD_ATTR: options?.attributes ?? [],
|
|
},
|
|
);
|
|
}
|
|
|
|
export function sanitizeHtmlForPreview(
|
|
input: string,
|
|
options?: SanitizeHtmlOptions,
|
|
): string {
|
|
return sanitizeHtml(input, options);
|
|
}
|