unis_manager/web/src/components/RichText.tsx

13 lines
732 B
TypeScript
Executable File
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import { hasRichMarks, sanitizeRich, textToHtml } from "../utils/richtext";
/**
* 正文展示:一律过一遍消毒再注入(内容是用户数据,不能直接 innerHTML)。
* 旧数据是纯文本,走同一条路(只把换行变成 <br>),所以调用方不用区分。
* 需要纯文本的地方(关键字高亮、导出、AI)用 utils/richtext 的 richToText。
*/
export function RichText({ value, className }: { value?: string | null; className?: string }) {
const html = hasRichMarks(value) ? sanitizeRich(value) : textToHtml(String(value || ""));
if (!html) return null;
return <span className={"rich-view" + (className ? " " + className : "")} dangerouslySetInnerHTML={{ __html: html }} />;
}