Skip to content
This repository was archived by the owner on Dec 18, 2024. It is now read-only.

Commit 704a5d0

Browse files
fix: correct links in doc-viewer markdown files to have the proper base value (#554)
1 parent f2054c0 commit 704a5d0

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

src/app/shared/doc-viewer/doc-viewer.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,20 @@ describe('DocViewer', () => {
4444
expect(docViewer.componentInstance.textContent).toBe('my docs page');
4545
});
4646

47+
48+
it('should correct hash based links', () => {
49+
let fixture = TestBed.createComponent(DocViewerTestComponent);
50+
fixture.componentRef.instance.documentUrl = `http://material.angular.io/doc-with-links.html`;
51+
fixture.detectChanges();
52+
53+
const url = fixture.componentInstance.documentUrl;
54+
http.expectOne(url).flush(FAKE_DOCS[url]);
55+
56+
let docViewer = fixture.debugElement.query(By.directive(DocViewer));
57+
// Our test runner runs at the page /context.html, so it will be the prepended value.
58+
expect(docViewer.nativeElement.innerHTML).toContain(`/context.html#test"`);
59+
});
60+
4761
it('should show error message when doc not found', () => {
4862
spyOn(console, 'log');
4963

@@ -84,4 +98,5 @@ const FAKE_DOCS = {
8498
'http://material.angular.io/doc-with-example.html': `
8599
<div>Check out this example:</div>
86100
<div material-docs-example="some-example"></div>`,
101+
'http://material.angular.io/doc-with-links.html': `<a href="#test">Test link</a>`,
87102
};

src/app/shared/doc-viewer/doc-viewer.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {ComponentPortal, DomPortalHost} from '@angular/cdk/portal';
22
import {HttpClient, HttpErrorResponse} from '@angular/common/http';
3+
import {DomSanitizer} from '@angular/platform-browser';
34
import {
45
ApplicationRef,
56
Component,
@@ -12,6 +13,7 @@ import {
1213
OnDestroy,
1314
Output,
1415
ViewContainerRef,
16+
SecurityContext,
1517
} from '@angular/core';
1618
import {Subscription} from 'rxjs';
1719
import {take} from 'rxjs/operators';
@@ -43,7 +45,8 @@ export class DocViewer implements OnDestroy {
4345
private _http: HttpClient,
4446
private _injector: Injector,
4547
private _viewContainerRef: ViewContainerRef,
46-
private _ngZone: NgZone) {
48+
private _ngZone: NgZone,
49+
private _domSanitizer: DomSanitizer) {
4750
}
4851

4952
/** Fetch a document by URL. */
@@ -60,11 +63,15 @@ export class DocViewer implements OnDestroy {
6063
}
6164

6265
/**
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.
6568
*/
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 href="#)+/g, `<a href="${window.location.href}#`));
74+
this._elementRef.nativeElement.innerHTML = correctedDocument;
6875
this.textContent = this._elementRef.nativeElement.textContent;
6976
this._loadComponents('material-docs-example', ExampleViewer);
7077
this._loadComponents('header-link', HeaderLink);

0 commit comments

Comments
 (0)