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

Commit 7409f3c

Browse files
committed
fix: correct links in doc-viewer markdown files to have the proper base value
1 parent 71abfe4 commit 7409f3c

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
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(`href="/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: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,14 @@ export class DocViewer implements OnDestroy {
6060
}
6161

6262
/**
63-
* Updates the displayed document
64-
* @param document The raw document content to show.
63+
* Updates the displayed document.
64+
* @param rawDocument The raw document content to show.
6565
*/
66-
private updateDocument(document: string) {
67-
this._elementRef.nativeElement.innerHTML = document;
66+
private updateDocument(rawDocument: string) {
67+
// Replaces all hash base links with the current path
68+
const correctedDocument = rawDocument.replace(
69+
/(<a href="#)+/g, `<a href="${window.location.pathname}#`);
70+
this._elementRef.nativeElement.innerHTML = correctedDocument;
6871
this.textContent = this._elementRef.nativeElement.textContent;
6972
this._loadComponents('material-docs-example', ExampleViewer);
7073
this._loadComponents('header-link', HeaderLink);

0 commit comments

Comments
 (0)