Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move site title href to route data for easier overrides #1842

Merged
merged 4 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/polite-cheetahs-help.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/starlight': patch
---

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.
7 changes: 7 additions & 0 deletions docs/src/content/docs/reference/overrides.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ The base path at which a language is served. `undefined` for root locale slugs.

The site title for this page’s locale.

#### `siteTitleHref`

**Type:** `string`

The value for the site title’s `href` attribute, linking back to the homepage, e.g. `/`.
For multilingual sites this will include the current locale, e.g. `/en/` or `/zh-cn/`.

#### `slug`

**Type:** `string`
Expand Down
7 changes: 2 additions & 5 deletions packages/starlight/components/SiteTitle.astro
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@
import { logos } from 'virtual:starlight/user-images';
import config from 'virtual:starlight/user-config';
import type { Props } from '../props';
import { formatPath } from '../utils/format-path';

const { siteTitle } = Astro.props;
const href = formatPath(Astro.props.locale || '/');
const { siteTitle, siteTitleHref } = Astro.props;
---

<a {href} class="site-title sl-flex">
<a href={siteTitleHref} class="site-title sl-flex">
{
config.logo && logos.dark && (
<>
Expand Down
8 changes: 8 additions & 0 deletions packages/starlight/utils/route-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { ensureTrailingSlash } from './path';
import type { Route } from './routing';
import { localizedId } from './slugs';
import { useTranslations } from './translations';
import { formatPath } from './format-path';

export interface PageProps extends Route {
headings: MarkdownHeading[];
Expand All @@ -17,6 +18,8 @@ export interface PageProps extends Route {
export interface StarlightRouteData extends Route {
/** Title of the site. */
siteTitle: string;
/** URL or path used as the link when clicking on the site title. */
siteTitleHref: string;
/** Array of Markdown headings extracted from the current page. */
headings: MarkdownHeading[];
/** Site navigation sidebar entries for this page. */
Expand Down Expand Up @@ -48,6 +51,7 @@ export function generateRouteData({
return {
...props,
siteTitle,
siteTitleHref: getSiteTitleHref(locale),
sidebar,
hasSidebar: entry.data.template !== 'splash',
pagination: getPrevNextLinks(sidebar, config.pagination, entry.data),
Expand Down Expand Up @@ -118,3 +122,7 @@ export function getSiteTitle(lang: string): string {
}
return config.title[defaultLang] as string;
}

export function getSiteTitleHref(locale: string | undefined): string {
return formatPath(locale || '/');
}
9 changes: 8 additions & 1 deletion packages/starlight/utils/starlight-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ import { type ContentConfig, type SchemaContext } from 'astro:content';
import config from 'virtual:starlight/user-config';
import { parseWithFriendlyErrors } from './error-map';
import { stripLeadingAndTrailingSlashes } from './path';
import { getSiteTitle, getToC, type PageProps, type StarlightRouteData } from './route-data';
import {
getSiteTitle,
getSiteTitleHref,
getToC,
type PageProps,
type StarlightRouteData,
} from './route-data';
import type { StarlightDocsEntry } from './routing';
import { slugToLocaleData, urlToSlug } from './slugs';
import { getPrevNextLinks, getSidebar } from './navigation';
Expand Down Expand Up @@ -224,6 +230,7 @@ export async function generateStarlightPageRouteData({
pagination: getPrevNextLinks(sidebar, config.pagination, entry.data),
sidebar,
siteTitle: getSiteTitle(localeData.lang),
siteTitleHref: getSiteTitleHref(localeData.locale),
slug,
toc: getToC({
...routeProps,
Expand Down