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

Commit 89a6cf1

Browse files
devversionjelbourn
authored andcommitted
fix: create proper fragment urls (#329)
By default fragment links are referring to the root page and the actual hash won't matter anymore. This can be fixed in a similar way to the `header-link` directive by updating the anchor URLs to include the current base URL. Since those are normal links and aren't `header-links`, the `header-link` directive is not an option. A directive that updates the links accordingly would be perfect, but there is no way to interpolate directives on those HTML templates dynamically. Having a component that creates/wraps the anchor elements is wrong and adds more pain than it helps. The links should can be just updated using simple DOM manipulation.
1 parent c348f1f commit 89a6cf1

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

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

+21-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {Subscription} from 'rxjs/Subscription';
1515
import {ExampleViewer} from '../example-viewer/example-viewer';
1616
import {HeaderLink} from './header-link';
1717
import {ComponentPortal, DomPortalHost} from '@angular/cdk/portal';
18+
import {Router} from '@angular/router';
1819

1920
@Component({
2021
selector: 'doc-viewer',
@@ -40,7 +41,8 @@ export class DocViewer implements OnDestroy {
4041
private _elementRef: ElementRef,
4142
private _http: Http,
4243
private _injector: Injector,
43-
private _viewContainerRef: ViewContainerRef) {}
44+
private _viewContainerRef: ViewContainerRef,
45+
private _router: Router) {}
4446

4547
/** Fetch a document by URL. */
4648
private _fetchDocument(url: string) {
@@ -57,6 +59,7 @@ export class DocViewer implements OnDestroy {
5759
this.textContent = this._elementRef.nativeElement.textContent;
5860
this._loadComponents('material-docs-example', ExampleViewer);
5961
this._loadComponents('header-link', HeaderLink);
62+
this._fixFragmentUrls();
6063
this.contentLoaded.next();
6164
} else {
6265
this._elementRef.nativeElement.innerText =
@@ -100,6 +103,23 @@ export class DocViewer implements OnDestroy {
100103
this._portalHosts = [];
101104
}
102105

106+
/**
107+
* A fragment link is a link that references a specific element on the page that should be
108+
* scrolled into the viewport on page load or click.
109+
*
110+
* By default those links refer to the root page of the documentation and the fragment links
111+
* won't work properly. Those links need to be updated to be relative to the current base URL.
112+
*/
113+
private _fixFragmentUrls() {
114+
const baseUrl = this._router.url.split('#')[0];
115+
const anchorElements =
116+
[].slice.call(this._elementRef.nativeElement.querySelectorAll('a')) as HTMLAnchorElement[];
117+
118+
anchorElements
119+
.filter(anchorEl => anchorEl.hash)
120+
.forEach(anchorEl => anchorEl.href = `${baseUrl}${anchorEl.hash}`);
121+
}
122+
103123
ngOnDestroy() {
104124
this._clearLiveExamples();
105125

0 commit comments

Comments
 (0)