1
1
import { ComponentPortal , DomPortalHost } from '@angular/cdk/portal' ;
2
2
import { HttpClient , HttpErrorResponse } from '@angular/common/http' ;
3
+ import { DomSanitizer } from '@angular/platform-browser' ;
3
4
import {
4
5
ApplicationRef ,
5
6
Component ,
@@ -12,6 +13,7 @@ import {
12
13
OnDestroy ,
13
14
Output ,
14
15
ViewContainerRef ,
16
+ SecurityContext ,
15
17
} from '@angular/core' ;
16
18
import { Subscription } from 'rxjs' ;
17
19
import { take } from 'rxjs/operators' ;
@@ -43,7 +45,8 @@ export class DocViewer implements OnDestroy {
43
45
private _http : HttpClient ,
44
46
private _injector : Injector ,
45
47
private _viewContainerRef : ViewContainerRef ,
46
- private _ngZone : NgZone ) {
48
+ private _ngZone : NgZone ,
49
+ private _domSanitizer : DomSanitizer ) {
47
50
}
48
51
49
52
/** Fetch a document by URL. */
@@ -60,11 +63,15 @@ export class DocViewer implements OnDestroy {
60
63
}
61
64
62
65
/**
63
- * Updates the displayed document
64
- * @param document The raw document content to show.
66
+ * Updates the displayed document.
67
+ * @param rawDocument The raw document content to show.
65
68
*/
66
- private updateDocument ( document : string ) {
67
- this . _elementRef . nativeElement . innerHTML = document ;
69
+ private updateDocument ( rawDocument : string ) {
70
+ // Replaces all hash base links with the current path
71
+ const correctedDocument = this . _domSanitizer . sanitize (
72
+ SecurityContext . HTML ,
73
+ rawDocument . replace ( / ( < a h r e f = " # ) + / g, `<a href="${ window . location . href } #` ) ) ;
74
+ this . _elementRef . nativeElement . innerHTML = correctedDocument ;
68
75
this . textContent = this . _elementRef . nativeElement . textContent ;
69
76
this . _loadComponents ( 'material-docs-example' , ExampleViewer ) ;
70
77
this . _loadComponents ( 'header-link' , HeaderLink ) ;
0 commit comments