@huyooo/ai-chat-shared 0.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +144 -0
- package/dist/index.js +262 -0
- package/dist/index.js.map +1 -0
- package/dist/styles.css +505 -0
- package/package.json +40 -0
- package/src/highlighter.ts +80 -0
- package/src/index.ts +46 -0
- package/src/markdown.ts +79 -0
- package/src/parser.ts +205 -0
- package/src/styles.css +505 -0
- package/src/types.ts +90 -0
package/src/markdown.ts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Markdown 渲染工具
|
|
3
|
+
* 提供统一的 Markdown 渲染函数,供 React 和 Vue 版本使用
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { marked, Renderer } from 'marked'
|
|
7
|
+
import DOMPurify from 'dompurify'
|
|
8
|
+
import { highlightCode } from './highlighter'
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* 渲染 Markdown 为 HTML
|
|
12
|
+
* @param markdown Markdown 文本
|
|
13
|
+
* @returns 安全的 HTML 字符串
|
|
14
|
+
*/
|
|
15
|
+
export function renderMarkdown(markdown: string): string {
|
|
16
|
+
if (!markdown) return ''
|
|
17
|
+
|
|
18
|
+
// 创建自定义渲染器(继承默认渲染器)
|
|
19
|
+
const renderer = new Renderer()
|
|
20
|
+
|
|
21
|
+
// 自定义代码块渲染
|
|
22
|
+
renderer.code = (code: string, language?: string) => {
|
|
23
|
+
const highlighted = highlightCode(code, language)
|
|
24
|
+
const lang = language || 'plaintext'
|
|
25
|
+
return `<pre class="markdown-code-block"><code class="language-${lang}">${highlighted}</code></pre>`
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// 自定义表格渲染(添加样式类和滚动容器)
|
|
29
|
+
renderer.table = (header: string, body: string) => {
|
|
30
|
+
return `<div class="markdown-table-wrapper"><table class="markdown-table"><thead>${header}</thead><tbody>${body}</tbody></table></div>`
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// 自定义链接渲染(添加 target="_blank" 和 rel="noopener noreferrer")
|
|
34
|
+
renderer.link = (href: string, title: string | null, text: string) => {
|
|
35
|
+
const titleAttr = title ? ` title="${title}"` : ''
|
|
36
|
+
return `<a href="${href}"${titleAttr} target="_blank" rel="noopener noreferrer" class="markdown-link">${text}</a>`
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// 自定义列表项渲染,将 checkbox 替换为文本 [x] 或 [ ]
|
|
40
|
+
renderer.listitem = (text: string, task: boolean, checked: boolean) => {
|
|
41
|
+
if (task) {
|
|
42
|
+
// 任务列表:将 checkbox 替换为文本
|
|
43
|
+
const checkboxText = checked ? '[x]' : '[ ]'
|
|
44
|
+
// 移除 marked 生成的 checkbox input,替换为文本
|
|
45
|
+
const textWithoutCheckbox = text.replace(/<input[^>]*>/g, '').trim()
|
|
46
|
+
return `<li class="markdown-task-item"><span class="markdown-task-checkbox">${checkboxText}</span> ${textWithoutCheckbox}</li>`
|
|
47
|
+
}
|
|
48
|
+
// 普通列表项
|
|
49
|
+
return `<li>${text}</li>`
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// 配置 marked(marked 12.x 使用新的配置方式)
|
|
53
|
+
marked.setOptions({
|
|
54
|
+
breaks: true, // 支持换行
|
|
55
|
+
gfm: true, // GitHub Flavored Markdown
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
// 渲染 Markdown(marked 12.x 返回字符串,不是 Promise)
|
|
59
|
+
let html = marked(markdown, { renderer }) as string
|
|
60
|
+
|
|
61
|
+
// 使用 DOMPurify 清理 HTML,防止 XSS
|
|
62
|
+
html = DOMPurify.sanitize(html, {
|
|
63
|
+
ALLOWED_TAGS: [
|
|
64
|
+
'p', 'br', 'strong', 'em', 'u', 's', 'code', 'pre',
|
|
65
|
+
'h1', 'h2', 'h3', 'h4', 'h5', 'h6',
|
|
66
|
+
'ul', 'ol', 'li', 'blockquote',
|
|
67
|
+
'table', 'thead', 'tbody', 'tr', 'th', 'td',
|
|
68
|
+
'a', 'img', 'hr',
|
|
69
|
+
'div', 'span',
|
|
70
|
+
],
|
|
71
|
+
ALLOWED_ATTR: [
|
|
72
|
+
'href', 'title', 'target', 'rel', 'class',
|
|
73
|
+
'src', 'alt', 'width', 'height',
|
|
74
|
+
],
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
return html
|
|
78
|
+
}
|
|
79
|
+
|
package/src/parser.ts
ADDED
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 内容解析器
|
|
3
|
+
* 将 AI 输出的原始文本解析为结构化的内容块
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import type { ContentBlock, TextBlock, CodeBlock } from './types'
|
|
7
|
+
|
|
8
|
+
/** 生成唯一 ID */
|
|
9
|
+
function generateId(): string {
|
|
10
|
+
return Math.random().toString(36).slice(2, 11)
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** 代码块正则:匹配 ```language\ncode\n``` 或 ```\ncode\n``` */
|
|
14
|
+
const CODE_BLOCK_REGEX = /```(\w*)\n([\s\S]*?)```/g
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* 解析原始内容为内容块列表
|
|
18
|
+
* @param raw 原始文本内容
|
|
19
|
+
* @returns 内容块列表
|
|
20
|
+
*/
|
|
21
|
+
export function parseContent(raw: string): ContentBlock[] {
|
|
22
|
+
if (!raw) return []
|
|
23
|
+
|
|
24
|
+
const blocks: ContentBlock[] = []
|
|
25
|
+
let lastIndex = 0
|
|
26
|
+
|
|
27
|
+
// 使用正则匹配所有代码块
|
|
28
|
+
let match: RegExpExecArray | null
|
|
29
|
+
const regex = new RegExp(CODE_BLOCK_REGEX.source, 'g')
|
|
30
|
+
|
|
31
|
+
while ((match = regex.exec(raw)) !== null) {
|
|
32
|
+
// 添加代码块前的文本
|
|
33
|
+
if (match.index > lastIndex) {
|
|
34
|
+
const textContent = raw.slice(lastIndex, match.index)
|
|
35
|
+
if (textContent.trim()) {
|
|
36
|
+
blocks.push(createTextBlock(textContent))
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// 添加代码块
|
|
41
|
+
const language = match[1] || undefined
|
|
42
|
+
const code = match[2]
|
|
43
|
+
blocks.push(createCodeBlock(code, language))
|
|
44
|
+
|
|
45
|
+
lastIndex = match.index + match[0].length
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// 添加最后一段文本
|
|
49
|
+
if (lastIndex < raw.length) {
|
|
50
|
+
const textContent = raw.slice(lastIndex)
|
|
51
|
+
if (textContent.trim()) {
|
|
52
|
+
blocks.push(createTextBlock(textContent))
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return blocks
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** 创建文本块 */
|
|
60
|
+
function createTextBlock(content: string): TextBlock {
|
|
61
|
+
return {
|
|
62
|
+
id: generateId(),
|
|
63
|
+
type: 'text',
|
|
64
|
+
content: content.trim(),
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** 创建代码块 */
|
|
69
|
+
function createCodeBlock(content: string, language?: string, filename?: string): CodeBlock {
|
|
70
|
+
return {
|
|
71
|
+
id: generateId(),
|
|
72
|
+
type: 'code',
|
|
73
|
+
content,
|
|
74
|
+
language,
|
|
75
|
+
filename,
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// ============ 流式解析支持 ============
|
|
80
|
+
|
|
81
|
+
/** 流式解析状态 */
|
|
82
|
+
export interface StreamParseState {
|
|
83
|
+
/** 未完成的缓冲区 */
|
|
84
|
+
buffer: string
|
|
85
|
+
/** 已解析的块 */
|
|
86
|
+
blocks: ContentBlock[]
|
|
87
|
+
/** 是否在代码块中 */
|
|
88
|
+
inCodeBlock: boolean
|
|
89
|
+
/** 当前代码块语言 */
|
|
90
|
+
codeLanguage?: string
|
|
91
|
+
/** 当前代码块内容 */
|
|
92
|
+
codeContent: string
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** 创建初始流式解析状态 */
|
|
96
|
+
export function createStreamParseState(): StreamParseState {
|
|
97
|
+
return {
|
|
98
|
+
buffer: '',
|
|
99
|
+
blocks: [],
|
|
100
|
+
inCodeBlock: false,
|
|
101
|
+
codeLanguage: undefined,
|
|
102
|
+
codeContent: '',
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* 流式解析(增量更新)
|
|
108
|
+
* @param chunk 新增的文本块
|
|
109
|
+
* @param state 当前解析状态
|
|
110
|
+
* @returns 更新后的状态
|
|
111
|
+
*/
|
|
112
|
+
export function parseContentStream(chunk: string, state: StreamParseState): StreamParseState {
|
|
113
|
+
const newState = { ...state }
|
|
114
|
+
newState.buffer += chunk
|
|
115
|
+
|
|
116
|
+
// 处理缓冲区
|
|
117
|
+
while (true) {
|
|
118
|
+
if (newState.inCodeBlock) {
|
|
119
|
+
// 在代码块中,查找结束标记
|
|
120
|
+
const endIndex = newState.buffer.indexOf('```')
|
|
121
|
+
if (endIndex !== -1) {
|
|
122
|
+
// 找到结束标记
|
|
123
|
+
newState.codeContent += newState.buffer.slice(0, endIndex)
|
|
124
|
+
newState.blocks.push(createCodeBlock(
|
|
125
|
+
newState.codeContent.trim(),
|
|
126
|
+
newState.codeLanguage
|
|
127
|
+
))
|
|
128
|
+
newState.buffer = newState.buffer.slice(endIndex + 3)
|
|
129
|
+
newState.inCodeBlock = false
|
|
130
|
+
newState.codeLanguage = undefined
|
|
131
|
+
newState.codeContent = ''
|
|
132
|
+
} else {
|
|
133
|
+
// 没找到结束标记,继续累积
|
|
134
|
+
newState.codeContent += newState.buffer
|
|
135
|
+
newState.buffer = ''
|
|
136
|
+
break
|
|
137
|
+
}
|
|
138
|
+
} else {
|
|
139
|
+
// 不在代码块中,查找开始标记
|
|
140
|
+
const startIndex = newState.buffer.indexOf('```')
|
|
141
|
+
if (startIndex !== -1) {
|
|
142
|
+
// 找到开始标记
|
|
143
|
+
const beforeCode = newState.buffer.slice(0, startIndex)
|
|
144
|
+
if (beforeCode.trim()) {
|
|
145
|
+
newState.blocks.push(createTextBlock(beforeCode))
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// 解析语言标识
|
|
149
|
+
const afterStart = newState.buffer.slice(startIndex + 3)
|
|
150
|
+
const newlineIndex = afterStart.indexOf('\n')
|
|
151
|
+
if (newlineIndex !== -1) {
|
|
152
|
+
newState.codeLanguage = afterStart.slice(0, newlineIndex).trim() || undefined
|
|
153
|
+
newState.buffer = afterStart.slice(newlineIndex + 1)
|
|
154
|
+
newState.inCodeBlock = true
|
|
155
|
+
newState.codeContent = ''
|
|
156
|
+
} else {
|
|
157
|
+
// 语言行还没完整,等待更多数据
|
|
158
|
+
break
|
|
159
|
+
}
|
|
160
|
+
} else {
|
|
161
|
+
// 没有代码块标记,检查是否有不完整的 ``` 开头
|
|
162
|
+
const lastBackticks = newState.buffer.lastIndexOf('`')
|
|
163
|
+
if (lastBackticks !== -1 && newState.buffer.length - lastBackticks < 3) {
|
|
164
|
+
// 可能是不完整的 ```,保留
|
|
165
|
+
const safeText = newState.buffer.slice(0, lastBackticks)
|
|
166
|
+
if (safeText.trim()) {
|
|
167
|
+
newState.blocks.push(createTextBlock(safeText))
|
|
168
|
+
}
|
|
169
|
+
newState.buffer = newState.buffer.slice(lastBackticks)
|
|
170
|
+
} else {
|
|
171
|
+
// 安全的纯文本
|
|
172
|
+
if (newState.buffer.trim()) {
|
|
173
|
+
newState.blocks.push(createTextBlock(newState.buffer))
|
|
174
|
+
}
|
|
175
|
+
newState.buffer = ''
|
|
176
|
+
}
|
|
177
|
+
break
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
return newState
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* 完成流式解析(处理剩余缓冲区)
|
|
187
|
+
* @param state 当前解析状态
|
|
188
|
+
* @returns 最终的内容块列表
|
|
189
|
+
*/
|
|
190
|
+
export function finishStreamParse(state: StreamParseState): ContentBlock[] {
|
|
191
|
+
const blocks = [...state.blocks]
|
|
192
|
+
|
|
193
|
+
if (state.inCodeBlock) {
|
|
194
|
+
// 未闭合的代码块,作为代码块处理
|
|
195
|
+
blocks.push(createCodeBlock(
|
|
196
|
+
(state.codeContent + state.buffer).trim(),
|
|
197
|
+
state.codeLanguage
|
|
198
|
+
))
|
|
199
|
+
} else if (state.buffer.trim()) {
|
|
200
|
+
// 剩余文本
|
|
201
|
+
blocks.push(createTextBlock(state.buffer))
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
return blocks
|
|
205
|
+
}
|