@@ -55,32 +55,42 @@ export class MarkdownDirective implements OnInit, OnDestroy {
5555
5656 async render ( value : string , forcePreview = false ) : Promise < SafeHtml > {
5757 if ( isEmpty ( value ) || ( ! environment . markdown . enabled && ! forcePreview ) ) {
58- return value ;
59- }
60- const MarkdownIt = await this . markdownIt ;
61- const md = new MarkdownIt ( {
62- html : true ,
63- linkify : true ,
64- } ) ;
65-
66- const html = this . sanitizer . sanitize ( SecurityContext . HTML , md . render ( value ) ) ;
67- this . el . innerHTML = html ;
68-
69- if ( environment . markdown . mathjax ) {
70- this . renderMathjax ( ) ;
58+ this . el . innerHTML = value ;
59+ return ;
60+ } else {
61+ if ( environment . markdown . mathjax ) {
62+ this . renderMathjaxThenMarkdown ( value ) ;
63+ } else {
64+ this . renderMarkdown ( value ) ;
65+ }
7166 }
7267 }
7368
74- private renderMathjax ( ) {
69+ private renderMathjaxThenMarkdown ( value : string ) {
70+ const sanitized = this . sanitizer . sanitize ( SecurityContext . HTML , value ) ;
71+ this . el . innerHTML = sanitized ;
7572 this . mathService . ready ( ) . pipe (
7673 filter ( ( ready ) => ready ) ,
7774 take ( 1 ) ,
7875 takeUntil ( this . alive$ ) ,
7976 ) . subscribe ( ( ) => {
80- this . mathService . render ( this . el ) ;
77+ this . mathService . render ( this . el ) ?. then ( _ => {
78+ this . renderMarkdown ( this . el . innerHTML , true ) ;
79+ } ) ;
8180 } ) ;
8281 }
8382
83+ private async renderMarkdown ( value : string , alreadySanitized = false ) {
84+ const MarkdownIt = await this . markdownIt ;
85+ const md = new MarkdownIt ( {
86+ html : true ,
87+ linkify : true ,
88+ } ) ;
89+
90+ const html = alreadySanitized ? md . render ( value ) : this . sanitizer . sanitize ( SecurityContext . HTML , md . render ( value ) ) ;
91+ this . el . innerHTML = html ;
92+ }
93+
8494 ngOnDestroy ( ) {
8595 this . alive$ . next ( false ) ;
8696 }
0 commit comments