Skip to content

Commit 5ac8f31

Browse files
author
Andrea Barbasso
committed
[DSC-1768] fix markdown rendering applying before mathjax rendering
1 parent 1b19d0f commit 5ac8f31

5 files changed

Lines changed: 26 additions & 18 deletions

File tree

src/app/core/shared/client-math.service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export class ClientMathService extends MathService {
2222

2323
protected mathJaxOptions = {
2424
tex: {
25-
inlineMath: [['$', '$'], ['\\(', '\\)']]
25+
inlineMath: [['$', '$'], ['$$', '$$'], ['\\(', '\\)']]
2626
},
2727
svg: {
2828
fontCache: 'global'
@@ -99,7 +99,7 @@ export class ClientMathService extends MathService {
9999
*/
100100
render(element: HTMLElement) {
101101
if (environment.markdown.mathjax) {
102-
this._window.nativeWindow.MathJax.typesetPromise([element]);
102+
return (window as any).MathJax.typesetPromise([element]) as Promise<any>;
103103
}
104104
}
105105
}

src/app/core/shared/math.service.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ export class MockMathService extends MathService {
1515
return of(true);
1616
}
1717

18-
render(element: HTMLElement): void {
19-
return;
18+
render(element: HTMLElement): Promise<any> {
19+
return Promise.resolve();
2020
}
2121
}
2222

src/app/core/shared/math.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ export abstract class MathService {
1515

1616
protected abstract registerMathJaxAsync(config: MathJaxConfig): Promise<any>;
1717
abstract ready(): Observable<boolean>;
18-
abstract render(element: HTMLElement): void;
18+
abstract render(element: HTMLElement): Promise<any>;
1919
}

src/app/core/shared/server-math.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,6 @@ export class ServerMathService extends MathService {
3939
}
4040

4141
render(element: HTMLElement) {
42-
return;
42+
return Promise.resolve();
4343
}
4444
}

src/app/shared/utils/markdown.directive.ts

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,31 +60,39 @@ export class MarkdownDirective implements OnChanges, OnDestroy {
6060
this.el.innerHTML = value;
6161
return;
6262
} else {
63-
const MarkdownIt = await this.markdownIt;
64-
const md = new MarkdownIt({
65-
html: true,
66-
linkify: true,
67-
});
68-
69-
const html = this.sanitizer.sanitize(SecurityContext.HTML, md.render(value));
70-
this.el.innerHTML = html;
71-
7263
if (environment.markdown.mathjax) {
73-
this.renderMathjax();
64+
this.renderMathjaxThenMarkdown(value);
65+
} else {
66+
this.renderMarkdown(value);
7467
}
7568
}
7669
}
7770

78-
private renderMathjax() {
71+
private renderMathjaxThenMarkdown(value: string) {
72+
const sanitized = this.sanitizer.sanitize(SecurityContext.HTML, value);
73+
this.el.innerHTML = sanitized;
7974
this.mathService.ready().pipe(
8075
filter((ready) => ready),
8176
take(1),
8277
takeUntil(this.alive$)
8378
).subscribe(() => {
84-
this.mathService.render(this.el);
79+
this.mathService.render(this.el)?.then(_ => {
80+
this.renderMarkdown(this.el.innerHTML, true);
81+
});
8582
});
8683
}
8784

85+
private async renderMarkdown(value: string, alreadySanitized = false) {
86+
const MarkdownIt = await this.markdownIt;
87+
const md = new MarkdownIt({
88+
html: true,
89+
linkify: true,
90+
});
91+
92+
const html = alreadySanitized ? md.render(value) : this.sanitizer.sanitize(SecurityContext.HTML, md.render(value));
93+
this.el.innerHTML = html;
94+
}
95+
8896
ngOnDestroy() {
8997
this.alive$.next(false);
9098
}

0 commit comments

Comments
 (0)