Skip to content

Commit 6657b46

Browse files
authored
Merge pull request #3112 from 4Science/task/main/DURACOM-273
[#3111] fix markdown rendering applying before mathjax rendering
2 parents 1efa886 + 66e1a24 commit 6657b46

File tree

5 files changed

+31
-21
lines changed

5 files changed

+31
-21
lines changed

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

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

3232
protected mathJaxOptions = {
3333
tex: {
34-
inlineMath: [['$', '$'], ['\\(', '\\)']],
34+
inlineMath: [['$', '$'], ['$$', '$$'], ['\\(', '\\)']],
3535
},
3636
svg: {
3737
fontCache: 'global',
@@ -108,7 +108,7 @@ export class ClientMathService extends MathService {
108108
*/
109109
render(element: HTMLElement) {
110110
if (environment.markdown.mathjax) {
111-
this._window.nativeWindow.MathJax.typesetPromise([element]);
111+
return (window as any).MathJax.typesetPromise([element]) as Promise<any>;
112112
}
113113
}
114114
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ export class MockMathService extends MathService {
2222
return of(true);
2323
}
2424

25-
render(element: HTMLElement): void {
26-
return;
25+
render(element: HTMLElement): Promise<any> {
26+
return Promise.resolve();
2727
}
2828
}
2929

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
@@ -47,6 +47,6 @@ export class ServerMathService extends MathService {
4747
}
4848

4949
render(element: HTMLElement) {
50-
return;
50+
return Promise.resolve();
5151
}
5252
}

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

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)