forked from 4Science/dspace-angular
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merged in DSC-1401-cherry-pick-UXP-114-maintenance (pull request DSpa…
…ce#1575) DSC-1401 cherry pick UXP 114 maintenance Approved-by: Giuseppe Digilio
- Loading branch information
Showing
28 changed files
with
408 additions
and
209 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import { DOCUMENT } from '@angular/common'; | ||
import { | ||
Inject, | ||
Injectable, | ||
} from '@angular/core'; | ||
import { Observable, ReplaySubject, Subject } from 'rxjs'; | ||
import { environment } from 'src/environments/environment'; | ||
import { | ||
NativeWindowRef, | ||
NativeWindowService, | ||
} from '../services/window.service';import { MathJaxConfig, MathService } from './math.service'; | ||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
/** | ||
* Provide the MathService for CSR | ||
*/ | ||
export class ClientMathService extends MathService { | ||
|
||
protected isReady$: Subject<boolean>; | ||
|
||
protected mathJaxOptions = { | ||
tex: { | ||
inlineMath: [['$', '$'], ['\\(', '\\)']] | ||
}, | ||
svg: { | ||
fontCache: 'global' | ||
}, | ||
startup: { | ||
typeset: false | ||
} | ||
}; | ||
|
||
protected mathJax: MathJaxConfig = { | ||
source: 'https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js', | ||
id: 'MathJaxScript' | ||
}; | ||
protected mathJaxFallback: MathJaxConfig = { | ||
source: 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-chtml.min.js', | ||
id: 'MathJaxBackupScript' | ||
}; | ||
|
||
constructor( | ||
@Inject(DOCUMENT) private _document: Document, | ||
@Inject(NativeWindowService) protected _window: NativeWindowRef, | ||
) { | ||
super(); | ||
|
||
this.isReady$ = new ReplaySubject<boolean>(); | ||
|
||
void this.registerMathJaxAsync(this.mathJax) | ||
.then(() => this.isReady$.next(true)) | ||
.catch(_ => { | ||
void this.registerMathJaxAsync(this.mathJaxFallback) | ||
.then(() => this.isReady$.next(true)); | ||
}); | ||
} | ||
|
||
/** | ||
* Register the specified MathJax script in the document | ||
* | ||
* @param config The configuration object for the script | ||
*/ | ||
protected async registerMathJaxAsync(config: MathJaxConfig): Promise<any> { | ||
if (environment.markdown.mathjax) { | ||
return new Promise<void>((resolve, reject) => { | ||
|
||
const optionsScript: HTMLScriptElement = this._document.createElement('script'); | ||
optionsScript.type = 'text/javascript'; | ||
optionsScript.text = `MathJax = ${JSON.stringify(this.mathJaxOptions)};`; | ||
this._document.head.appendChild(optionsScript); | ||
|
||
const script: HTMLScriptElement = this._document.createElement('script'); | ||
script.id = config.id; | ||
script.type = 'text/javascript'; | ||
script.src = config.source; | ||
script.crossOrigin = 'anonymous'; | ||
script.async = true; | ||
script.onload = () => resolve(); | ||
script.onerror = error => reject(error); | ||
this._document.head.appendChild(script); | ||
}); | ||
} | ||
return Promise.resolve(); | ||
} | ||
|
||
/** | ||
* Return the status of the script registration | ||
*/ | ||
ready(): Observable<boolean> { | ||
return this.isReady$; | ||
} | ||
|
||
/** | ||
* Render the specified element using the MathJax JavaScript | ||
* | ||
* @param element The element to render with MathJax | ||
*/ | ||
render(element: HTMLElement) { | ||
if (environment.markdown.mathjax) { | ||
this._window.nativeWindow.MathJax.typesetPromise([element]); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
import { Observable, of } from 'rxjs'; | ||
import { MathService, MathJaxConfig } from './math.service'; | ||
|
||
export class MockMathService extends MathService { | ||
protected mathJaxOptions: any = {}; | ||
protected mathJax: MathJaxConfig = { source: '', id: '' }; | ||
protected mathJaxFallback: MathJaxConfig = { source: '', id: '' }; | ||
|
||
protected registerMathJaxAsync(config: MathJaxConfig): Promise<any> { | ||
return Promise.resolve(); | ||
} | ||
|
||
ready(): Observable<boolean> { | ||
return of(true); | ||
} | ||
|
||
render(element: HTMLElement): void { | ||
return; | ||
} | ||
} | ||
|
||
describe('MathService', () => { | ||
let service: MockMathService; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({}); | ||
service = new MockMathService(); | ||
spyOn(service, 'render'); | ||
}); | ||
|
||
it('should be created', () => { | ||
expect(service).toBeTruthy(); | ||
}); | ||
|
||
it('should be ready', (done) => { | ||
service.ready().subscribe(isReady => { | ||
expect(isReady).toBe(true); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should render', () => { | ||
service.render(document.createElement('div')); | ||
expect(service.render).toHaveBeenCalled(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { Observable } from 'rxjs'; | ||
|
||
export interface MathJaxConfig { | ||
source: string; | ||
id: string; | ||
} | ||
|
||
/** | ||
* This service is used to provide the MathJax library with the ability to render markdown code | ||
*/ | ||
export abstract class MathService { | ||
protected abstract mathJaxOptions: any; | ||
protected abstract mathJax: MathJaxConfig; | ||
protected abstract mathJaxFallback: MathJaxConfig; | ||
|
||
protected abstract registerMathJaxAsync(config: MathJaxConfig): Promise<any>; | ||
abstract ready(): Observable<boolean>; | ||
abstract render(element: HTMLElement): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { Observable, ReplaySubject, Subject } from 'rxjs'; | ||
import { MathJaxConfig, MathService } from './math.service'; | ||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
/** | ||
* Provide the MathService for SSR | ||
*/ | ||
export class ServerMathService extends MathService { | ||
|
||
protected signal: Subject<boolean>; | ||
|
||
protected mathJaxOptions = {}; | ||
|
||
protected mathJax: MathJaxConfig = { | ||
source: '', | ||
id: '' | ||
}; | ||
protected mathJaxFallback: MathJaxConfig = { | ||
source: '', | ||
id: '' | ||
}; | ||
|
||
constructor() { | ||
super(); | ||
|
||
this.signal = new ReplaySubject<boolean>(); | ||
this.signal.next(true); | ||
} | ||
|
||
protected async registerMathJaxAsync(config: MathJaxConfig): Promise<any> { | ||
return Promise.resolve(); | ||
} | ||
|
||
ready(): Observable<boolean> { | ||
return this.signal; | ||
} | ||
|
||
render(element: HTMLElement) { | ||
return; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
<nu-markdown-preview [value]="value"></nu-markdown-preview> | ||
<span [dsMarkdown]="value"></span> |
Oops, something went wrong.