@@ -27,6 +27,9 @@ interface LightweightCodeHighlighter {
27
27
renderWhitespace ?: boolean ;
28
28
}
29
29
30
+ // The maximum number of characters per line that we will display in the preview.
31
+ const MAX_NUMBER_OF_CHARACTER_PER_LINE = 1000 ;
32
+
30
33
/**
31
34
* Lightweight code highlighter that uses the Lezer parser to highlight code.
32
35
* This is helpful in scenarios where we need to highlight a ton of code snippets
@@ -49,12 +52,19 @@ export const LightweightCodeHighlighter = memo<LightweightCodeHighlighter>((prop
49
52
return code . trimEnd ( ) . split ( '\n' ) ;
50
53
} , [ code ] ) ;
51
54
55
+ const isFileTooLargeToDisplay = useMemo ( ( ) => {
56
+ return unhighlightedLines . some ( line => line . length > MAX_NUMBER_OF_CHARACTER_PER_LINE ) ;
57
+ } , [ code ] ) ;
52
58
53
59
const [ highlightedLines , setHighlightedLines ] = useState < React . ReactNode [ ] | null > ( null ) ;
54
60
55
61
const highlightStyle = useCodeMirrorHighlighter ( ) ;
56
62
57
63
useEffect ( ( ) => {
64
+ if ( isFileTooLargeToDisplay ) {
65
+ return ;
66
+ }
67
+
58
68
measure ( ( ) => Promise . all (
59
69
unhighlightedLines
60
70
. map ( async ( line , index ) => {
@@ -103,12 +113,21 @@ export const LightweightCodeHighlighter = memo<LightweightCodeHighlighter>((prop
103
113
const lineNumberDigits = String ( lineCount ) . length ;
104
114
const lineNumberWidth = `${ lineNumberDigits + 2 } ch` ; // +2 for padding
105
115
116
+ if ( isFileTooLargeToDisplay ) {
117
+ return (
118
+ < div className = "font-mono text-sm px-2" >
119
+ File too large to display in preview.
120
+ </ div >
121
+ ) ;
122
+ }
123
+
106
124
return (
107
125
< div
108
126
style = { {
109
127
fontFamily : tailwind . theme . fontFamily . editor ,
110
128
fontSize : tailwind . theme . fontSize . editor ,
111
129
whiteSpace : renderWhitespace ? 'pre-wrap' : 'none' ,
130
+ wordBreak : 'break-all' ,
112
131
} }
113
132
>
114
133
{ ( highlightedLines ?? unhighlightedLines ) . map ( ( line , index ) => (
0 commit comments