From ac41426a20cf646a0d0f915b993330e54bbddd80 Mon Sep 17 00:00:00 2001 From: Felix Roos Date: Tue, 5 Dec 2023 23:09:34 +0100 Subject: [PATCH 1/9] improve slashing + base href behavior + prefix sidebar links & mdx links with base + add trailing slashes --- website/astro.config.mjs | 14 +++++++++----- .../src/components/LeftSidebar/LeftSidebar.astro | 3 ++- .../components/RightSidebar/TableOfContents.tsx | 2 +- website/src/repl/Header.jsx | 4 +++- 4 files changed, 15 insertions(+), 8 deletions(-) diff --git a/website/astro.config.mjs b/website/astro.config.mjs index f8fceaf76..84a89b335 100644 --- a/website/astro.config.mjs +++ b/website/astro.config.mjs @@ -13,6 +13,7 @@ import AstroPWA from '@vite-pwa/astro'; const site = `https://strudel.cc/`; // root url without a path const base = '/'; // base path of the strudel site +const baseNoTrailing = base.endsWith('/') ? base.slice(0, -1) : base; // this rehype plugin converts relative anchor links to absolute ones // it wokrs by prepending the absolute page path to anchor links @@ -24,13 +25,16 @@ function absoluteAnchors() { const chunks = file.history[0].split('/src/pages/'); // file.history[0] is the file path const path = chunks[chunks.length - 1].slice(0, -4); // only path inside src/pages, without .mdx return rehypeUrls((url) => { - if (!url.href.startsWith('#')) { - return; + if (url.href.startsWith('/')) { + const hrefWithTrailingSlash = url.href.endsWith('/') ? url.href : url.href + '/'; + return baseNoTrailing + hrefWithTrailingSlash; + } + if (url.href.startsWith('#')) { + const absoluteUrl = `${baseNoTrailing}/${path}/${url.href}`; + return absoluteUrl; } - const baseWithSlash = base.endsWith('/') ? base : base + '/'; - const absoluteUrl = baseWithSlash + path + url.href; // console.log(url.href + ' -> ', absoluteUrl); - return absoluteUrl; + return; })(tree); }; } diff --git a/website/src/components/LeftSidebar/LeftSidebar.astro b/website/src/components/LeftSidebar/LeftSidebar.astro index b3f69a7e1..8b823db6a 100644 --- a/website/src/components/LeftSidebar/LeftSidebar.astro +++ b/website/src/components/LeftSidebar/LeftSidebar.astro @@ -9,6 +9,7 @@ type Props = { const { currentPage } = Astro.props as Props; const { BASE_URL } = import.meta.env; let currentPageMatch = currentPage.slice(BASE_URL.length, currentPage.endsWith('/') ? -1 : undefined); +const baseNoTrailing = BASE_URL.endsWith('/') ? BASE_URL.slice(0, -1) : BASE_URL; const langCode = getLanguageFromURL(currentPage) || 'en'; const sidebar = SIDEBAR[langCode]; @@ -23,7 +24,7 @@ const sidebar = SIDEBAR[langCode];

{header}