@@ -10,9 +10,10 @@ import {
10
10
ViewEncapsulation ,
11
11
inject ,
12
12
} from '@angular/core' ;
13
+ import { takeUntilDestroyed } from '@angular/core/rxjs-interop' ;
13
14
import { DomSanitizer , SafeHtml } from '@angular/platform-browser' ;
14
15
import { ActivatedRoute , Data } from '@angular/router' ;
15
- import { Observable , of } from 'rxjs' ;
16
+ import { Observable , from , of } from 'rxjs' ;
16
17
import { catchError , map , mergeMap } from 'rxjs/operators' ;
17
18
18
19
import { AnchorNavigationDirective } from './anchor-navigation.directive' ;
@@ -54,15 +55,7 @@ export default class AnalogMarkdownComponent
54
55
}
55
56
}
56
57
57
- async loadMermaid ( mermaidImport : Promise < typeof import ( 'mermaid' ) > ) {
58
- this . mermaid = await mermaidImport ;
59
- this . mermaid . default . initialize ( { startOnLoad : false } ) ;
60
- // Explicitly running mermaid as ngAfterViewChecked
61
- // has probably already been called
62
- this . zone . runOutsideAngular ( ( ) => this . mermaid ?. default . run ( ) ) ;
63
- }
64
-
65
- ngOnInit ( ) {
58
+ ngOnInit ( ) : void {
66
59
this . updateContent ( ) ;
67
60
}
68
61
@@ -87,4 +80,20 @@ export default class AnalogMarkdownComponent
87
80
this . contentRenderer . enhance ( ) ;
88
81
this . zone . runOutsideAngular ( ( ) => this . mermaid ?. default . run ( ) ) ;
89
82
}
83
+
84
+ private loadMermaid ( mermaidImport : Promise < typeof import ( 'mermaid' ) > ) {
85
+ this . zone . runOutsideAngular ( ( ) =>
86
+ // Wrap into an observable to avoid redundant initialization once
87
+ // the markdown component is destroyed before the promise is resolved.
88
+ from ( mermaidImport )
89
+ . pipe ( takeUntilDestroyed ( ) )
90
+ . subscribe ( ( mermaid ) => {
91
+ this . mermaid = mermaid ;
92
+ this . mermaid . default . initialize ( { startOnLoad : false } ) ;
93
+ // Explicitly running mermaid as ngAfterViewChecked
94
+ // has probably already been called
95
+ this . mermaid ?. default . run ( ) ;
96
+ } )
97
+ ) ;
98
+ }
90
99
}
0 commit comments