mirror of
https://github.com/massbug/judge4c.git
synced 2025-05-17 23:12:23 +00:00
refactor(shiki): migrate to shiki/core with lazy initialization
- Replace shiki with shiki/core for better tree-shaking - Change highlighter initialization to explicit theme/lang imports - Export getHighlighter function instead of direct highlighter instance - Add error handling for uninitialized highlighter access
This commit is contained in:
parent
2efdc21419
commit
cea0027799
@ -1,25 +1,29 @@
|
|||||||
import { MonacoTheme } from "@/types/monaco-theme";
|
import {
|
||||||
import { EditorLanguage } from "@/generated/client";
|
createHighlighterCore,
|
||||||
import { createHighlighter, Highlighter } from "shiki";
|
createOnigurumaEngine,
|
||||||
|
HighlighterCore,
|
||||||
|
} from "shiki";
|
||||||
|
|
||||||
// Get all values from the ProgrammingLanguage and Theme enums
|
let highlighter: HighlighterCore;
|
||||||
const themes = Object.values(MonacoTheme);
|
|
||||||
const languages = [...Object.values(EditorLanguage), "markdown"];
|
|
||||||
|
|
||||||
// Use lazy initialization for highlighter
|
const initHighlighter = async () => {
|
||||||
let highlighter: Highlighter;
|
highlighter = await createHighlighterCore({
|
||||||
|
themes: [
|
||||||
|
import("@shikijs/themes/github-light-default"),
|
||||||
|
import("@shikijs/themes/github-dark-default"),
|
||||||
|
],
|
||||||
|
langs: [import("@shikijs/langs/c"), import("@shikijs/langs/cpp")],
|
||||||
|
engine: createOnigurumaEngine(import("shiki/wasm")),
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
const initializeHighlighter = async () => {
|
initHighlighter();
|
||||||
try {
|
|
||||||
highlighter = await createHighlighter({
|
export const getHighlighter = () => {
|
||||||
themes: themes, // Use all values from the Theme enum
|
if (!highlighter) {
|
||||||
langs: languages, // Use all values from the ProgrammingLanguage enum
|
throw new Error(
|
||||||
});
|
"Highlighter not initialized yet! Call initHighlighter() first."
|
||||||
} catch (error) {
|
);
|
||||||
console.error("Error initializing highlighter:", error);
|
|
||||||
}
|
}
|
||||||
}
|
return highlighter;
|
||||||
|
};
|
||||||
initializeHighlighter();
|
|
||||||
|
|
||||||
export { highlighter };
|
|
||||||
|
Loading…
Reference in New Issue
Block a user