SkillMarkdown.tsx
3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import type { ReactNode } from 'react';
function renderInline(text: string, keyPrefix: string) {
const nodes: ReactNode[] = [];
const pattern = /(`[^`]+`)|(\[[^\]]+\]\([^)]+\))/g;
let lastIndex = 0;
let match: RegExpExecArray | null;
match = pattern.exec(text);
while (match) {
if (match.index > lastIndex) {
nodes.push(text.slice(lastIndex, match.index));
}
const token = match[0];
if (token.startsWith('`')) {
nodes.push(
<code
key={`${keyPrefix}-code-${match.index}`}
className="rounded bg-[var(--gl-color-bg-page)] px-1.5 py-0.5 font-mono text-[13px]"
>
{token.slice(1, -1)}
</code>,
);
} else {
const link = /^\[([^\]]+)\]\(([^)]+)\)$/.exec(token);
if (link) {
nodes.push(
<a
key={`${keyPrefix}-link-${match.index}`}
className="text-[var(--gl-color-primary)] underline"
href={link[2]}
rel="noreferrer"
target="_blank"
>
{link[1]}
</a>,
);
}
}
lastIndex = match.index + token.length;
match = pattern.exec(text);
}
if (lastIndex < text.length) {
nodes.push(text.slice(lastIndex));
}
return nodes;
}
function renderParagraph(text: string, key: string) {
if (text.startsWith('### ')) return <h4 key={key} className="m-0 text-base font-semibold">{text.slice(4)}</h4>;
if (text.startsWith('## ')) return <h3 key={key} className="m-0 text-lg font-semibold">{text.slice(3)}</h3>;
if (text.startsWith('# ')) return <h2 key={key} className="m-0 text-xl font-semibold">{text.slice(2)}</h2>;
if (text.startsWith('> ')) return <blockquote key={key} className="m-0 border-l-4 border-[var(--gl-color-border-secondary)] pl-3 text-[var(--gl-color-text-secondary)]">{renderInline(text.slice(2), key)}</blockquote>;
return <p key={key} className="m-0 leading-[var(--gl-line-height-md)]">{renderInline(text, key)}</p>;
}
export function SkillMarkdown({ markdown }: { markdown: string }) {
const lines = markdown.split('\n');
const nodes: ReactNode[] = [];
let index = 0;
while (index < lines.length) {
const line = lines[index];
if (!line.trim()) {
index += 1;
continue;
}
if (line.startsWith('```')) {
const codeLines: string[] = [];
index += 1;
while (index < lines.length && !lines[index].startsWith('```')) {
codeLines.push(lines[index]);
index += 1;
}
nodes.push(
<pre
key={`code-${index}`}
className="m-0 overflow-x-auto rounded-[var(--gl-radius-sm)] bg-[#0f172a] p-3 text-[13px] leading-6 text-[#e2e8f0]"
data-testid="skill-markdown-codeblock"
>
<code>{codeLines.join('\n')}</code>
</pre>,
);
index += 1;
continue;
}
if (line.startsWith('- ')) {
const items: string[] = [];
while (index < lines.length && lines[index].startsWith('- ')) {
items.push(lines[index].slice(2));
index += 1;
}
nodes.push(
<ul key={`list-${index}`} className="m-0 grid gap-1 pl-5">
{items.map((item, itemIndex) => <li key={`${index}-${itemIndex}`}>{renderInline(item, `list-${index}-${itemIndex}`)}</li>)}
</ul>,
);
continue;
}
const paragraphLines = [line];
index += 1;
while (index < lines.length && lines[index].trim() && !lines[index].startsWith('- ') && !lines[index].startsWith('```')) {
paragraphLines.push(lines[index]);
index += 1;
}
nodes.push(renderParagraph(paragraphLines.join(' '), `paragraph-${index}`));
}
return <div className="grid gap-3">{nodes}</div>;
}