summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Swithinbank2023-05-17 10:34:10 +0200
committerGitHub2023-05-17 10:34:10 +0200
commita91191e8308ffa746a3eadeea61e39412f32f926 (patch)
tree36fbbea511b6848e7c3bd87d66a6e7945a64afb6
parent608f34cbbe485c39730f33828971397f9c8a3534 (diff)
downloadIT.starlight-a91191e8308ffa746a3eadeea61e39412f32f926.tar.gz
IT.starlight-a91191e8308ffa746a3eadeea61e39412f32f926.tar.bz2
IT.starlight-a91191e8308ffa746a3eadeea61e39412f32f926.zip
Better `base` URL support (#62)
-rw-r--r--.changeset/curvy-maps-care.md5
-rw-r--r--packages/starlight/404.astro3
-rw-r--r--packages/starlight/components/HeadSEO.astro5
-rw-r--r--packages/starlight/components/SiteTitle.astro7
-rw-r--r--packages/starlight/utils/base.ts14
-rw-r--r--packages/starlight/utils/navigation.ts10
6 files changed, 29 insertions, 15 deletions
diff --git a/.changeset/curvy-maps-care.md b/.changeset/curvy-maps-care.md
new file mode 100644
index 00000000..41b53a6d
--- /dev/null
+++ b/.changeset/curvy-maps-care.md
@@ -0,0 +1,5 @@
+---
+"@astrojs/starlight": patch
+---
+
+Make `base` support consistent, including when `trailingSlash: 'never'` is set.
diff --git a/packages/starlight/404.astro b/packages/starlight/404.astro
index cdef0b31..de897a79 100644
--- a/packages/starlight/404.astro
+++ b/packages/starlight/404.astro
@@ -17,6 +17,7 @@ import ThemeProvider from './components/ThemeProvider.astro';
// Important that this is the last import so it can override built-in styles.
import 'virtual:starlight/user-css';
+import { withBase } from './utils/base';
const { lang = 'en', dir = 'ltr', locale } = config.defaultLocale || {};
---
@@ -37,7 +38,7 @@ const { lang = 'en', dir = 'ltr', locale } = config.defaultLocale || {};
<p>Houston, we have a problem.</p>
<p>
We couldn’t find that link. Check the address or <a
- href={import.meta.env.BASE_URL}>head back home</a
+ href={withBase('/')}>head back home</a
>.
</p>
</MarkdownContent>
diff --git a/packages/starlight/components/HeadSEO.astro b/packages/starlight/components/HeadSEO.astro
index 01f8dbbf..c4706471 100644
--- a/packages/starlight/components/HeadSEO.astro
+++ b/packages/starlight/components/HeadSEO.astro
@@ -4,6 +4,7 @@ import config from 'virtual:starlight/user-config';
import type { HeadConfigSchema } from '../schemas/head';
import { createHead } from '../utils/head';
import { localizedUrl } from '../utils/localizedUrl';
+import { withBase } from '../utils/base';
interface Props {
data: CollectionEntry<'docs'>['data'];
@@ -32,7 +33,7 @@ const headDefaults: z.input<ReturnType<typeof HeadConfigSchema>> = [
tag: 'link',
attrs: {
rel: 'shortcut icon',
- href: import.meta.env.BASE_URL + 'favicon.svg',
+ href: withBase('/favicon.svg'),
type: 'image/svg+xml',
},
},
@@ -80,7 +81,7 @@ if (Astro.site) {
tag: 'link',
attrs: {
rel: 'sitemap',
- href: import.meta.env.BASE_URL + 'sitemap-index.xml',
+ href: withBase('/sitemap-index.xml'),
},
});
}
diff --git a/packages/starlight/components/SiteTitle.astro b/packages/starlight/components/SiteTitle.astro
index 0a4472aa..705025a0 100644
--- a/packages/starlight/components/SiteTitle.astro
+++ b/packages/starlight/components/SiteTitle.astro
@@ -1,6 +1,7 @@
---
import { logos } from 'virtual:starlight/user-images';
import config from 'virtual:starlight/user-config';
+import { withBase } from '../utils/base';
interface Props {
locale: string | undefined;
@@ -22,11 +23,7 @@ if (config.logo) {
if (err) throw new Error(err);
}
-const { locale } = Astro.props
-
-const href = locale
- ? `${import.meta.env.BASE_URL.replace(/\/$/, '')}/${locale}/`
- : import.meta.env.BASE_URL
+const href = withBase(Astro.props.locale || '/');
---
<a {href} class="site-title flex">
diff --git a/packages/starlight/utils/base.ts b/packages/starlight/utils/base.ts
new file mode 100644
index 00000000..4565b753
--- /dev/null
+++ b/packages/starlight/utils/base.ts
@@ -0,0 +1,14 @@
+const base = stripTrailingSlash(import.meta.env.BASE_URL);
+
+/** Get the a root-relative URL path with the site’s `base` prefixed. */
+export function withBase(path: string) {
+ path = stripLeadingSlash(stripTrailingSlash(path));
+ return path ? base + '/' + path + '/' : base + '/';
+}
+
+function stripLeadingSlash(path: string) {
+ return path.replace(/^\//, '');
+}
+function stripTrailingSlash(path: string) {
+ return path.replace(/\/$/, '');
+}
diff --git a/packages/starlight/utils/navigation.ts b/packages/starlight/utils/navigation.ts
index 1fa19cf9..ae4f918b 100644
--- a/packages/starlight/utils/navigation.ts
+++ b/packages/starlight/utils/navigation.ts
@@ -1,7 +1,8 @@
import { basename, dirname } from 'node:path';
import config from 'virtual:starlight/user-config';
-import { slugToPathname } from './slugs';
+import { withBase } from './base';
import { Route, getLocaleRoutes, routes } from './routing';
+import { slugToPathname } from './slugs';
import type {
AutoSidebarGroup,
SidebarItem,
@@ -100,12 +101,7 @@ function linkFromConfig(
/** Create a link entry. */
function makeLink(href: string, label: string, currentPathname: string): Link {
- if (!isAbsolute(href)) {
- href = ensureLeadingAndTrailingSlashes(href);
- /** Base URL with trailing `/` stripped. */
- const base = import.meta.env.BASE_URL.replace(/\/$/, '');
- if (base) href = base + href;
- }
+ if (!isAbsolute(href)) href = withBase(href);
const isCurrent = href === currentPathname;
return { type: 'link', label, href, isCurrent };
}