summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRick Viscomi2023-06-07 12:57:57 -0400
committerGitHub2023-06-07 18:57:57 +0200
commit6ab31b4900166f952c1ca5ec4e4a1ef66f31be97 (patch)
tree0c544db0a1851cb7a2a2739d93c545ac79d34d2b
parent3b608adddae6f4f8d933ebc6a4dd06b4dacea1b1 (diff)
downloadIT.starlight-6ab31b4900166f952c1ca5ec4e4a1ef66f31be97.tar.gz
IT.starlight-6ab31b4900166f952c1ca5ec4e4a1ef66f31be97.tar.bz2
IT.starlight-6ab31b4900166f952c1ca5ec4e4a1ef66f31be97.zip
Split `withBase` to support both paths and files (#174)
-rw-r--r--packages/starlight/404.astro4
-rw-r--r--packages/starlight/components/HeadSEO.astro6
-rw-r--r--packages/starlight/components/SiteTitle.astro4
-rw-r--r--packages/starlight/utils/base.ts8
-rw-r--r--packages/starlight/utils/navigation.ts4
5 files changed, 16 insertions, 10 deletions
diff --git a/packages/starlight/404.astro b/packages/starlight/404.astro
index 0a4efd08..ae7f1765 100644
--- a/packages/starlight/404.astro
+++ b/packages/starlight/404.astro
@@ -1,6 +1,6 @@
---
import config from 'virtual:starlight/user-config';
-import { withBase } from './utils/base';
+import { pathWithBase } from './utils/base';
// Built-in CSS styles.
import './style/props.css';
@@ -40,7 +40,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={withBase('/')}>head back home</a>.
+ <a href={pathWithBase('/')}>head back home</a>.
</p>
</MarkdownContent>
</main>
diff --git a/packages/starlight/components/HeadSEO.astro b/packages/starlight/components/HeadSEO.astro
index c4706471..0df95276 100644
--- a/packages/starlight/components/HeadSEO.astro
+++ b/packages/starlight/components/HeadSEO.astro
@@ -4,7 +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';
+import { fileWithBase } from '../utils/base';
interface Props {
data: CollectionEntry<'docs'>['data'];
@@ -33,7 +33,7 @@ const headDefaults: z.input<ReturnType<typeof HeadConfigSchema>> = [
tag: 'link',
attrs: {
rel: 'shortcut icon',
- href: withBase('/favicon.svg'),
+ href: fileWithBase('/favicon.svg'),
type: 'image/svg+xml',
},
},
@@ -81,7 +81,7 @@ if (Astro.site) {
tag: 'link',
attrs: {
rel: 'sitemap',
- href: withBase('/sitemap-index.xml'),
+ href: fileWithBase('/sitemap-index.xml'),
},
});
}
diff --git a/packages/starlight/components/SiteTitle.astro b/packages/starlight/components/SiteTitle.astro
index 6f29fff2..1a45bf06 100644
--- a/packages/starlight/components/SiteTitle.astro
+++ b/packages/starlight/components/SiteTitle.astro
@@ -1,7 +1,7 @@
---
import { logos } from 'virtual:starlight/user-images';
import config from 'virtual:starlight/user-config';
-import { withBase } from '../utils/base';
+import { pathWithBase } from '../utils/base';
interface Props {
locale: string | undefined;
@@ -23,7 +23,7 @@ if (config.logo) {
if (err) throw new Error(err);
}
-const href = withBase(Astro.props.locale || '/');
+const href = pathWithBase(Astro.props.locale || '/');
---
<a {href} class="site-title flex">
diff --git a/packages/starlight/utils/base.ts b/packages/starlight/utils/base.ts
index 4565b753..7769396f 100644
--- a/packages/starlight/utils/base.ts
+++ b/packages/starlight/utils/base.ts
@@ -1,11 +1,17 @@
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) {
+export function pathWithBase(path: string) {
path = stripLeadingSlash(stripTrailingSlash(path));
return path ? base + '/' + path + '/' : base + '/';
}
+/** Get the a root-relative file URL path with the site’s `base` prefixed. */
+export function fileWithBase(path: string) {
+ path = stripLeadingSlash(stripTrailingSlash(path));
+ return path ? base + '/' + path : base;
+}
+
function stripLeadingSlash(path: string) {
return path.replace(/^\//, '');
}
diff --git a/packages/starlight/utils/navigation.ts b/packages/starlight/utils/navigation.ts
index 394990ac..0228d4a0 100644
--- a/packages/starlight/utils/navigation.ts
+++ b/packages/starlight/utils/navigation.ts
@@ -1,6 +1,6 @@
import { basename, dirname } from 'node:path';
import config from 'virtual:starlight/user-config';
-import { withBase } from './base';
+import { pathWithBase } from './base';
import { pickLang } from './i18n';
import { Route, getLocaleRoutes, routes } from './routing';
import { localeToLang, slugToPathname } from './slugs';
@@ -109,7 +109,7 @@ function linkFromConfig(
/** Create a link entry. */
function makeLink(href: string, label: string, currentPathname: string): Link {
- if (!isAbsolute(href)) href = withBase(href);
+ if (!isAbsolute(href)) href = pathWithBase(href);
const isCurrent = href === currentPathname;
return { type: 'link', label, href, isCurrent };
}