-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathshikiloader.ts
45 lines (41 loc) · 1.4 KB
/
shikiloader.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import ExecutionEnvironment from "@docusaurus/ExecutionEnvironment";
import { LanguageRegistration, createHighlighter } from "shiki";
import { targets } from "@site/src/components/LinguaFrancaMultiTargetUtils";
import LFTextMateLanguageDefinition from "./lflang.tmLanguage.json";
import { HighlighterGeneric, BuiltinLanguage, BuiltinTheme } from "shiki";
declare global {
interface Window {
LFWebsite: {
shikijiInstance?: HighlighterGeneric<BuiltinLanguage, BuiltinTheme>;
};
}
}
export const loadShiki = async () => {
if (window.LFWebsite?.shikijiInstance != null) {
return window.LFWebsite.shikijiInstance;
}
const shiki = await createHighlighter({
themes: ["material-theme-lighter", "material-theme-darker"],
langs: [
...targets,
// I can't get TS not complain, but trust me bro! It will work......
LFTextMateLanguageDefinition as unknown as LanguageRegistration,
],
});
if (window.LFWebsite == null) {
window.LFWebsite = {};
}
window.LFWebsite.shikijiInstance = shiki;
return shiki;
};
if (ExecutionEnvironment.canUseDOM) {
// As soon as the site loads in the browser, check if a Shikiji instance is created. If not, create one and save to global.
// Don't catch. Let it throw~ let it throw~
loadShiki().then((v) => {
if (v != null) {
console.log("Shiki loaded.");
} else {
throw Error("Shiki did loaded correctly.");
}
});
}