Skip to content

Commit c783863

Browse files
delucisHiDeoo
andauthored
Move site title href to route data for easier overrides (#1842)
Co-authored-by: HiDeoo <494699+HiDeoo@users.noreply.github.com>
1 parent e57873c commit c783863

5 files changed

Lines changed: 30 additions & 6 deletions

File tree

.changeset/polite-cheetahs-help.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@astrojs/starlight': patch
3+
---
4+
5+
Moves the `href` used in the site title link to Starlight’s route data object. This makes it possible for overrides to change the title link while reusing Starlight’s default component implemenation.

docs/src/content/docs/reference/overrides.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ The base path at which a language is served. `undefined` for root locale slugs.
5656

5757
The site title for this page’s locale.
5858

59+
#### `siteTitleHref`
60+
61+
**Type:** `string`
62+
63+
The value for the site title’s `href` attribute, linking back to the homepage, e.g. `/`.
64+
For multilingual sites this will include the current locale, e.g. `/en/` or `/zh-cn/`.
65+
5966
#### `slug`
6067

6168
**Type:** `string`

packages/starlight/components/SiteTitle.astro

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,10 @@
22
import { logos } from 'virtual:starlight/user-images';
33
import config from 'virtual:starlight/user-config';
44
import type { Props } from '../props';
5-
import { formatPath } from '../utils/format-path';
6-
7-
const { siteTitle } = Astro.props;
8-
const href = formatPath(Astro.props.locale || '/');
5+
const { siteTitle, siteTitleHref } = Astro.props;
96
---
107

11-
<a {href} class="site-title sl-flex">
8+
<a href={siteTitleHref} class="site-title sl-flex">
129
{
1310
config.logo && logos.dark && (
1411
<>

packages/starlight/utils/route-data.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { ensureTrailingSlash } from './path';
99
import type { Route } from './routing';
1010
import { localizedId } from './slugs';
1111
import { useTranslations } from './translations';
12+
import { formatPath } from './format-path';
1213

1314
export interface PageProps extends Route {
1415
headings: MarkdownHeading[];
@@ -17,6 +18,8 @@ export interface PageProps extends Route {
1718
export interface StarlightRouteData extends Route {
1819
/** Title of the site. */
1920
siteTitle: string;
21+
/** URL or path used as the link when clicking on the site title. */
22+
siteTitleHref: string;
2023
/** Array of Markdown headings extracted from the current page. */
2124
headings: MarkdownHeading[];
2225
/** Site navigation sidebar entries for this page. */
@@ -48,6 +51,7 @@ export function generateRouteData({
4851
return {
4952
...props,
5053
siteTitle,
54+
siteTitleHref: getSiteTitleHref(locale),
5155
sidebar,
5256
hasSidebar: entry.data.template !== 'splash',
5357
pagination: getPrevNextLinks(sidebar, config.pagination, entry.data),
@@ -118,3 +122,7 @@ export function getSiteTitle(lang: string): string {
118122
}
119123
return config.title[defaultLang] as string;
120124
}
125+
126+
export function getSiteTitleHref(locale: string | undefined): string {
127+
return formatPath(locale || '/');
128+
}

packages/starlight/utils/starlight-page.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@ import { type ContentConfig, type SchemaContext } from 'astro:content';
33
import config from 'virtual:starlight/user-config';
44
import { parseWithFriendlyErrors } from './error-map';
55
import { stripLeadingAndTrailingSlashes } from './path';
6-
import { getSiteTitle, getToC, type PageProps, type StarlightRouteData } from './route-data';
6+
import {
7+
getSiteTitle,
8+
getSiteTitleHref,
9+
getToC,
10+
type PageProps,
11+
type StarlightRouteData,
12+
} from './route-data';
713
import type { StarlightDocsEntry } from './routing';
814
import { slugToLocaleData, urlToSlug } from './slugs';
915
import { getPrevNextLinks, getSidebar } from './navigation';
@@ -224,6 +230,7 @@ export async function generateStarlightPageRouteData({
224230
pagination: getPrevNextLinks(sidebar, config.pagination, entry.data),
225231
sidebar,
226232
siteTitle: getSiteTitle(localeData.lang),
233+
siteTitleHref: getSiteTitleHref(localeData.locale),
227234
slug,
228235
toc: getToC({
229236
...routeProps,

0 commit comments

Comments
 (0)