From 960139c3d065e67b083d5cc25f8123fff4ce4369 Mon Sep 17 00:00:00 2001 From: HiDeoo <494699+HiDeoo@users.noreply.github.com> Date: Thu, 5 Sep 2024 12:07:34 +0200 Subject: [PATCH 1/4] feat: translatable sidebar badges --- .changeset/slow-flowers-sort.md | 5 ++ docs/src/components/sidebar-preview.astro | 5 +- docs/src/content/docs/guides/sidebar.mdx | 59 ++++++++++++++++ .../i18n-sidebar-badge-error.test.ts | 20 ++++++ .../i18n-sidebar-badge-error/vitest.config.ts | 15 ++++ .../i18n-sidebar-fallback-slug.test.ts | 30 ++++++-- .../i18n-sidebar/i18n-sidebar.test.ts | 45 +++++++++--- .../__tests__/i18n-sidebar/vitest.config.ts | 17 ++++- packages/starlight/schemas/badge.ts | 32 ++++++--- packages/starlight/schemas/sidebar.ts | 6 +- packages/starlight/utils/navigation.ts | 70 ++++++++++++++++--- 11 files changed, 264 insertions(+), 40 deletions(-) create mode 100644 .changeset/slow-flowers-sort.md create mode 100644 packages/starlight/__tests__/i18n-sidebar-badge-error/i18n-sidebar-badge-error.test.ts create mode 100644 packages/starlight/__tests__/i18n-sidebar-badge-error/vitest.config.ts diff --git a/.changeset/slow-flowers-sort.md b/.changeset/slow-flowers-sort.md new file mode 100644 index 00000000000..c93a151fb03 --- /dev/null +++ b/.changeset/slow-flowers-sort.md @@ -0,0 +1,5 @@ +--- +'@astrojs/starlight': minor +--- + +Adds support for translating sidebar badges. diff --git a/docs/src/components/sidebar-preview.astro b/docs/src/components/sidebar-preview.astro index 810ac1cc925..b7761defa40 100644 --- a/docs/src/components/sidebar-preview.astro +++ b/docs/src/components/sidebar-preview.astro @@ -5,13 +5,16 @@ import type { InternalSidebarLinkItem, } from '../../../packages/starlight/schemas/sidebar'; import SidebarSublist from '../../../packages/starlight/components/SidebarSublist.astro'; +import type { Badge } from '../../../packages/starlight/schemas/badge'; import type { SidebarEntry } from '../../../packages/starlight/utils/navigation'; interface Props { config: SidebarConfig; } -type SidebarConfig = Exclude[]; +type SidebarConfig = (Exclude & { + badge?: Badge; +})[]; const { config } = Astro.props; diff --git a/docs/src/content/docs/guides/sidebar.mdx b/docs/src/content/docs/guides/sidebar.mdx index 8e368b15f5c..541dba9ca44 100644 --- a/docs/src/content/docs/guides/sidebar.mdx +++ b/docs/src/content/docs/guides/sidebar.mdx @@ -520,6 +520,65 @@ Browsing the documentation in Brazilian Portuguese will generate the following s In multilingual sites, the value of `slug` does not include the language portion of the URL. For example, if you have pages at `en/intro` and `pt-br/intro`, the slug is `intro` when configuring the sidebar. +### Internationalization with badges + +For [badges](#badges), the `badge` property or the `text` when using the object form can be a string, or for multilingual sites, an object with values for each different locale. +When using the object form, the keys must be [BCP-47](https://www.w3.org/International/questions/qa-choosing-language-tags) tags (e.g. `en`, `ar`, or `zh-CN`): + +```js {11-14,19-22} +starlight({ + sidebar: [ + { + label: 'Constelações', + translations: { + 'pt-BR': 'Constelações', + }, + items: [ + { + slug: 'constellations/andromeda', + badge: { + en: 'New', + 'pt-BR': 'Novo', + }, + }, + { + slug: 'constellations/scorpius', + badge: { + text: { + en: 'Outdated', + 'pt-BR': 'Descontinuado', + }, + variant: 'caution', + }, + }, + ], + }, + ], +}); +``` + +Browsing the documentation in Brazilian Portuguese will generate the following sidebar: + + + ## Collapsing groups Groups of links can be collapsed by default by setting the `collapsed` property to `true`. diff --git a/packages/starlight/__tests__/i18n-sidebar-badge-error/i18n-sidebar-badge-error.test.ts b/packages/starlight/__tests__/i18n-sidebar-badge-error/i18n-sidebar-badge-error.test.ts new file mode 100644 index 00000000000..fddea454474 --- /dev/null +++ b/packages/starlight/__tests__/i18n-sidebar-badge-error/i18n-sidebar-badge-error.test.ts @@ -0,0 +1,20 @@ +import { describe, expect, test, vi } from 'vitest'; +import { getSidebar } from '../../utils/navigation'; + +vi.mock('astro:content', async () => + (await import('../test-utils')).mockedAstroContent({ + docs: [['getting-started.mdx', { title: 'Getting Started' }]], + }) +); + +describe('getSidebar', () => { + test('throws an error if an i18n badge doesn’t have a key for the default language', () => { + expect(() => getSidebar('/', undefined)).toThrowErrorMatchingInlineSnapshot(` + "[AstroUserError]: + The badge text for "Getting Started" must have a key for the default language "en-US". + Hint: + Update the Starlight config to include a badge text for the default language. + Learn more about sidebar badges internationalization at https://starlight.astro.build/guides/sidebar/#internationalization-with-badges" + `); + }); +}); diff --git a/packages/starlight/__tests__/i18n-sidebar-badge-error/vitest.config.ts b/packages/starlight/__tests__/i18n-sidebar-badge-error/vitest.config.ts new file mode 100644 index 00000000000..3fa8dee3d7a --- /dev/null +++ b/packages/starlight/__tests__/i18n-sidebar-badge-error/vitest.config.ts @@ -0,0 +1,15 @@ +import { defineVitestConfig } from '../test-config'; + +export default defineVitestConfig({ + title: 'i18n sidebar badge error', + locales: { + fr: { label: 'French' }, + root: { label: 'English', lang: 'en-US' }, + }, + sidebar: [ + { + slug: 'getting-started', + badge: { fr: 'Nouveau' }, + }, + ], +}); diff --git a/packages/starlight/__tests__/i18n-sidebar/i18n-sidebar-fallback-slug.test.ts b/packages/starlight/__tests__/i18n-sidebar/i18n-sidebar-fallback-slug.test.ts index e67e558eeab..db1a5fb6fe5 100644 --- a/packages/starlight/__tests__/i18n-sidebar/i18n-sidebar-fallback-slug.test.ts +++ b/packages/starlight/__tests__/i18n-sidebar/i18n-sidebar-fallback-slug.test.ts @@ -37,7 +37,10 @@ describe('getSidebar', () => { }, { "attrs": {}, - "badge": undefined, + "badge": { + "text": "New", + "variant": "default", + }, "href": "/manual-setup", "isCurrent": false, "label": "Do it yourself", @@ -45,7 +48,10 @@ describe('getSidebar', () => { }, { "attrs": {}, - "badge": undefined, + "badge": { + "text": "Eco-friendly", + "variant": "success", + }, "href": "/environmental-impact", "isCurrent": false, "label": "Eco-friendly docs", @@ -65,7 +71,10 @@ describe('getSidebar', () => { }, { "attrs": {}, - "badge": undefined, + "badge": { + "text": "Deprecated", + "variant": "default", + }, "href": "/guides/authoring-content", "isCurrent": false, "label": "Authoring Content in Markdown", @@ -107,7 +116,10 @@ describe('getSidebar', () => { }, { "attrs": {}, - "badge": undefined, + "badge": { + "text": "Nouveau", + "variant": "default", + }, "href": "/fr/manual-setup", "isCurrent": false, "label": "Fait maison", @@ -115,7 +127,10 @@ describe('getSidebar', () => { }, { "attrs": {}, - "badge": undefined, + "badge": { + "text": "Écologique", + "variant": "success", + }, "href": "/fr/environmental-impact", "isCurrent": false, "label": "Eco-friendly docs", @@ -135,7 +150,10 @@ describe('getSidebar', () => { }, { "attrs": {}, - "badge": undefined, + "badge": { + "text": "Deprecated", + "variant": "default", + }, "href": "/fr/guides/authoring-content", "isCurrent": false, "label": "Authoring Content in Markdown", diff --git a/packages/starlight/__tests__/i18n-sidebar/i18n-sidebar.test.ts b/packages/starlight/__tests__/i18n-sidebar/i18n-sidebar.test.ts index dad2b4133c6..8ae38d9c362 100644 --- a/packages/starlight/__tests__/i18n-sidebar/i18n-sidebar.test.ts +++ b/packages/starlight/__tests__/i18n-sidebar/i18n-sidebar.test.ts @@ -44,7 +44,10 @@ describe('getSidebar', () => { }, { "attrs": {}, - "badge": undefined, + "badge": { + "text": "New", + "variant": "default", + }, "href": "/manual-setup", "isCurrent": false, "label": "Do it yourself", @@ -52,7 +55,10 @@ describe('getSidebar', () => { }, { "attrs": {}, - "badge": undefined, + "badge": { + "text": "Eco-friendly", + "variant": "success", + }, "href": "/environmental-impact", "isCurrent": false, "label": "Eco-friendly docs", @@ -72,7 +78,10 @@ describe('getSidebar', () => { }, { "attrs": {}, - "badge": undefined, + "badge": { + "text": "Deprecated", + "variant": "default", + }, "href": "/guides/authoring-content", "isCurrent": false, "label": "Authoring Content in Markdown", @@ -114,7 +123,10 @@ describe('getSidebar', () => { }, { "attrs": {}, - "badge": undefined, + "badge": { + "text": "Nouveau", + "variant": "default", + }, "href": "/fr/manual-setup", "isCurrent": false, "label": "Fait maison", @@ -122,7 +134,10 @@ describe('getSidebar', () => { }, { "attrs": {}, - "badge": undefined, + "badge": { + "text": "Écologique", + "variant": "success", + }, "href": "/fr/environmental-impact", "isCurrent": false, "label": "Documents écologiques", @@ -142,7 +157,10 @@ describe('getSidebar', () => { }, { "attrs": {}, - "badge": undefined, + "badge": { + "text": "Deprecated", + "variant": "default", + }, "href": "/fr/guides/authoring-content", "isCurrent": false, "label": "Création de contenu en Markdown", @@ -184,7 +202,10 @@ describe('getSidebar', () => { }, { "attrs": {}, - "badge": undefined, + "badge": { + "text": "Nouveau", + "variant": "default", + }, "href": "/fr/manual-setup", "isCurrent": false, "label": "Fait maison", @@ -192,7 +213,10 @@ describe('getSidebar', () => { }, { "attrs": {}, - "badge": undefined, + "badge": { + "text": "Écologique", + "variant": "success", + }, "href": "/fr/environmental-impact", "isCurrent": false, "label": "Documents écologiques", @@ -212,7 +236,10 @@ describe('getSidebar', () => { }, { "attrs": {}, - "badge": undefined, + "badge": { + "text": "Deprecated", + "variant": "default", + }, "href": "/fr/guides/authoring-content", "isCurrent": false, "label": "Création de contenu en Markdown", diff --git a/packages/starlight/__tests__/i18n-sidebar/vitest.config.ts b/packages/starlight/__tests__/i18n-sidebar/vitest.config.ts index 59f3bdf141f..cf302448004 100644 --- a/packages/starlight/__tests__/i18n-sidebar/vitest.config.ts +++ b/packages/starlight/__tests__/i18n-sidebar/vitest.config.ts @@ -9,11 +9,22 @@ export default defineVitestConfig({ sidebar: [ { slug: 'index' }, 'getting-started', - { slug: 'manual-setup', label: 'Do it yourself', translations: { fr: 'Fait maison' } }, - { slug: 'environmental-impact' }, + { + slug: 'manual-setup', + label: 'Do it yourself', + translations: { fr: 'Fait maison' }, + badge: { 'en-US': 'New', fr: 'Nouveau' }, + }, + { + slug: 'environmental-impact', + badge: { + variant: 'success', + text: { 'en-US': 'Eco-friendly', fr: 'Écologique' }, + }, + }, { label: 'Guides', - items: [{ slug: 'guides/pages' }, { slug: 'guides/authoring-content' }], + items: [{ slug: 'guides/pages' }, { slug: 'guides/authoring-content', badge: 'Deprecated' }], }, 'resources/plugins', ], diff --git a/packages/starlight/schemas/badge.ts b/packages/starlight/schemas/badge.ts index a2cac1b177a..a3a6eedf09a 100644 --- a/packages/starlight/schemas/badge.ts +++ b/packages/starlight/schemas/badge.ts @@ -1,13 +1,20 @@ import { z } from 'astro/zod'; -const badgeSchema = () => - z.object({ - variant: z.enum(['note', 'danger', 'success', 'caution', 'tip', 'default']).default('default'), - text: z.string(), - class: z.string().optional(), - }); - -export const BadgeComponentSchema = badgeSchema() +const badgeBaseSchema = z.object({ + variant: z.enum(['note', 'danger', 'success', 'caution', 'tip', 'default']).default('default'), + class: z.string().optional(), +}); + +const badgeSchema = badgeBaseSchema.extend({ + text: z.string(), +}); + +const i18nBadgeTextSchema = z.union([z.string(), z.record(z.string())]); +const i18nBadgeSchema = badgeBaseSchema.extend({ + text: i18nBadgeTextSchema, +}); + +export const BadgeComponentSchema = badgeSchema .extend({ size: z.enum(['small', 'medium', 'large']).default('small'), }) @@ -17,7 +24,7 @@ export type BadgeComponentProps = z.input; export const BadgeConfigSchema = () => z - .union([z.string(), badgeSchema()]) + .union([z.string(), badgeSchema]) .transform((badge) => { if (typeof badge === 'string') { return { variant: 'default' as const, text: badge }; @@ -26,4 +33,9 @@ export const BadgeConfigSchema = () => }) .optional(); -export type Badge = z.output>; +export const I18nBadgeConfigSchema = () => + z.union([i18nBadgeTextSchema, i18nBadgeSchema]).optional(); + +export type Badge = z.output; +export type I18nBadge = z.output; +export type I18nBadgeConfig = z.output>; diff --git a/packages/starlight/schemas/sidebar.ts b/packages/starlight/schemas/sidebar.ts index b71d159ed44..2c4cc1d588f 100644 --- a/packages/starlight/schemas/sidebar.ts +++ b/packages/starlight/schemas/sidebar.ts @@ -1,7 +1,7 @@ import type { AstroBuiltinAttributes } from 'astro'; import type { HTMLAttributes } from 'astro/types'; import { z } from 'astro/zod'; -import { BadgeConfigSchema } from './badge'; +import { I18nBadgeConfigSchema } from './badge'; import { stripLeadingAndTrailingSlashes } from '../utils/path'; const SidebarBaseSchema = z.object({ @@ -9,8 +9,8 @@ const SidebarBaseSchema = z.object({ label: z.string(), /** Translations of the `label` for each supported language. */ translations: z.record(z.string()).default({}), - /** Adds a badge to the link item */ - badge: BadgeConfigSchema(), + /** Adds a badge to the item */ + badge: I18nBadgeConfigSchema(), }); const SidebarGroupSchema = SidebarBaseSchema.extend({ diff --git a/packages/starlight/utils/navigation.ts b/packages/starlight/utils/navigation.ts index cd1475d1522..dadf48de2d3 100644 --- a/packages/starlight/utils/navigation.ts +++ b/packages/starlight/utils/navigation.ts @@ -1,6 +1,6 @@ import { AstroError } from 'astro/errors'; import config from 'virtual:starlight/user-config'; -import type { Badge } from '../schemas/badge'; +import type { Badge, I18nBadge, I18nBadgeConfig } from '../schemas/badge'; import type { PrevNextLinkConfig } from '../schemas/prevNextLink'; import type { AutoSidebarGroup, @@ -11,7 +11,7 @@ import type { } from '../schemas/sidebar'; import { createPathFormatter } from './createPathFormatter'; import { formatPath } from './format-path'; -import { pickLang } from './i18n'; +import { BuiltInDefaultLocale, pickLang } from './i18n'; import { ensureLeadingSlash, ensureTrailingSlash, stripLeadingAndTrailingSlashes } from './path'; import { getLocaleRoutes, routes, type Route } from './routing'; import { localeToLang, slugToPathname } from './slugs'; @@ -79,12 +79,13 @@ function configItemToEntry( } else if ('slug' in item) { return linkFromInternalSidebarLinkItem(item, locale, currentPathname); } else { + const label = pickLang(item.translations, localeToLang(locale)) || item.label; return { type: 'group', - label: pickLang(item.translations, localeToLang(locale)) || item.label, + label, entries: item.items.map((i) => configItemToEntry(i, currentPathname, locale, routes)), collapsed: item.collapsed, - badge: item.badge, + badge: getSidebarBadge(item.badge, locale, label), }; } } @@ -106,12 +107,13 @@ function groupFromAutogenerateConfig( doc.id.startsWith(localeDir + '/') ); const tree = treeify(dirDocs, localeDir); + const label = pickLang(item.translations, localeToLang(locale)) || item.label; return { type: 'group', - label: pickLang(item.translations, localeToLang(locale)) || item.label, + label, entries: sidebarFromDir(tree, currentPathname, locale, subgroupCollapsed ?? item.collapsed), collapsed: item.collapsed, - badge: item.badge, + badge: getSidebarBadge(item.badge, locale, label), }; } @@ -131,7 +133,13 @@ function linkFromSidebarLinkItem( if (locale) href = '/' + locale + href; } const label = pickLang(item.translations, localeToLang(locale)) || item.label; - return makeSidebarLink(href, label, currentPathname, item.badge, item.attrs); + return makeSidebarLink( + href, + label, + currentPathname, + getSidebarBadge(item.badge, locale, label), + item.attrs + ); } /** Create a link entry from an automatic internal link item in user config. */ @@ -161,7 +169,13 @@ function linkFromInternalSidebarLinkItem( } const label = pickLang(item.translations, localeToLang(locale)) || item.label || entry.entry.data.title; - return makeSidebarLink(entry.slug, label, currentPathname, item.badge, item.attrs); + return makeSidebarLink( + entry.slug, + label, + currentPathname, + getSidebarBadge(item.badge, locale, label), + item.attrs + ); } /** Process sidebar link options to create a link entry. */ @@ -446,3 +460,43 @@ function stripExtension(path: string) { const periodIndex = path.lastIndexOf('.'); return path.slice(0, periodIndex > -1 ? periodIndex : undefined); } + +/** Get a sidebar badge for a given item. */ +function getSidebarBadge( + config: I18nBadgeConfig, + locale: string | undefined, + itemLabel: string +): Badge | undefined { + if (!config) return; + if (isI18nBadge(config)) { + return { ...config, text: getSidebarBadgeText(config.text, locale, itemLabel) }; + } + return { variant: 'default', text: getSidebarBadgeText(config, locale, itemLabel) }; +} + +/** Get the badge text for a sidebar item. */ +function getSidebarBadgeText( + text: string | Record, + locale: string | undefined, + itemLabel: string +): string { + if (typeof text === 'string') return text; + const defaultLang = + config.defaultLocale?.lang || config.defaultLocale?.locale || BuiltInDefaultLocale.lang; + const defaultText = text[defaultLang]; + + if (!defaultText) { + throw new AstroError( + `The badge text for "${itemLabel}" must have a key for the default language "${defaultLang}".`, + 'Update the Starlight config to include a badge text for the default language.\n' + + 'Learn more about sidebar badges internationalization at https://starlight.astro.build/guides/sidebar/#internationalization-with-badges' + ); + } + + return pickLang(text, localeToLang(locale)) || defaultText; +} + +/** Check if a badge config is a badge with internationalized text. */ +function isI18nBadge(config: I18nBadgeConfig): config is I18nBadge { + return typeof config === 'object' && 'text' in config && 'variant' in config; +} From e5a5d3376d31058c404f2b035e9e7b7997d5031b Mon Sep 17 00:00:00 2001 From: HiDeoo <494699+HiDeoo@users.noreply.github.com> Date: Mon, 9 Sep 2024 11:08:42 +0200 Subject: [PATCH 2/4] feat: remove translatable sidebar badge shorthand syntax --- docs/src/content/docs/guides/sidebar.mdx | 23 ++++--------------- .../i18n-sidebar-badge-error/vitest.config.ts | 2 +- .../__tests__/i18n-sidebar/vitest.config.ts | 2 +- packages/starlight/schemas/badge.ts | 6 ++--- packages/starlight/utils/navigation.ts | 13 ++++------- 5 files changed, 13 insertions(+), 33 deletions(-) diff --git a/docs/src/content/docs/guides/sidebar.mdx b/docs/src/content/docs/guides/sidebar.mdx index 541dba9ca44..35500284651 100644 --- a/docs/src/content/docs/guides/sidebar.mdx +++ b/docs/src/content/docs/guides/sidebar.mdx @@ -522,33 +522,25 @@ For example, if you have pages at `en/intro` and `pt-br/intro`, the slug is `int ### Internationalization with badges -For [badges](#badges), the `badge` property or the `text` when using the object form can be a string, or for multilingual sites, an object with values for each different locale. +For [badges](#badges), the `text` property can be a string, or for multilingual sites, an object with values for each different locale. When using the object form, the keys must be [BCP-47](https://www.w3.org/International/questions/qa-choosing-language-tags) tags (e.g. `en`, `ar`, or `zh-CN`): -```js {11-14,19-22} +```js {11-16} starlight({ sidebar: [ { - label: 'Constelações', + label: 'Constellations', translations: { 'pt-BR': 'Constelações', }, items: [ { slug: 'constellations/andromeda', - badge: { - en: 'New', - 'pt-BR': 'Novo', - }, - }, - { - slug: 'constellations/scorpius', badge: { text: { - en: 'Outdated', - 'pt-BR': 'Descontinuado', + en: 'New', + 'pt-BR': 'Novo', }, - variant: 'caution', }, }, ], @@ -569,11 +561,6 @@ Browsing the documentation in Brazilian Portuguese will generate the following s link: '', badge: { text: 'Novo', variant: 'default' }, }, - { - label: 'Escorpião', - link: '', - badge: { text: 'Descontinuado', variant: 'caution' }, - }, ], }, ]} diff --git a/packages/starlight/__tests__/i18n-sidebar-badge-error/vitest.config.ts b/packages/starlight/__tests__/i18n-sidebar-badge-error/vitest.config.ts index 3fa8dee3d7a..a741e609357 100644 --- a/packages/starlight/__tests__/i18n-sidebar-badge-error/vitest.config.ts +++ b/packages/starlight/__tests__/i18n-sidebar-badge-error/vitest.config.ts @@ -9,7 +9,7 @@ export default defineVitestConfig({ sidebar: [ { slug: 'getting-started', - badge: { fr: 'Nouveau' }, + badge: { text: { fr: 'Nouveau' } }, }, ], }); diff --git a/packages/starlight/__tests__/i18n-sidebar/vitest.config.ts b/packages/starlight/__tests__/i18n-sidebar/vitest.config.ts index cf302448004..6cdb4d746f6 100644 --- a/packages/starlight/__tests__/i18n-sidebar/vitest.config.ts +++ b/packages/starlight/__tests__/i18n-sidebar/vitest.config.ts @@ -13,7 +13,7 @@ export default defineVitestConfig({ slug: 'manual-setup', label: 'Do it yourself', translations: { fr: 'Fait maison' }, - badge: { 'en-US': 'New', fr: 'Nouveau' }, + badge: { text: { 'en-US': 'New', fr: 'Nouveau' } }, }, { slug: 'environmental-impact', diff --git a/packages/starlight/schemas/badge.ts b/packages/starlight/schemas/badge.ts index a3a6eedf09a..f3f1f738f68 100644 --- a/packages/starlight/schemas/badge.ts +++ b/packages/starlight/schemas/badge.ts @@ -9,9 +9,8 @@ const badgeSchema = badgeBaseSchema.extend({ text: z.string(), }); -const i18nBadgeTextSchema = z.union([z.string(), z.record(z.string())]); const i18nBadgeSchema = badgeBaseSchema.extend({ - text: i18nBadgeTextSchema, + text: z.union([z.string(), z.record(z.string())]), }); export const BadgeComponentSchema = badgeSchema @@ -33,8 +32,7 @@ export const BadgeConfigSchema = () => }) .optional(); -export const I18nBadgeConfigSchema = () => - z.union([i18nBadgeTextSchema, i18nBadgeSchema]).optional(); +export const I18nBadgeConfigSchema = () => z.union([z.string(), i18nBadgeSchema]).optional(); export type Badge = z.output; export type I18nBadge = z.output; diff --git a/packages/starlight/utils/navigation.ts b/packages/starlight/utils/navigation.ts index dadf48de2d3..526d9f29ddc 100644 --- a/packages/starlight/utils/navigation.ts +++ b/packages/starlight/utils/navigation.ts @@ -468,15 +468,15 @@ function getSidebarBadge( itemLabel: string ): Badge | undefined { if (!config) return; - if (isI18nBadge(config)) { - return { ...config, text: getSidebarBadgeText(config.text, locale, itemLabel) }; + if (typeof config === 'string') { + return { variant: 'default', text: config }; } - return { variant: 'default', text: getSidebarBadgeText(config, locale, itemLabel) }; + return { ...config, text: getSidebarBadgeText(config.text, locale, itemLabel) }; } /** Get the badge text for a sidebar item. */ function getSidebarBadgeText( - text: string | Record, + text: I18nBadge['text'], locale: string | undefined, itemLabel: string ): string { @@ -495,8 +495,3 @@ function getSidebarBadgeText( return pickLang(text, localeToLang(locale)) || defaultText; } - -/** Check if a badge config is a badge with internationalized text. */ -function isI18nBadge(config: I18nBadgeConfig): config is I18nBadge { - return typeof config === 'object' && 'text' in config && 'variant' in config; -} From fd48b6b64ad3df725acd7a58e0df97774b727798 Mon Sep 17 00:00:00 2001 From: HiDeoo <494699+HiDeoo@users.noreply.github.com> Date: Wed, 11 Sep 2024 18:00:42 +0200 Subject: [PATCH 3/4] test: update coverage thresholds --- packages/starlight/vitest.config.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/starlight/vitest.config.ts b/packages/starlight/vitest.config.ts index e15ce18747c..4b23b0fe138 100644 --- a/packages/starlight/vitest.config.ts +++ b/packages/starlight/vitest.config.ts @@ -21,10 +21,10 @@ export default defineConfig({ ], thresholds: { autoUpdate: true, - lines: 89.53, - functions: 94.08, - branches: 93.07, - statements: 89.53, + lines: 89.68, + functions: 94.21, + branches: 92.83, + statements: 89.68, }, }, }, From dc863a80240a1d0b92f10d45f81414e40d5a999b Mon Sep 17 00:00:00 2001 From: Chris Swithinbank Date: Wed, 18 Sep 2024 10:01:05 +0200 Subject: [PATCH 4/4] Update Vitest thresholds --- packages/starlight/vitest.config.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/starlight/vitest.config.ts b/packages/starlight/vitest.config.ts index 47ca99be5ea..d94437430c5 100644 --- a/packages/starlight/vitest.config.ts +++ b/packages/starlight/vitest.config.ts @@ -21,10 +21,10 @@ export default defineConfig({ ], thresholds: { autoUpdate: true, - lines: 89.18, - functions: 92.7, - branches: 93.04, - statements: 89.18, + lines: 89.28, + functions: 92.78, + branches: 92.83, + statements: 89.28, }, }, },