Skip to content

Commit 2f68e16

Browse files
authored
fix(CodeBlock): resolved async import loading for CodeBlockHighlight (#672)
1 parent 689cc20 commit 2f68e16

File tree

1 file changed

+45
-25
lines changed

1 file changed

+45
-25
lines changed

src/extensions/markdown/CodeBlock/CodeBlockHighlight/CodeBlockHighlight.ts

+45-25
Original file line numberDiff line numberDiff line change
@@ -37,42 +37,58 @@ export const CodeBlockHighlight: ExtensionAuto<CodeBlockHighlightOptions> = (bui
3737
let lowlight: Lowlight;
3838
let hljs: typeof HLJS;
3939

40-
try {
41-
hljs = require('highlight.js/lib/core');
42-
const low = require('lowlight');
43-
const all: HighlightLangMap = low.all;
44-
const create: typeof createLowlight = low.createLowlight;
45-
langs = {...all, ...opts.langs};
46-
lowlight = create(langs);
47-
} catch (e) {
48-
globalLogger.info('Skip code_block highlighting');
49-
builder.logger.log('Skip code_block highlighting');
50-
return;
51-
}
40+
const loadModules = async () => {
41+
try {
42+
hljs = (await import('highlight.js/lib/core')).default;
43+
const low = await import('lowlight');
44+
45+
const all: HighlightLangMap = low.all;
46+
const create: typeof createLowlight = low.createLowlight;
47+
langs = {...all, ...opts.langs};
48+
lowlight = create(langs);
49+
return true;
50+
} catch (e) {
51+
globalLogger.info('Skip code_block highlighting');
52+
builder.logger.log('Skip code_block highlighting');
53+
return false;
54+
}
55+
};
5256

5357
builder.addPlugin(() => {
58+
let modulesLoaded = false;
59+
5460
const selectItems: LangSelectItem[] = [];
5561
const mapping: Record<string, string> = {};
56-
for (const lang of Object.keys(langs)) {
57-
const defs = langs[lang](hljs);
58-
selectItems.push({
59-
value: lang,
60-
content: defs.name || capitalize(lang),
61-
});
62-
if (defs.aliases) {
63-
for (const alias of defs.aliases) {
64-
mapping[alias] = lang;
65-
}
66-
}
67-
}
6862

6963
// TODO: add TAB key handler
7064
// TODO: Remove constant selection of block
7165
return new Plugin<DecorationSet>({
7266
key,
7367
state: {
74-
init: (_, state) => getDecorations(state.doc),
68+
init: (_, state) => {
69+
loadModules().then(() => {
70+
modulesLoaded = true;
71+
72+
for (const lang of Object.keys(langs)) {
73+
const defs = langs[lang](hljs);
74+
selectItems.push({
75+
value: lang,
76+
content: defs.name || capitalize(lang),
77+
});
78+
if (defs.aliases) {
79+
for (const alias of defs.aliases) {
80+
mapping[alias] = lang;
81+
}
82+
}
83+
}
84+
});
85+
return getDecorations(state.doc);
86+
},
7587
apply: (tr, decos, oldState, newState) => {
88+
if (!modulesLoaded) {
89+
return DecorationSet.empty;
90+
}
91+
7692
if (tr.docChanged) {
7793
const oldNodeName = oldState.selection.$head.parent.type.name;
7894
const newNodeName = newState.selection.$head.parent.type.name;
@@ -168,6 +184,10 @@ export const CodeBlockHighlight: ExtensionAuto<CodeBlockHighlightOptions> = (bui
168184
function getDecorations(doc: Node) {
169185
const decos: Decoration[] = [];
170186

187+
if (!lowlight) {
188+
return DecorationSet.empty;
189+
}
190+
171191
for (const {node, pos} of findChildrenByType(doc, codeBlockType(doc.type.schema), true)) {
172192
let from = pos + 1;
173193
let nodes: Root['children'];

0 commit comments

Comments
 (0)