Skip to content

Commit c7ffeec

Browse files
committed
limit on blocks
1 parent d3ee8de commit c7ffeec

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

src/components/DocumentView/CodeBlock/highlight.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ type PositionedToken = ThemedToken & { start: number; end: number };
3737
*/
3838
let lineCount = 0;
3939
let tokenCount = 0;
40+
let blockCount = 0;
4041
const LINE_LIMIT = 10000;
4142

4243
const runner = asyncMutexFunction();
@@ -60,7 +61,7 @@ export async function highlight(block: DocumentBlockCode): Promise<HighlightLine
6061

6162
return runner.runBlocking(async () => {
6263
const start = Date.now();
63-
console.log(`start ${block.key} ${tokenCount} tokens ${lineCount} lines`)
64+
// console.log(`start ${block.key} ${tokenCount} tokens ${lineCount} lines`)
6465
const inlines: InlineIndexed[] = [];
6566
const code = getPlainCodeBlock(block, inlines);
6667

@@ -71,9 +72,13 @@ export async function highlight(block: DocumentBlockCode): Promise<HighlightLine
7172
const highlighter = await loadHighlighter();
7273
await loadHighlighterLanguage(highlighter, langName);
7374
lineCount += block.nodes.length;
75+
blockCount += 1;
7476

77+
if (blockCount >= 100) {
78+
return plainHighlighting(block);
79+
}
7580

76-
const lines = highlighter.codeToTokensBase(code.replace('\\', '').replaceAll(/[^\x00-\x7F]+/gi, ''), {
81+
const lines = highlighter.codeToTokensBase(code, {
7782
lang: langName,
7883
tokenizeMaxLineLength: 120,
7984
});
@@ -107,9 +112,14 @@ export async function highlight(block: DocumentBlockCode): Promise<HighlightLine
107112
});
108113

109114
const duration = Date.now() - start;
110-
console.log(
111-
`end ${block.key} ${duration}ms code len: ${code.length} lineCountBefore: ${lineCount} tokenCount: ${tokenCount} created lines: ${result.length} langs:${highlighter.getLoadedLanguages().join(',')}`,
112-
);
115+
// if (duration > 150) {
116+
// console.log(`--- start ---`)
117+
// console.log(code);
118+
// console.log(`--- end ---`)
119+
console.log(
120+
`end ${block.key} ${blockCount} ${duration}ms lines:${block.nodes.length} code len: ${code.length} lineCountBefore: ${lineCount} tokenCount: ${tokenCount} created lines: ${result.length} langs:${highlighter.getLoadedLanguages().join(',')}`,
121+
);
122+
// }
113123
return result;
114124
});
115125
}
@@ -340,6 +350,7 @@ const loadHighlighter = singleton(async () => {
340350
// loads it on its own.
341351
//
342352
// Otherwise for Vercel/Cloudflare, we need to load it ourselves.
353+
console.log('load the wasm');
343354
await loadWasm((obj) => WebAssembly.instantiate(onigWasm, obj));
344355
}
345356
const highlighter = await getHighlighter({

0 commit comments

Comments
 (0)