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

fix: correct links in doc-viewer markdown files to have the proper base value #554

Merged
merged 1 commit into from
Nov 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/app/shared/doc-viewer/doc-viewer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,20 @@ describe('DocViewer', () => {
expect(docViewer.componentInstance.textContent).toBe('my docs page');
});


it('should correct hash based links', () => {
let fixture = TestBed.createComponent(DocViewerTestComponent);
fixture.componentRef.instance.documentUrl = `http://material.angular.io/doc-with-links.html`;
fixture.detectChanges();

const url = fixture.componentInstance.documentUrl;
http.expectOne(url).flush(FAKE_DOCS[url]);

let docViewer = fixture.debugElement.query(By.directive(DocViewer));
// Our test runner runs at the page /context.html, so it will be the prepended value.
expect(docViewer.nativeElement.innerHTML).toContain(`/context.html#test"`);
});

it('should show error message when doc not found', () => {
spyOn(console, 'log');

Expand Down Expand Up @@ -84,4 +98,5 @@ const FAKE_DOCS = {
'http://material.angular.io/doc-with-example.html': `
<div>Check out this example:</div>
<div material-docs-example="some-example"></div>`,
'http://material.angular.io/doc-with-links.html': `<a href="#test">Test link</a>`,
};
17 changes: 12 additions & 5 deletions src/app/shared/doc-viewer/doc-viewer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {ComponentPortal, DomPortalHost} from '@angular/cdk/portal';
import {HttpClient, HttpErrorResponse} from '@angular/common/http';
import {DomSanitizer} from '@angular/platform-browser';
import {
ApplicationRef,
Component,
Expand All @@ -12,6 +13,7 @@ import {
OnDestroy,
Output,
ViewContainerRef,
SecurityContext,
} from '@angular/core';
import {Subscription} from 'rxjs';
import {take} from 'rxjs/operators';
Expand Down Expand Up @@ -43,7 +45,8 @@ export class DocViewer implements OnDestroy {
private _http: HttpClient,
private _injector: Injector,
private _viewContainerRef: ViewContainerRef,
private _ngZone: NgZone) {
private _ngZone: NgZone,
private _domSanitizer: DomSanitizer) {
}

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

/**
* Updates the displayed document
* @param document The raw document content to show.
* Updates the displayed document.
* @param rawDocument The raw document content to show.
*/
private updateDocument(document: string) {
this._elementRef.nativeElement.innerHTML = document;
private updateDocument(rawDocument: string) {
// Replaces all hash base links with the current path
const correctedDocument = this._domSanitizer.sanitize(
SecurityContext.HTML,
rawDocument.replace(/(<a href="#)+/g, `<a href="${window.location.href}#`));
this._elementRef.nativeElement.innerHTML = correctedDocument;
this.textContent = this._elementRef.nativeElement.textContent;
this._loadComponents('material-docs-example', ExampleViewer);
this._loadComponents('header-link', HeaderLink);
Expand Down