|
1 | 1 | import { use, type CSSProperties } from "react"; |
2 | 2 |
|
| 3 | +import { |
| 4 | + withEmbeddedScripts, |
| 5 | + type EmbeddedScript, |
| 6 | + type SyntaxToken, |
| 7 | +} from "@t3tools/client-runtime/work-log/embedded-scripts"; |
| 8 | + |
3 | 9 | import { resolveDiffThemeName } from "../../lib/diffRendering"; |
4 | | -import type { EmbeddedScript } from "../../lib/embeddedScripts"; |
5 | 10 | import { getSyntaxHighlighterPromise } from "../../lib/syntaxHighlighting"; |
6 | 11 |
|
7 | | -interface SyntaxToken { |
8 | | - readonly content: string; |
9 | | - readonly offset: number; |
10 | | - readonly color?: string; |
11 | | - readonly fontStyle?: number; |
12 | | -} |
13 | | - |
14 | | -type TokenLines = ReadonlyArray<ReadonlyArray<SyntaxToken>>; |
15 | | - |
16 | 12 | const NO_EMBEDDED_SCRIPTS: ReadonlyArray<EmbeddedScript> = []; |
17 | 13 |
|
18 | 14 | function syntaxTokenStyle(token: SyntaxToken): CSSProperties { |
@@ -80,66 +76,6 @@ export function HighlightedTokens({ |
80 | 76 | ); |
81 | 77 | } |
82 | 78 |
|
83 | | -export function withEmbeddedScripts( |
84 | | - code: string, |
85 | | - lines: TokenLines, |
86 | | - embedded: ReadonlyArray<EmbeddedScript>, |
87 | | - tokenize: (text: string, language: string) => TokenLines, |
88 | | -): TokenLines { |
89 | | - if (embedded.length === 0) return lines; |
90 | | - const styles = Array.from<SyntaxToken | undefined>({ length: code.length }); |
91 | | - for (const token of lines.flat()) { |
92 | | - styles.fill(token, token.offset, token.offset + token.content.length); |
93 | | - } |
94 | | - for (const script of embedded) { |
95 | | - let scriptLines: TokenLines; |
96 | | - try { |
97 | | - scriptLines = tokenize(script.text, script.language); |
98 | | - } catch { |
99 | | - continue; |
100 | | - } |
101 | | - for (const token of scriptLines.flat()) { |
102 | | - if (token.content === "") continue; |
103 | | - const last = token.offset + token.content.length - 1; |
104 | | - styles.fill(token, script.starts[token.offset], script.ends[last]); |
105 | | - } |
106 | | - } |
107 | | - return restyledLines(code, styles); |
108 | | -} |
109 | | - |
110 | | -function restyledLines( |
111 | | - code: string, |
112 | | - styles: ReadonlyArray<SyntaxToken | undefined>, |
113 | | -): SyntaxToken[][] { |
114 | | - const lines: SyntaxToken[][] = []; |
115 | | - let start = 0; |
116 | | - for (const lineBreak of [...code.matchAll(/\r?\n/gu), undefined]) { |
117 | | - const end = lineBreak?.index ?? code.length; |
118 | | - const tokens: SyntaxToken[] = []; |
119 | | - for (let index = start; index < end;) { |
120 | | - const style = styles[index]; |
121 | | - let next = index + 1; |
122 | | - while ( |
123 | | - next < end && |
124 | | - styles[next]?.color === style?.color && |
125 | | - styles[next]?.fontStyle === style?.fontStyle |
126 | | - ) { |
127 | | - next += 1; |
128 | | - } |
129 | | - tokens.push({ |
130 | | - content: code.slice(index, next), |
131 | | - offset: index, |
132 | | - ...(style?.color ? { color: style.color } : {}), |
133 | | - ...(style?.fontStyle ? { fontStyle: style.fontStyle } : {}), |
134 | | - }); |
135 | | - index = next; |
136 | | - } |
137 | | - lines.push(tokens); |
138 | | - if (lineBreak) start = lineBreak.index + lineBreak[0].length; |
139 | | - } |
140 | | - return lines; |
141 | | -} |
142 | | - |
143 | 79 | /** Restore CRLF from source; Shiki strips both line-ending forms. */ |
144 | 80 | export function keyedLines<Token extends SyntaxToken>( |
145 | 81 | code: string, |
|
0 commit comments