260810_03_ENH: 에디터 버전 맞춤, ssr에 대한 조치
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "kri-ui-registry",
|
"name": "kri-ui-registry",
|
||||||
"version": "0.11.0",
|
"version": "0.11.1",
|
||||||
"private": true,
|
"private": true,
|
||||||
"description": "KRI internal shadcn UI registry",
|
"description": "KRI internal shadcn UI registry",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import { resolveUploadUrl } from './types'
|
|||||||
import { compressImage } from './utils/imageCompression'
|
import { compressImage } from './utils/imageCompression'
|
||||||
import { useDebouncedChange } from './hooks/useDebouncedChange'
|
import { useDebouncedChange } from './hooks/useDebouncedChange'
|
||||||
import { sanitizeHtmlForPreview } from './utils/sanitizeHtml'
|
import { sanitizeHtmlForPreview } from './utils/sanitizeHtml'
|
||||||
|
import { preserveEmptyParagraphs } from './utils/emptyParagraph'
|
||||||
import { useTextareaInsert } from './hooks/useTextareaInsert'
|
import { useTextareaInsert } from './hooks/useTextareaInsert'
|
||||||
|
|
||||||
type HtmlOnlyProps = Omit<KriEditorProps, 'mode'> & { mode: 'html' }
|
type HtmlOnlyProps = Omit<KriEditorProps, 'mode'> & { mode: 'html' }
|
||||||
@@ -121,7 +122,10 @@ export function KriEditorHtmlOnly({
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const previewHtml = React.useMemo(() => sanitizeHtmlForPreview(html), [html])
|
const previewHtml = React.useMemo(
|
||||||
|
() => sanitizeHtmlForPreview(preserveEmptyParagraphs(html)),
|
||||||
|
[html],
|
||||||
|
)
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={cn('rounded-md border bg-background', className)}>
|
<div className={cn('rounded-md border bg-background', className)}>
|
||||||
|
|||||||
@@ -1,12 +1,21 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import DOMPurify from "dompurify";
|
import DOMPurify, { type DOMPurify as DOMPurifyInstance } from "dompurify";
|
||||||
|
|
||||||
export type SanitizeHtmlOptions = {
|
export type SanitizeHtmlOptions = {
|
||||||
tags?: string[];
|
tags?: string[];
|
||||||
attributes?: string[];
|
attributes?: string[];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Next.js SSR(Node) 환경에서는 dompurify가 window에 바인딩된 인스턴스가 아니라
|
||||||
|
// (window: Window) => DOMPurify 형태의 factory만 노출한다. window가 없으면
|
||||||
|
// factory를 호출할 방법이 없으므로, 여기서 안전하게 null로 폴백한다.
|
||||||
|
function resolvePurifier(): DOMPurifyInstance | null {
|
||||||
|
if (typeof window === "undefined") return null;
|
||||||
|
if (typeof DOMPurify.sanitize === "function") return DOMPurify;
|
||||||
|
return DOMPurify(window);
|
||||||
|
}
|
||||||
|
|
||||||
export function sanitizeHtml(
|
export function sanitizeHtml(
|
||||||
input: string,
|
input: string,
|
||||||
options?: SanitizeHtmlOptions,
|
options?: SanitizeHtmlOptions,
|
||||||
@@ -15,7 +24,16 @@ export function sanitizeHtml(
|
|||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
return DOMPurify.sanitize(
|
const purifier = resolvePurifier();
|
||||||
|
|
||||||
|
if (!purifier) {
|
||||||
|
// DOM이 없어 실제 sanitize를 수행할 수 없는 환경(SSR 등)에서는
|
||||||
|
// 검증되지 않은 HTML을 그대로 노출하지 않도록 태그를 모두 제거한 텍스트만 반환한다.
|
||||||
|
// 브라우저에서 하이드레이션된 이후 재계산될 때 실제 sanitize 결과로 대체된다.
|
||||||
|
return input.replace(/<[^>]*>/g, "");
|
||||||
|
}
|
||||||
|
|
||||||
|
return purifier.sanitize(
|
||||||
input,
|
input,
|
||||||
{
|
{
|
||||||
USE_PROFILES: {
|
USE_PROFILES: {
|
||||||
|
|||||||
@@ -329,7 +329,7 @@
|
|||||||
],
|
],
|
||||||
"dependencies": [
|
"dependencies": [
|
||||||
"@tiptap/extension-color",
|
"@tiptap/extension-color",
|
||||||
"@tiptap/extension-font-family",
|
"@tiptap/extension-font-family@3.27.1",
|
||||||
"@tiptap/extension-highlight",
|
"@tiptap/extension-highlight",
|
||||||
"@tiptap/extension-image",
|
"@tiptap/extension-image",
|
||||||
"@tiptap/extension-link",
|
"@tiptap/extension-link",
|
||||||
|
|||||||
Reference in New Issue
Block a user