返回 DeepSeek-Reasonix
highlightLines.ts
根目录 / desktop / frontend / src / lib / highlightLines.ts
1 // A highlight.js span can cross newlines. Balance it at each line boundary so
2 // React can retain unchanged line hosts without losing multiline token styles.
3 export function splitHighlightedCodeLines(html: string): string[] {
4 const lines: string[] = [];
5 const openTags: string[] = [];
6 let current = "";
7 let offset = 0;
8 const closeTags = () => [...openTags].reverse()
9 .map(tag => tag.startsWith("<mark") ? "</mark>" : "</span>").join("");
10 while (offset < html.length) {
11 if (html[offset] === "\n") {
12 lines.push(current + closeTags());
13 current = openTags.join("");
14 offset += 1;
15 continue;
16 }
17 if (html[offset] === "<") {
18 const end = html.indexOf(">", offset);
19 if (end !== -1) {
20 const tag = html.slice(offset, end + 1);
21 current += tag;
22 if (/^<(span|mark)\b/.test(tag)) openTags.push(tag);
23 else if (/^<\/(span|mark)>$/.test(tag)) openTags.pop();
24 offset = end + 1;
25 continue;
26 }
27 }
28 const length = (html.codePointAt(offset) ?? 0) > 0xffff ? 2 : 1;
29 current += html.slice(offset, offset + length);
30 offset += length;
31 }
32 lines.push(current + closeTags());
33 return lines;
34 }
35
35 lines TYPESCRIPT