From 73a98821b785e0de1e059d50bbdce402b3a8eaca Mon Sep 17 00:00:00 2001 From: Caleb Jasik Date: Sat, 4 Sep 2021 19:55:55 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=98DOC:=20Fix=20URL=20normalization=20?= =?UTF-8?q?for=20the=20Left=20Sidebar=20in=20docs=20(#1299)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fix URL normalization for the Left Sidebar in docs * Move the fix into `util.ts` as suggested by @FredKSchott --- docs/src/components/LeftSidebar/LeftSidebar.astro | 6 ++++-- docs/src/util.ts | 10 ++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/docs/src/components/LeftSidebar/LeftSidebar.astro b/docs/src/components/LeftSidebar/LeftSidebar.astro index 813de0cf75..1960dbb04b 100644 --- a/docs/src/components/LeftSidebar/LeftSidebar.astro +++ b/docs/src/components/LeftSidebar/LeftSidebar.astro @@ -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) => { diff --git a/docs/src/util.ts b/docs/src/util.ts index 0ec91bce09..2693737354 100644 --- a/docs/src/util.ts +++ b/docs/src/util.ts @@ -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(/[/\\]+$/, ''); +}