📘DOC: Fix URL normalization for the Left Sidebar in docs (#1299)

* Fix URL normalization for the Left Sidebar in docs

* Move the fix into `util.ts` as suggested by @FredKSchott
This commit is contained in:
Caleb Jasik 2021-09-04 19:55:55 -05:00 committed by GitHub
parent 02c38a0d3b
commit 73a98821b7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 2 deletions

View file

@ -1,8 +1,10 @@
---
import { SIDEBAR } from '../../config.ts';
import { getLanguageFromURL } from '../../util.ts';
import { getLanguageFromURL, removeLeadingSlash, removeTrailingSlash } from '../../util.ts';
const {currentPage} = Astro.props;
const currentPageMatch = currentPage.slice(1);
// Get the slug w/o a leading or trailing slash
const currentPageMatch = removeLeadingSlash(removeTrailingSlash(currentPage));
const langCode = getLanguageFromURL(currentPage);
// SIDEBAR is a flat array. Group it by sections to properly render.
const sidebarSections = SIDEBAR[langCode].reduce((col, item) => {

View file

@ -2,3 +2,13 @@ export function getLanguageFromURL(pathname: string) {
const langCodeMatch = pathname.match(/\/([a-z]{2}-?[A-Z]{0,2})\//);
return langCodeMatch ? langCodeMatch[1] : 'en';
}
/** Remove \ and / from beginning of string */
export function removeLeadingSlash(path: string) {
return path.replace(/^[/\\]+/, '');
}
/** Remove \ and / from end of string */
export function removeTrailingSlash(path: string) {
return path.replace(/[/\\]+$/, '');
}