Skip to content

Commit

Permalink
Test plain highlighting
Browse files Browse the repository at this point in the history
  • Loading branch information
emmerich committed Apr 13, 2024
1 parent dd28b4b commit 1ae437d
Showing 1 changed file with 1 addition and 45 deletions.
46 changes: 1 addition & 45 deletions src/components/DocumentView/CodeBlock/highlight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,51 +31,7 @@ type PositionedToken = ThemedToken & { start: number; end: number };
* Highlight a code block while preserving inline elements.
*/
export async function highlight(block: DocumentBlockCode): Promise<HighlightLine[]> {
const langName = block.data.syntax ? getLanguageForSyntax(block.data.syntax) : null;
if (!langName) {
// Language not found, fallback to plain highlighting
return plainHighlighting(block);
}

const inlines: InlineIndexed[] = [];
const code = getPlainCodeBlock(block, inlines);

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

const highlighter = await loadHighlighter();
await loadHighlighterLanguage(langName);
const lines = highlighter.codeToTokensBase(code, {
lang: langName,
});
let currentIndex = 0;

return lines.map((tokens, index) => {
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,
};
});
return plainHighlighting(block);
}

/**
Expand Down

0 comments on commit 1ae437d

Please sign in to comment.