-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeBlock.tsx
More file actions
117 lines (98 loc) · 2.63 KB
/
Copy pathCodeBlock.tsx
File metadata and controls
117 lines (98 loc) · 2.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
import { Box, Text } from 'ink';
import { memo, useEffect, useState } from 'react';
import { ROLE } from '../../constants';
interface CodeBlockProps {
code: string;
language?: string;
role: string;
}
const highlightCache = new Map<string, string>();
const CODE_BLOCK_REGEX =
/^(?<indent>[ \t]*)(`{3,})(\w+)?[ \t]*\n([\s\S]*?)^\k<indent>\2[ \t]*$/gm;
export function normalizeCodeBlockContent(
content: string,
indent = '',
): string {
if (!indent) {
return content.trim();
}
const indentPattern = new RegExp(`^${indent}`, 'gm');
return content.replace(indentPattern, '').trim();
}
export async function prewarmCodeBlocks(content: string): Promise<void> {
const promises: Promise<void>[] = [];
let match;
CODE_BLOCK_REGEX.lastIndex = 0;
while ((match = CODE_BLOCK_REGEX.exec(content)) !== null) {
const indent = match[1];
const language = match[3];
const code = normalizeCodeBlockContent(match[4], indent);
// v8 ignore next 2
if (code) {
promises.push(prewarmHighlight(code, language));
}
}
await Promise.all(promises);
}
export async function prewarmHighlight(
code: string,
language?: string,
): Promise<void> {
// v8 ignore start
const cacheKey = `${language ?? ''}:${code}`;
if (highlightCache.has(cacheKey)) {
return;
}
// v8 ignore stop
const result = await highlightCode(code, language);
highlightCache.set(cacheKey, result);
}
async function highlightCode(code: string, language = 'text'): Promise<string> {
const { codeToANSI } = await import('@shikijs/cli');
try {
return await codeToANSI(code, language as never, 'github-light');
} catch {
// v8 ignore next
return code;
}
}
export const CodeBlock = memo(function CodeBlock({
code,
language,
role,
}: CodeBlockProps) {
const cacheKey = `${language ?? ''}:${code}`;
const [highlighted, setHighlighted] = useState<string>(
() => highlightCache.get(cacheKey) ?? code,
);
useEffect(() => {
let canceled = false;
async function loadHighlight() {
try {
const result = await highlightCode(code, language);
highlightCache.set(cacheKey, result);
if (!canceled) {
setHighlighted(result);
}
} catch {
// Keep plain code on error
}
}
void loadHighlight();
return () => {
canceled = true;
};
}, [cacheKey, code, language]);
const isSystem = role === ROLE.SYSTEM;
return (
<Box
flexDirection="column"
borderStyle="bold"
borderColor={isSystem ? 'gray' : 'dim'}
paddingX={1}
marginY={1}
>
<Text dimColor={isSystem}>{highlighted}</Text>
</Box>
);
});