diff --git a/package.json b/package.json index d43c1bf..c041b54 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "kri-ui-registry", - "version": "0.9.5", + "version": "0.9.6", "private": true, "description": "KRI internal shadcn UI registry", "scripts": { diff --git a/registry/components/kri-editor/KriEditorWysiwyg.tsx b/registry/components/kri-editor/KriEditorWysiwyg.tsx index 180323d..a576799 100644 --- a/registry/components/kri-editor/KriEditorWysiwyg.tsx +++ b/registry/components/kri-editor/KriEditorWysiwyg.tsx @@ -87,6 +87,8 @@ export function KriEditorWysiwyg(props: KriEditorProps) { allowBase64 = false, sanitizeHtml: sanitizeHtmlOpt = true, toolbar = {}, + editorExtensions = [], + allowedCustomHtml, } = props; const showHtmlToggle = mode === "both"; @@ -179,6 +181,7 @@ export function KriEditorWysiwyg(props: KriEditorProps) { }, }), Placeholder.configure({ placeholder }), + ...editorExtensions, ], content: value || "

", @@ -436,12 +439,12 @@ export function KriEditorWysiwyg(props: KriEditorProps) { } const raw = htmlDraftRef.current; - const safe = sanitizeHtmlOpt ? sanitizeHtml(raw) : raw; + const safe = sanitizeHtmlOpt ? sanitizeHtml(raw, allowedCustomHtml) : raw; editor.commands.setContent(safe, { emitUpdate: false, parseOptions: { preserveWhitespace: "full" } }); flushChange(editor.getHTML()); setView("wysiwyg"); - }, [editor, flushChange, sanitizeHtmlOpt]); + }, [allowedCustomHtml, editor, flushChange, sanitizeHtmlOpt]); useEffect(() => { if (!editor) return; @@ -558,8 +561,8 @@ export function KriEditorWysiwyg(props: KriEditorProps) { }, [command, editor, props.onCommandReady]); const previewHtml = useMemo(() => { - return sanitizeHtmlOpt ? sanitizeHtml(previewSource) : previewSource; - }, [previewSource, sanitizeHtmlOpt]); + return sanitizeHtmlOpt ? sanitizeHtml(previewSource, allowedCustomHtml) : previewSource; + }, [allowedCustomHtml, previewSource, sanitizeHtmlOpt]); if (!editor) { return ( diff --git a/registry/components/kri-editor/index.ts b/registry/components/kri-editor/index.ts index 86e0a7d..11bfa8f 100644 --- a/registry/components/kri-editor/index.ts +++ b/registry/components/kri-editor/index.ts @@ -4,6 +4,7 @@ export { KriEditorSms } from './KriEditorSms' export { KriEditorWysiwyg } from './KriEditorWysiwyg' export { ToolbarButton as KriEditorToolbarButton } from './toolbar/ToolbarButton' export type { + KriEditorAllowedHtml, KriEditorCommand, KriEditorMode, KriEditorProps, diff --git a/registry/components/kri-editor/types.ts b/registry/components/kri-editor/types.ts index ed055a4..fd9f394 100644 --- a/registry/components/kri-editor/types.ts +++ b/registry/components/kri-editor/types.ts @@ -1,3 +1,4 @@ +import type { Extensions } from '@tiptap/core' import type { Editor } from '@tiptap/react' import type { ReactNode } from 'react' @@ -33,6 +34,11 @@ export type KriEditorToolbarContext = { view: 'wysiwyg' | 'html' } +export type KriEditorAllowedHtml = { + tags?: string[] + attributes?: string[] +} + export type KriEditorProps = { onEditorReady?: (editor: Editor | HTMLTextAreaElement | null) => void onCommandReady?: (command: KriEditorCommand | null) => void @@ -64,6 +70,8 @@ export type KriEditorProps = { lmsMaxBytes?: number toolbar?: KriEditorToolbarConfig | false renderToolbarEnd?: (context: KriEditorToolbarContext) => ReactNode + editorExtensions?: Extensions + allowedCustomHtml?: KriEditorAllowedHtml } export type ToolbarState = { diff --git a/registry/components/kri-editor/utils/sanitizeHtml.ts b/registry/components/kri-editor/utils/sanitizeHtml.ts index 3e52308..720a9b2 100644 --- a/registry/components/kri-editor/utils/sanitizeHtml.ts +++ b/registry/components/kri-editor/utils/sanitizeHtml.ts @@ -2,14 +2,14 @@ import DOMPurify from "dompurify"; -const HTML_PROFILE = { - USE_PROFILES: { - html: true, - }, -} as const; +export type SanitizeHtmlOptions = { + tags?: string[]; + attributes?: string[]; +}; export function sanitizeHtml( input: string, + options?: SanitizeHtmlOptions, ): string { if (!input) { return ""; @@ -17,12 +17,19 @@ export function sanitizeHtml( return DOMPurify.sanitize( input, - HTML_PROFILE, + { + USE_PROFILES: { + html: true, + }, + ADD_TAGS: options?.tags ?? [], + ADD_ATTR: options?.attributes ?? [], + }, ); } export function sanitizeHtmlForPreview( input: string, + options?: SanitizeHtmlOptions, ): string { - return sanitizeHtml(input); + return sanitizeHtml(input, options); }