From 1c0fc3849771713d5a3e7a572bdbf1483ae5551b Mon Sep 17 00:00:00 2001 From: HiDeoo <494699+HiDeoo@users.noreply.github.com> Date: Wed, 1 May 2024 17:51:26 +0200 Subject: [PATCH] Add missing `siteTitle` property to Starlight pages route data (#1812) --- .changeset/six-ravens-tap.md | 5 +++++ .../basics/starlight-page-route-data.test.ts | 18 ++++++++++++++++++ packages/starlight/utils/route-data.ts | 2 +- packages/starlight/utils/starlight-page.ts | 3 ++- 4 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 .changeset/six-ravens-tap.md diff --git a/.changeset/six-ravens-tap.md b/.changeset/six-ravens-tap.md new file mode 100644 index 00000000000..aeac6efa7d1 --- /dev/null +++ b/.changeset/six-ravens-tap.md @@ -0,0 +1,5 @@ +--- +'@astrojs/starlight': patch +--- + +Fixes an issue where the `siteTitle` property would not be set when using the `` component. diff --git a/packages/starlight/__tests__/basics/starlight-page-route-data.test.ts b/packages/starlight/__tests__/basics/starlight-page-route-data.test.ts index 95f611e255c..e5164e04108 100644 --- a/packages/starlight/__tests__/basics/starlight-page-route-data.test.ts +++ b/packages/starlight/__tests__/basics/starlight-page-route-data.test.ts @@ -1,4 +1,6 @@ import { assert, expect, test, vi } from 'vitest'; +import { generateRouteData } from '../../utils/route-data'; +import { routes } from '../../utils/routing'; import { generateStarlightPageRouteData, type StarlightPageProps, @@ -46,6 +48,7 @@ test('adds data to route shape', async () => { // Starlight pages respect the passed data. expect(data.entry.data.title).toBe(starlightPageProps.frontmatter.title); // Starlight pages get expected defaults. + expect(data.siteTitle).toBe('Basics'); expect(data.hasSidebar).toBe(true); expect(data.headings).toEqual([]); expect(data.entryMeta.dir).toBe('ltr'); @@ -494,3 +497,18 @@ test('strips unknown frontmatter properties', async () => { }); expect('unknown' in data.entry.data).toBe(false); }); + +test('generates data with a similar root shape to regular route data', async () => { + const route = routes[0]!; + const data = generateRouteData({ + props: { ...route, headings: [{ depth: 1, slug: 'heading-1', text: 'Heading 1' }] }, + url: new URL('https://example.com'), + }); + + const starlightPageData = await generateStarlightPageRouteData({ + props: starlightPageProps, + url: starlightPageUrl, + }); + + expect(Object.keys(data).sort()).toEqual(Object.keys(starlightPageData).sort()); +}); diff --git a/packages/starlight/utils/route-data.ts b/packages/starlight/utils/route-data.ts index f9c6717de98..a47e68f262f 100644 --- a/packages/starlight/utils/route-data.ts +++ b/packages/starlight/utils/route-data.ts @@ -111,7 +111,7 @@ function getEditUrl({ entry, id, isFallback }: PageProps): URL | undefined { } /** Get the site title for a given language. **/ -function getSiteTitle(lang: string): string { +export function getSiteTitle(lang: string): string { const defaultLang = config.defaultLocale.lang as string; if (lang && config.title[lang]) { return config.title[lang] as string; diff --git a/packages/starlight/utils/starlight-page.ts b/packages/starlight/utils/starlight-page.ts index 88fbef872cc..e736457bbb5 100644 --- a/packages/starlight/utils/starlight-page.ts +++ b/packages/starlight/utils/starlight-page.ts @@ -3,7 +3,7 @@ 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 { getToC, type PageProps, type StarlightRouteData } from './route-data'; +import { getSiteTitle, getToC, type PageProps, type StarlightRouteData } from './route-data'; import type { StarlightDocsEntry } from './routing'; import { slugToLocaleData, urlToSlug } from './slugs'; import { getPrevNextLinks, getSidebar } from './navigation'; @@ -223,6 +223,7 @@ export async function generateStarlightPageRouteData({ lastUpdated, pagination: getPrevNextLinks(sidebar, config.pagination, entry.data), sidebar, + siteTitle: getSiteTitle(localeData.lang), slug, toc: getToC({ ...routeProps,