Open History to see the last commit
1import type { MarkdownHighlightedToken } from "./SelectableMarkdownText.types";
2
3/** Keep finished lines colored while the current line awaits highlighting. */
4export function pendingCodeHighlight(
5 previousCode: string,
6 code: string,
7 tokens: ReadonlyArray<ReadonlyArray<MarkdownHighlightedToken>>,
8): ReadonlyArray<ReadonlyArray<MarkdownHighlightedToken>> | null {
9 const end = previousCode.lastIndexOf("\n") + 1;
10 if (!end || !code.startsWith(previousCode.slice(0, end))) return null;
11 const completedLines = previousCode.slice(0, end).split("\n").length - 1;
12 return [
13 ...tokens.slice(0, completedLines),
14 ...code
15 .slice(end)
16 .split("\n")
17 .map((content) => [{ content, color: null, fontStyle: null }]),
18 ];
19}