website-01/lib/content.ts
2026-06-22 14:43:46 +08:00

16 lines
659 B
TypeScript

/**
* 内容格式检测:判断 `content` 字段是 HTML 还是 Markdown。
*
* 业务前提:
* - RichEditor (Quill) 输出的 HTML 始终包含 <p> 等块级标签
* - 纯 Markdown 文本不含 HTML 块级标签(行内 `<em>` 等极少见,且即便匹配也按 HTML 处理仍可被 dangerouslySetInnerHTML 渲染)
*/
const HTML_BLOCK_RE =
/<\/?(p|div|span|h[1-6]|ul|ol|li|table|tbody|thead|tr|td|th|br|img|a|strong|em|b|i|blockquote|pre|code|hr|figure|figcaption|section|article|header|footer|html|body)\b/i;
export function isHTMLContent(s: string | null | undefined): boolean {
if (!s) return false;
return HTML_BLOCK_RE.test(s);
}