Skip to content

Commit c393b28

Browse files
committed
debug loaded langs
1 parent 6a0f68c commit c393b28

File tree

1 file changed

+32
-33
lines changed

1 file changed

+32
-33
lines changed

src/components/DocumentView/CodeBlock/highlight.ts

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -59,57 +59,56 @@ export async function highlight(block: DocumentBlockCode): Promise<HighlightLine
5959
// }
6060

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

6667
inlines.sort((a, b) => {
6768
return a.start - b.start;
6869
});
6970

70-
const highlighter = await loadHighlighter(tokenCount > 4000);
71+
const highlighter = await loadHighlighter();
7172
await loadHighlighterLanguage(highlighter, langName);
7273
lineCount += block.nodes.length;
7374

74-
const start = Date.now();
75+
7576
const lines = highlighter.codeToTokensBase(code, {
7677
lang: langName,
7778
tokenizeMaxLineLength: 120,
7879
});
79-
const duration = Date.now() - start;
8080

8181
let currentIndex = 0;
82-
const result = plainHighlighting(block);
83-
// const result = lines.map((tokens, index) => {
84-
// tokenCount += tokens.length;
85-
// const lineBlock = block.nodes[index];
86-
// const result: HighlightToken[] = [];
87-
88-
// const eatToken = (): PositionedToken | null => {
89-
// const token = tokens.shift();
90-
// if (token) {
91-
// currentIndex += token.content.length;
92-
// }
93-
// return token
94-
// ? { ...token, start: currentIndex - token.content.length, end: currentIndex }
95-
// : null;
96-
// };
97-
98-
// while (tokens.length > 0) {
99-
// result.push(...matchTokenAndInlines(eatToken, inlines));
100-
// }
101-
102-
// currentIndex += 1; // for the \n
103-
104-
// return {
105-
// highlighted: !!lineBlock.data.highlighted,
106-
// tokens: result,
107-
// };
108-
// });
82+
const result = lines.map((tokens, index) => {
83+
tokenCount += tokens.length;
84+
const lineBlock = block.nodes[index];
85+
const result: HighlightToken[] = [];
86+
87+
const eatToken = (): PositionedToken | null => {
88+
const token = tokens.shift();
89+
if (token) {
90+
currentIndex += token.content.length;
91+
}
92+
return token
93+
? { ...token, start: currentIndex - token.content.length, end: currentIndex }
94+
: null;
95+
};
96+
97+
while (tokens.length > 0) {
98+
result.push(...matchTokenAndInlines(eatToken, inlines));
99+
}
109100

110-
101+
currentIndex += 1; // for the \n
102+
103+
return {
104+
highlighted: !!lineBlock.data.highlighted,
105+
tokens: result,
106+
};
107+
});
108+
109+
const duration = Date.now() - start;
111110
console.log(
112-
`end ${block.key} ${duration}ms code len: ${code.length} lineCountBefore: ${lineCount} tokenCount: ${tokenCount} created lines: ${result.length}`,
111+
`end ${block.key} ${duration}ms code len: ${code.length} lineCountBefore: ${lineCount} tokenCount: ${tokenCount} created lines: ${result.length} langs:${highlighter.getLoadedLanguages().join(',')}`,
113112
);
114113
return result;
115114
});

0 commit comments

Comments
 (0)