Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Port dspace-8_x] [#3111] fix markdown rendering applying before mathjax rendering #3218

Merged
merged 1 commit into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/app/core/shared/client-math.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class ClientMathService extends MathService {

protected mathJaxOptions = {
tex: {
inlineMath: [['$', '$'], ['\\(', '\\)']],
inlineMath: [['$', '$'], ['$$', '$$'], ['\\(', '\\)']],
},
svg: {
fontCache: 'global',
Expand Down Expand Up @@ -108,7 +108,7 @@ export class ClientMathService extends MathService {
*/
render(element: HTMLElement) {
if (environment.markdown.mathjax) {
this._window.nativeWindow.MathJax.typesetPromise([element]);
return (window as any).MathJax.typesetPromise([element]) as Promise<any>;
}
}
}
4 changes: 2 additions & 2 deletions src/app/core/shared/math.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ export class MockMathService extends MathService {
return of(true);
}

render(element: HTMLElement): void {
return;
render(element: HTMLElement): Promise<any> {
return Promise.resolve();
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/app/core/shared/math.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ export abstract class MathService {

protected abstract registerMathJaxAsync(config: MathJaxConfig): Promise<any>;
abstract ready(): Observable<boolean>;
abstract render(element: HTMLElement): void;
abstract render(element: HTMLElement): Promise<any>;
}
2 changes: 1 addition & 1 deletion src/app/core/shared/server-math.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@ export class ServerMathService extends MathService {
}

render(element: HTMLElement) {
return;
return Promise.resolve();
}
}
40 changes: 25 additions & 15 deletions src/app/shared/utils/markdown.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,32 +55,42 @@

async render(value: string, forcePreview = false): Promise<SafeHtml> {
if (isEmpty(value) || (!environment.markdown.enabled && !forcePreview)) {
return value;
}
const MarkdownIt = await this.markdownIt;
const md = new MarkdownIt({
html: true,
linkify: true,
});

const html = this.sanitizer.sanitize(SecurityContext.HTML, md.render(value));
this.el.innerHTML = html;

if (environment.markdown.mathjax) {
this.renderMathjax();
this.el.innerHTML = value;
return;

Check warning on line 59 in src/app/shared/utils/markdown.directive.ts

View check run for this annotation

Codecov / codecov/patch

src/app/shared/utils/markdown.directive.ts#L58-L59

Added lines #L58 - L59 were not covered by tests
} else {
if (environment.markdown.mathjax) {
this.renderMathjaxThenMarkdown(value);

Check warning on line 62 in src/app/shared/utils/markdown.directive.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L62 was not covered by tests
} else {
this.renderMarkdown(value);

Check warning on line 64 in src/app/shared/utils/markdown.directive.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L64 was not covered by tests
}
}
}

private renderMathjax() {
private renderMathjaxThenMarkdown(value: string) {
const sanitized = this.sanitizer.sanitize(SecurityContext.HTML, value);
this.el.innerHTML = sanitized;

Check warning on line 71 in src/app/shared/utils/markdown.directive.ts

View check run for this annotation

Codecov / codecov/patch

src/app/shared/utils/markdown.directive.ts#L70-L71

Added lines #L70 - L71 were not covered by tests
this.mathService.ready().pipe(
filter((ready) => ready),
take(1),
takeUntil(this.alive$),
).subscribe(() => {
this.mathService.render(this.el);
this.mathService.render(this.el)?.then(_ => {
this.renderMarkdown(this.el.innerHTML, true);

Check warning on line 78 in src/app/shared/utils/markdown.directive.ts

View check run for this annotation

Codecov / codecov/patch

src/app/shared/utils/markdown.directive.ts#L77-L78

Added lines #L77 - L78 were not covered by tests
});
});
}

private async renderMarkdown(value: string, alreadySanitized = false) {
const MarkdownIt = await this.markdownIt;
const md = new MarkdownIt({

Check warning on line 85 in src/app/shared/utils/markdown.directive.ts

View check run for this annotation

Codecov / codecov/patch

src/app/shared/utils/markdown.directive.ts#L84-L85

Added lines #L84 - L85 were not covered by tests
html: true,
linkify: true,
});

const html = alreadySanitized ? md.render(value) : this.sanitizer.sanitize(SecurityContext.HTML, md.render(value));
this.el.innerHTML = html;

Check warning on line 91 in src/app/shared/utils/markdown.directive.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L91 was not covered by tests
}

ngOnDestroy() {
this.alive$.next(false);
}
Expand Down
Loading