260728_04_ENH: 에디터에 커스텀 태그 인식하도록 수정

This commit is contained in:
2026-07-28 17:08:31 +09:00
parent ac672dfc77
commit cf6e9ec909
5 changed files with 31 additions and 12 deletions

View File

@@ -1,6 +1,6 @@
{ {
"name": "kri-ui-registry", "name": "kri-ui-registry",
"version": "0.9.5", "version": "0.9.6",
"private": true, "private": true,
"description": "KRI internal shadcn UI registry", "description": "KRI internal shadcn UI registry",
"scripts": { "scripts": {

View File

@@ -87,6 +87,8 @@ export function KriEditorWysiwyg(props: KriEditorProps) {
allowBase64 = false, allowBase64 = false,
sanitizeHtml: sanitizeHtmlOpt = true, sanitizeHtml: sanitizeHtmlOpt = true,
toolbar = {}, toolbar = {},
editorExtensions = [],
allowedCustomHtml,
} = props; } = props;
const showHtmlToggle = mode === "both"; const showHtmlToggle = mode === "both";
@@ -179,6 +181,7 @@ export function KriEditorWysiwyg(props: KriEditorProps) {
}, },
}), }),
Placeholder.configure({ placeholder }), Placeholder.configure({ placeholder }),
...editorExtensions,
], ],
content: value || "<p></p>", content: value || "<p></p>",
@@ -436,12 +439,12 @@ export function KriEditorWysiwyg(props: KriEditorProps) {
} }
const raw = htmlDraftRef.current; 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" } }); editor.commands.setContent(safe, { emitUpdate: false, parseOptions: { preserveWhitespace: "full" } });
flushChange(editor.getHTML()); flushChange(editor.getHTML());
setView("wysiwyg"); setView("wysiwyg");
}, [editor, flushChange, sanitizeHtmlOpt]); }, [allowedCustomHtml, editor, flushChange, sanitizeHtmlOpt]);
useEffect(() => { useEffect(() => {
if (!editor) return; if (!editor) return;
@@ -558,8 +561,8 @@ export function KriEditorWysiwyg(props: KriEditorProps) {
}, [command, editor, props.onCommandReady]); }, [command, editor, props.onCommandReady]);
const previewHtml = useMemo(() => { const previewHtml = useMemo(() => {
return sanitizeHtmlOpt ? sanitizeHtml(previewSource) : previewSource; return sanitizeHtmlOpt ? sanitizeHtml(previewSource, allowedCustomHtml) : previewSource;
}, [previewSource, sanitizeHtmlOpt]); }, [allowedCustomHtml, previewSource, sanitizeHtmlOpt]);
if (!editor) { if (!editor) {
return ( return (

View File

@@ -4,6 +4,7 @@ export { KriEditorSms } from './KriEditorSms'
export { KriEditorWysiwyg } from './KriEditorWysiwyg' export { KriEditorWysiwyg } from './KriEditorWysiwyg'
export { ToolbarButton as KriEditorToolbarButton } from './toolbar/ToolbarButton' export { ToolbarButton as KriEditorToolbarButton } from './toolbar/ToolbarButton'
export type { export type {
KriEditorAllowedHtml,
KriEditorCommand, KriEditorCommand,
KriEditorMode, KriEditorMode,
KriEditorProps, KriEditorProps,

View File

@@ -1,3 +1,4 @@
import type { Extensions } from '@tiptap/core'
import type { Editor } from '@tiptap/react' import type { Editor } from '@tiptap/react'
import type { ReactNode } from 'react' import type { ReactNode } from 'react'
@@ -33,6 +34,11 @@ export type KriEditorToolbarContext = {
view: 'wysiwyg' | 'html' view: 'wysiwyg' | 'html'
} }
export type KriEditorAllowedHtml = {
tags?: string[]
attributes?: string[]
}
export type KriEditorProps = { export type KriEditorProps = {
onEditorReady?: (editor: Editor | HTMLTextAreaElement | null) => void onEditorReady?: (editor: Editor | HTMLTextAreaElement | null) => void
onCommandReady?: (command: KriEditorCommand | null) => void onCommandReady?: (command: KriEditorCommand | null) => void
@@ -64,6 +70,8 @@ export type KriEditorProps = {
lmsMaxBytes?: number lmsMaxBytes?: number
toolbar?: KriEditorToolbarConfig | false toolbar?: KriEditorToolbarConfig | false
renderToolbarEnd?: (context: KriEditorToolbarContext) => ReactNode renderToolbarEnd?: (context: KriEditorToolbarContext) => ReactNode
editorExtensions?: Extensions
allowedCustomHtml?: KriEditorAllowedHtml
} }
export type ToolbarState = { export type ToolbarState = {

View File

@@ -2,14 +2,14 @@
import DOMPurify from "dompurify"; import DOMPurify from "dompurify";
const HTML_PROFILE = { export type SanitizeHtmlOptions = {
USE_PROFILES: { tags?: string[];
html: true, attributes?: string[];
}, };
} as const;
export function sanitizeHtml( export function sanitizeHtml(
input: string, input: string,
options?: SanitizeHtmlOptions,
): string { ): string {
if (!input) { if (!input) {
return ""; return "";
@@ -17,12 +17,19 @@ export function sanitizeHtml(
return DOMPurify.sanitize( return DOMPurify.sanitize(
input, input,
HTML_PROFILE, {
USE_PROFILES: {
html: true,
},
ADD_TAGS: options?.tags ?? [],
ADD_ATTR: options?.attributes ?? [],
},
); );
} }
export function sanitizeHtmlForPreview( export function sanitizeHtmlForPreview(
input: string, input: string,
options?: SanitizeHtmlOptions,
): string { ): string {
return sanitizeHtml(input); return sanitizeHtml(input, options);
} }