Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
more test
Browse files Browse the repository at this point in the history
emmerich committed Apr 13, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 5a37175 commit 7cb8172
Showing 2 changed files with 44 additions and 40 deletions.
82 changes: 43 additions & 39 deletions src/components/DocumentView/CodeBlock/highlight.ts
Original file line number Diff line number Diff line change
@@ -58,50 +58,54 @@ export async function highlight(block: DocumentBlockCode): Promise<HighlightLine
return plainHighlighting(block);
}

const inlines: InlineIndexed[] = [];
const code = getPlainCodeBlock(block, inlines);
return runner.runBlocking(async () => {
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,
};
});
});
}

2 changes: 1 addition & 1 deletion src/lib/async.ts
Original file line number Diff line number Diff line change
@@ -232,7 +232,7 @@ export function singleton<R>(execute: () => Promise<R>): () => Promise<R> {
const states = new WeakMap<object, Promise<R>>();

return async () => {
console.log('cachedResult', cachedResult === UndefinedSymbol);
// console.log('cachedResult', cachedResult === UndefinedSymbol);
if (cachedResult !== UndefinedSymbol) {
// Result is actually shared between requests
return cachedResult;

0 comments on commit 7cb8172

Please sign in to comment.