From 7cb8172b70bbbfdd80afc47fb630f3f1fd1fb2c0 Mon Sep 17 00:00:00 2001 From: Steven Hall Date: Sat, 13 Apr 2024 18:32:43 +0100 Subject: [PATCH] more test --- .../DocumentView/CodeBlock/highlight.ts | 82 ++++++++++--------- src/lib/async.ts | 2 +- 2 files changed, 44 insertions(+), 40 deletions(-) diff --git a/src/components/DocumentView/CodeBlock/highlight.ts b/src/components/DocumentView/CodeBlock/highlight.ts index d4902c3ee7..9e6bb2a3b4 100644 --- a/src/components/DocumentView/CodeBlock/highlight.ts +++ b/src/components/DocumentView/CodeBlock/highlight.ts @@ -58,50 +58,54 @@ export async function highlight(block: DocumentBlockCode): Promise { + const inlines: InlineIndexed[] = []; + const code = getPlainCodeBlock(block, inlines); - inlines.sort((a, b) => { - return a.start - b.start; - }); + inlines.sort((a, b) => { + return a.start - b.start; + }); - const lineCountBefore = lineCount; - const highlighter = await loadHighlighter(); - await loadHighlighterLanguage(highlighter, langName); - - const lines = highlighter.codeToTokensBase(code, { - lang: langName, - tokenizeMaxLineLength: 120, - }); - - let currentIndex = 0; - - console.log(`${block.key}${code.length} ${lineCountBefore} ${tokenCount}`); - return lines.map((tokens, index) => { - tokenCount += tokens.length; - const lineBlock = block.nodes[index]; - const result: HighlightToken[] = []; - - const eatToken = (): PositionedToken | null => { - const token = tokens.shift(); - if (token) { - currentIndex += token.content.length; - } - return token - ? { ...token, start: currentIndex - token.content.length, end: currentIndex } - : null; - }; + const lineCountBefore = lineCount; + const highlighter = await loadHighlighter(); + await loadHighlighterLanguage(highlighter, langName); - while (tokens.length > 0) { - result.push(...matchTokenAndInlines(eatToken, inlines)); - } + const lines = highlighter.codeToTokensBase(code, { + lang: langName, + tokenizeMaxLineLength: 120, + }); - currentIndex += 1; // for the \n + let currentIndex = 0; - return { - highlighted: !!lineBlock.data.highlighted, - tokens: result, - }; + console.log( + `${block.key} code len: ${code.length} lineCountBefore: ${lineCountBefore} tokenCount: ${tokenCount}`, + ); + return lines.map((tokens, index) => { + tokenCount += tokens.length; + const lineBlock = block.nodes[index]; + const result: HighlightToken[] = []; + + const eatToken = (): PositionedToken | null => { + const token = tokens.shift(); + if (token) { + currentIndex += token.content.length; + } + return token + ? { ...token, start: currentIndex - token.content.length, end: currentIndex } + : null; + }; + + while (tokens.length > 0) { + result.push(...matchTokenAndInlines(eatToken, inlines)); + } + + currentIndex += 1; // for the \n + + return { + highlighted: !!lineBlock.data.highlighted, + tokens: result, + }; + }); }); } diff --git a/src/lib/async.ts b/src/lib/async.ts index d7b63c6d6b..60e57ce6df 100644 --- a/src/lib/async.ts +++ b/src/lib/async.ts @@ -232,7 +232,7 @@ export function singleton(execute: () => Promise): () => Promise { const states = new WeakMap>(); return async () => { - console.log('cachedResult', cachedResult === UndefinedSymbol); + // console.log('cachedResult', cachedResult === UndefinedSymbol); if (cachedResult !== UndefinedSymbol) { // Result is actually shared between requests return cachedResult;