1
1
import { Injectable , Inject , PLATFORM_ID , Optional } from '@angular/core' ;
2
2
import { DOCUMENT , isPlatformBrowser } from '@angular/common' ;
3
- import { BehaviorSubject , Observable , from , EMPTY } from 'rxjs' ;
3
+ import { BehaviorSubject , Observable , from , EMPTY , zip } from 'rxjs' ;
4
4
import { catchError , tap , map , switchMap , filter , take } from 'rxjs/operators' ;
5
5
import { HIGHLIGHT_OPTIONS , HighlightLibrary , HighlightOptions } from './highlight.model' ;
6
6
@@ -24,13 +24,13 @@ export class HighlightLoader {
24
24
this . _ready . next ( doc . defaultView . hljs ) ;
25
25
} else {
26
26
// Load hljs library
27
- this . _loadHighlightLibrary ( ) . pipe (
27
+ this . _loadLibrary ( ) . pipe (
28
28
switchMap ( ( hljs : HighlightLibrary ) => {
29
29
if ( this . _options . lineNumbers ) {
30
30
// Make hljs available on window object (required for the line numbers library)
31
31
doc . defaultView . hljs = hljs ;
32
32
// Load line numbers library
33
- return this . _loadLineNumbers ( ) . pipe ( tap ( ( ) => this . _ready . next ( hljs ) ) ) ;
33
+ return loadLineNumbers ( ) . pipe ( tap ( ( ) => this . _ready . next ( hljs ) ) ) ;
34
34
} else {
35
35
this . _ready . next ( hljs ) ;
36
36
return EMPTY ;
@@ -47,23 +47,59 @@ export class HighlightLoader {
47
47
/**
48
48
* Lazy-Load highlight.js library
49
49
*/
50
- private _loadHighlightLibrary ( ) : Observable < HighlightLibrary > {
51
- return this . importModule ( import ( 'highlight.js' ) ) ;
50
+ private _loadLibrary ( ) : Observable < HighlightLibrary > {
51
+ const core = loadCoreLibrary ( ) . pipe (
52
+ switchMap ( ( hljs : HighlightLibrary ) =>
53
+ this . _loadLanguages ( hljs ) . pipe ( map ( ( ) => hljs ) )
54
+ )
55
+ ) ;
56
+ const all = loadAllLibrary ( ) ;
57
+ return ( this . _options && this . _options . languages && Object . keys ( this . _options . languages ) . length ) ? core : all ;
52
58
}
53
59
54
60
/**
55
- * Lazy-Load highlight.js library
61
+ * Lazy-load highlight.js languages
56
62
*/
57
- private _loadLineNumbers ( ) : Observable < HighlightLibrary > {
58
- return this . importModule ( import ( 'highlightjs-line-numbers.js' ) ) ;
59
- }
60
-
61
- private importModule ( loader : Promise < any > ) : Observable < any > {
62
- return from ( loader ) . pipe (
63
- filter ( ( module : any ) => ! ! module && ! ! module . default ) ,
64
- map ( ( module : any ) => module . default )
63
+ private _loadLanguages ( hljs : HighlightLibrary ) : Observable < any > {
64
+ const languages = Object . entries ( this . _options . languages ) . map ( ( [ langName , langLoader ] ) =>
65
+ importModule ( langLoader ( ) ) . pipe (
66
+ tap ( ( langFunc : any ) => {
67
+ console . log ( 'register lang' , langName , langFunc ) ;
68
+ hljs . registerLanguage ( langName , langFunc ) ;
69
+ } )
70
+ )
65
71
) ;
72
+ return zip ( ...languages ) ;
66
73
}
74
+ }
67
75
76
+ /**
77
+ * Import highlight.js library with all languages
78
+ */
79
+ function loadAllLibrary ( ) : Observable < HighlightLibrary > {
80
+ return importModule ( import ( 'highlight.js' ) ) ;
81
+ }
82
+
83
+ /**
84
+ * Import highlight.js core library
85
+ */
86
+ function loadCoreLibrary ( ) : Observable < HighlightLibrary > {
87
+ return importModule ( import ( 'highlight.js/lib/highlight' ) ) ;
88
+ }
68
89
90
+ /**
91
+ * Import line numbers library
92
+ */
93
+ function loadLineNumbers ( ) : Observable < any > {
94
+ return importModule ( import ( 'highlightjs-line-numbers.js' ) ) ;
95
+ }
96
+
97
+ /**
98
+ * Map loader response to module object
99
+ */
100
+ function importModule ( moduleLoader : Promise < any > ) : Observable < any > {
101
+ return from ( moduleLoader ) . pipe (
102
+ filter ( ( module : any ) => ! ! module && ! ! module . default ) ,
103
+ map ( ( module : any ) => module . default )
104
+ ) ;
69
105
}
0 commit comments