Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(trailing-slash): add trailing slash url serializer #98

Closed
wants to merge 4 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export class TableOfContentsComponent implements OnInit, OnDestroy {
switchMap((el) =>
this.content.getCurrent().pipe(map((c) => [el.id, c.route]))
),
tap(([el, route]) => this.location.replaceState(`${route}#${el}`)),
tap(([el, route]) => this.location.replaceState(`${route}/#${el}`)),
takeUntil(this.onDestroy$)
)
.subscribe();
Expand Down Expand Up @@ -107,7 +107,7 @@ export class TableOfContentsComponent implements OnInit, OnDestroy {
}

scrollTo(url: string, id: string) {
this.location.replaceState(`${url}#${id}`);
this.location.replaceState(`${url}/#${id}`);
this.document.getElementById(id)?.scrollIntoView({ behavior: 'smooth' });
}

Expand Down
8 changes: 5 additions & 3 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import {
import { NewsletterSignupModule } from '@components/newsletter-signup/newsletter-signup.module';
import { NizSearchComponentModule } from '@components/search/search.module';
import { PipesModule } from '@pipes/pipes.module';
import { UrlSerializer } from '@angular/router';
import { TrailingSlashUrlSerializer } from './utils/trailing-slash-url-serializer';

@NgModule({
declarations: [AppComponent],
Expand All @@ -35,17 +37,17 @@ import { PipesModule } from '@pipes/pipes.module';
MarkdownModule.forRoot({ loader: HttpClient }),
NizTabsModule,
NizTabModule,
NizFooterModule,
NizFooterModule,
NizNavbarModule,
NizToolbarModule,
NizInlineSvgModule,
NewsletterSignupModule,
NizSearchComponentModule,
PipesModule,
NizToastModule,
NizMenuModule
NizMenuModule,
],
providers: [],
providers: [{ provide: UrlSerializer, useClass: TrailingSlashUrlSerializer }],
bootstrap: [AppComponent],
schemas: [CUSTOM_ELEMENTS_SCHEMA],
})
Expand Down
23 changes: 23 additions & 0 deletions src/app/utils/trailing-slash-url-serializer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { DefaultUrlSerializer, UrlTree } from '@angular/router';

/**
* @see https://github.com/angular/angular/issues/16051
*/
export class TrailingSlashUrlSerializer extends DefaultUrlSerializer {
private static _withTrailingSlash(url: string): string {
const splitOn = url.indexOf('?') > -1 ? '?' : '#';
const pathArr = url.split(splitOn);

if (!pathArr[0].endsWith('/')) {
const fileName: string = url.substring(url.lastIndexOf('/') + 1);
if (fileName.indexOf('.') === -1) {
pathArr[0] += '/';
}
}
return pathArr.join(splitOn);
}

serialize(tree: UrlTree): string {
return TrailingSlashUrlSerializer._withTrailingSlash(super.serialize(tree));
}
}