Skip to content

Commit

Permalink
Add missing siteTitle property to Starlight pages route data (#1812)
Browse files Browse the repository at this point in the history
  • Loading branch information
HiDeoo authored May 1, 2024
1 parent 0e4d4f4 commit 1c0fc38
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/six-ravens-tap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/starlight': patch
---

Fixes an issue where the `siteTitle` property would not be set when using the `<StarlightPage />` component.
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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());
});
2 changes: 1 addition & 1 deletion packages/starlight/utils/route-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion packages/starlight/utils/starlight-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 1c0fc38

Please sign in to comment.