Skip to content

Commit 70442d8

Browse files
committed
Cleanup
1 parent bb0a03b commit 70442d8

File tree

1 file changed

+8
-17
lines changed

1 file changed

+8
-17
lines changed

src/components/DocumentView/CodeBlock/highlight.ts

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@ type PositionedToken = ThemedToken & { start: number; end: number };
3232
/**
3333
* Due to a combination of memory limitations of Cloudflare workers and the memory
3434
* cost of shiki, we need to set a limit on the number of lines we can highlight.
35+
*
36+
* This is done per invocation of the Cloudflare worker, so we can store it in-memory.
3537
*/
3638
let lineCount = 0;
37-
const LINE_LIMIT = 2000;
39+
const LINE_LIMIT = 750;
3840

3941
/**
4042
* Highlight a code block while preserving inline elements.
@@ -312,9 +314,11 @@ function cleanupLine(line: string): string {
312314
return line.replace(/\r/g, '');
313315
}
314316

315-
let memoryHighlighter: HighlighterGeneric<any, any> | undefined = undefined;
316-
317-
const loadHighlighterSingleton = singleton(async () => {
317+
/**
318+
* Load the highlighter, only once, and reuse it.
319+
* It makes sure to handle concurrent calls.
320+
*/
321+
const loadHighlighter = singleton(async () => {
318322
return await trace('highlighting.loadHighlighter', async () => {
319323
if (typeof onigWasm !== 'string') {
320324
// When running bun test, the import is a string, we ignore it and let the module
@@ -323,27 +327,14 @@ const loadHighlighterSingleton = singleton(async () => {
323327
// Otherwise for Vercel/Cloudflare, we need to load it ourselves.
324328
await loadWasm((obj) => WebAssembly.instantiate(onigWasm, obj));
325329
}
326-
console.log('getHighlighter');
327330
const highlighter = await getHighlighter({
328331
themes: [createCssVariablesTheme()],
329332
langs: [],
330333
});
331-
memoryHighlighter = highlighter;
332334
return highlighter;
333335
});
334336
});
335337

336-
/**
337-
* Load the highlighter, only once, and reuse it.
338-
* It makes sure to handle concurrent calls.
339-
*/
340-
const loadHighlighter = () => {
341-
if (memoryHighlighter) {
342-
return Promise.resolve(memoryHighlighter);
343-
}
344-
return loadHighlighterSingleton();
345-
};
346-
347338
const loadLanguagesMutex = asyncMutexFunction();
348339
async function loadHighlighterLanguage(
349340
highlighter: HighlighterGeneric<keyof typeof bundledLanguages, keyof typeof bundledThemes>,

0 commit comments

Comments
 (0)