优化: AI Chat全栈多批审查修复与架构清理(risk_level清理/路由解耦/工具渲染/测试补测/死代码)
This commit is contained in:
34
src/utils/markdown.ts
Normal file
34
src/utils/markdown.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
/**
|
||||
* 剥离 Markdown 语法字符,返回纯文本。
|
||||
*
|
||||
* 用于列表/卡片摘要等纯文本预览场景(详情页应 v-html 富文本渲染,不用此函数)。
|
||||
* 仅去除粗体、斜体、删除线、标题、行内代码、代码块围栏、列表、引用、
|
||||
* 链接、图片等 Markdown 语法标记,保留文字内容。
|
||||
*
|
||||
* 动机(UX-260618-13):列表摘要用纯文本插值显示 AI 生成内容,
|
||||
* 数据源含 Markdown 语法字符会裸字符外露,列表空间小且配合截断,
|
||||
* 剥离比富文本渲染更合适。
|
||||
*/
|
||||
|
||||
/** 剥离 Markdown 语法,返回纯文本;空输入返回空串 */
|
||||
export function stripMd(text: string | null | undefined): string {
|
||||
if (!text) return ''
|
||||
return text
|
||||
.replace(/!\[([^\]]*)\]\([^)]*\)/g, '$1') // 图片 alt
|
||||
.replace(/\[([^\]]*)\]\([^)]*\)/g, '$1') // 链接文本
|
||||
.replace(/```[a-zA-Z]*\n?/g, '') // 代码块围栏开
|
||||
.replace(/```/g, '') // 代码块围栏闭
|
||||
.replace(/`([^`]+)`/g, '$1') // 行内代码
|
||||
.replace(/^\s{0,3}>\s?/gm, '') // 引用
|
||||
.replace(/^\s{0,3}[-*+]\s+/gm, '') // 无序列表
|
||||
.replace(/^\s{0,3}\d+\.\s+/gm, '') // 有序列表
|
||||
.replace(/^#{1,6}\s+/gm, '') // 标题
|
||||
.replace(/^\s*[-=]{3,}\s*$/gm, '') // 水平线
|
||||
.replace(/\*\*([^*]+)\*\*/g, '$1') // 粗体
|
||||
.replace(/__([^_]+)__/g, '$1') // 粗体下划线
|
||||
.replace(/\*([^*]+)\*/g, '$1') // 斜体
|
||||
.replace(/_([^_]+)_/g, '$1') // 斜体下划线
|
||||
.replace(/~~([^~]+)~~/g, '$1') // 删除线
|
||||
.replace(/\s+/g, ' ') // 折叠连续空白
|
||||
.trim()
|
||||
}
|
||||
Reference in New Issue
Block a user