@@ -37,42 +37,58 @@ export const CodeBlockHighlight: ExtensionAuto<CodeBlockHighlightOptions> = (bui
37
37
let lowlight : Lowlight ;
38
38
let hljs : typeof HLJS ;
39
39
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
+ } ;
52
56
53
57
builder . addPlugin ( ( ) => {
58
+ let modulesLoaded = false ;
59
+
54
60
const selectItems : LangSelectItem [ ] = [ ] ;
55
61
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
- }
68
62
69
63
// TODO: add TAB key handler
70
64
// TODO: Remove constant selection of block
71
65
return new Plugin < DecorationSet > ( {
72
66
key,
73
67
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
+ } ,
75
87
apply : ( tr , decos , oldState , newState ) => {
88
+ if ( ! modulesLoaded ) {
89
+ return DecorationSet . empty ;
90
+ }
91
+
76
92
if ( tr . docChanged ) {
77
93
const oldNodeName = oldState . selection . $head . parent . type . name ;
78
94
const newNodeName = newState . selection . $head . parent . type . name ;
@@ -168,6 +184,10 @@ export const CodeBlockHighlight: ExtensionAuto<CodeBlockHighlightOptions> = (bui
168
184
function getDecorations ( doc : Node ) {
169
185
const decos : Decoration [ ] = [ ] ;
170
186
187
+ if ( ! lowlight ) {
188
+ return DecorationSet . empty ;
189
+ }
190
+
171
191
for ( const { node, pos} of findChildrenByType ( doc , codeBlockType ( doc . type . schema ) , true ) ) {
172
192
let from = pos + 1 ;
173
193
let nodes : Root [ 'children' ] ;
0 commit comments