|
| 1 | +import { DOCUMENT } from '@angular/common'; |
| 2 | +import { |
| 3 | + Inject, |
| 4 | + Injectable, |
| 5 | +} from '@angular/core'; |
| 6 | +import { Observable, ReplaySubject, Subject } from 'rxjs'; |
| 7 | +import { environment } from 'src/environments/environment'; |
| 8 | +import { |
| 9 | + NativeWindowRef, |
| 10 | + NativeWindowService, |
| 11 | +} from '../services/window.service';import { MathJaxConfig, MathService } from './math.service'; |
| 12 | + |
| 13 | +@Injectable({ |
| 14 | + providedIn: 'root' |
| 15 | +}) |
| 16 | +/** |
| 17 | + * Provide the MathService for CSR |
| 18 | + */ |
| 19 | +export class ClientMathService extends MathService { |
| 20 | + |
| 21 | + protected isReady$: Subject<boolean>; |
| 22 | + |
| 23 | + protected mathJaxOptions = { |
| 24 | + tex: { |
| 25 | + inlineMath: [['$', '$'], ['\\(', '\\)']] |
| 26 | + }, |
| 27 | + svg: { |
| 28 | + fontCache: 'global' |
| 29 | + }, |
| 30 | + startup: { |
| 31 | + typeset: false |
| 32 | + } |
| 33 | + }; |
| 34 | + |
| 35 | + protected mathJax: MathJaxConfig = { |
| 36 | + source: 'https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js', |
| 37 | + id: 'MathJaxScript' |
| 38 | + }; |
| 39 | + protected mathJaxFallback: MathJaxConfig = { |
| 40 | + source: 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-chtml.min.js', |
| 41 | + id: 'MathJaxBackupScript' |
| 42 | + }; |
| 43 | + |
| 44 | + constructor( |
| 45 | + @Inject(DOCUMENT) private _document: Document, |
| 46 | + @Inject(NativeWindowService) protected _window: NativeWindowRef, |
| 47 | + ) { |
| 48 | + super(); |
| 49 | + |
| 50 | + this.isReady$ = new ReplaySubject<boolean>(); |
| 51 | + |
| 52 | + void this.registerMathJaxAsync(this.mathJax) |
| 53 | + .then(() => this.isReady$.next(true)) |
| 54 | + .catch(_ => { |
| 55 | + void this.registerMathJaxAsync(this.mathJaxFallback) |
| 56 | + .then(() => this.isReady$.next(true)); |
| 57 | + }); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Register the specified MathJax script in the document |
| 62 | + * |
| 63 | + * @param config The configuration object for the script |
| 64 | + */ |
| 65 | + protected async registerMathJaxAsync(config: MathJaxConfig): Promise<any> { |
| 66 | + if (environment.markdown.mathjax) { |
| 67 | + return new Promise<void>((resolve, reject) => { |
| 68 | + |
| 69 | + const optionsScript: HTMLScriptElement = this._document.createElement('script'); |
| 70 | + optionsScript.type = 'text/javascript'; |
| 71 | + optionsScript.text = `MathJax = ${JSON.stringify(this.mathJaxOptions)};`; |
| 72 | + this._document.head.appendChild(optionsScript); |
| 73 | + |
| 74 | + const script: HTMLScriptElement = this._document.createElement('script'); |
| 75 | + script.id = config.id; |
| 76 | + script.type = 'text/javascript'; |
| 77 | + script.src = config.source; |
| 78 | + script.crossOrigin = 'anonymous'; |
| 79 | + script.async = true; |
| 80 | + script.onload = () => resolve(); |
| 81 | + script.onerror = error => reject(error); |
| 82 | + this._document.head.appendChild(script); |
| 83 | + }); |
| 84 | + } |
| 85 | + return Promise.resolve(); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Return the status of the script registration |
| 90 | + */ |
| 91 | + ready(): Observable<boolean> { |
| 92 | + return this.isReady$; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Render the specified element using the MathJax JavaScript |
| 97 | + * |
| 98 | + * @param element The element to render with MathJax |
| 99 | + */ |
| 100 | + render(element: HTMLElement) { |
| 101 | + if (environment.markdown.mathjax) { |
| 102 | + this._window.nativeWindow.MathJax.typesetPromise([element]); |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments