@astrojs/starlight 0.28.3 → 0.28.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,29 @@
1
1
  # @astrojs/starlight
2
2
 
3
+ ## 0.28.5
4
+
5
+ ### Patch Changes
6
+
7
+ - [#2546](https://github.com/withastro/starlight/pull/2546) [`bf42300`](https://github.com/withastro/starlight/commit/bf42300e76241a2df888dc458c59a7478a8b2d61) Thanks [@HiDeoo](https://github.com/HiDeoo)! - Fixes an issue where i18n content collection related errors, e.g. malformed JSON or YAML, would not be reported.
8
+
9
+ - [#2548](https://github.com/withastro/starlight/pull/2548) [`07673c8`](https://github.com/withastro/starlight/commit/07673c80114021a269065e451e660337237f76e1) Thanks [@HiDeoo](https://github.com/HiDeoo)! - Fixes a URL localization edge case. In projects without a root locale configured, slugs without a locale prefix did not fall back to the default locale as expected.
10
+
11
+ - [#2547](https://github.com/withastro/starlight/pull/2547) [`91e1dd7`](https://github.com/withastro/starlight/commit/91e1dd731a06657890a68b2d474199455df2756f) Thanks [@HiDeoo](https://github.com/HiDeoo)! - Fixes a Firefox Markdown content rendering issue for text sentences separated by a line break.
12
+
13
+ - [#2524](https://github.com/withastro/starlight/pull/2524) [`1b46783`](https://github.com/withastro/starlight/commit/1b4678325fb10714fc3508bd87a7563b10a0f803) Thanks [@jsparkdev](https://github.com/jsparkdev)! - Fixes a broken link to Astro’s Docs in an error message
14
+
15
+ ## 0.28.4
16
+
17
+ ### Patch Changes
18
+
19
+ - [#2444](https://github.com/withastro/starlight/pull/2444) [`d585b3e`](https://github.com/withastro/starlight/commit/d585b3e0485dd55b2ffab985a6c06d267d22fe51) Thanks [@HiDeoo](https://github.com/HiDeoo)! - Fixes a UI string translation issue for languages with a region subtag.
20
+
21
+ - [#2518](https://github.com/withastro/starlight/pull/2518) [`0f69db8`](https://github.com/withastro/starlight/commit/0f69db8b806833a7160570a469ddcdc8c0dec5e0) Thanks [@morinokami](https://github.com/morinokami)! - Updates Japanese UI translations
22
+
23
+ - [#2507](https://github.com/withastro/starlight/pull/2507) [`bd6ced5`](https://github.com/withastro/starlight/commit/bd6ced5bc46310b217c7bfe83a0f68ba4a03da45) Thanks [@HiDeoo](https://github.com/HiDeoo)! - Fixes a table of contents highlighting issue after resizing the window.
24
+
25
+ - [#2444](https://github.com/withastro/starlight/pull/2444) [`d585b3e`](https://github.com/withastro/starlight/commit/d585b3e0485dd55b2ffab985a6c06d267d22fe51) Thanks [@HiDeoo](https://github.com/HiDeoo)! - Refactors various components to use the new built-in localization system to access translated UI strings.
26
+
3
27
  ## 0.28.3
4
28
 
5
29
  ### Patch Changes
@@ -88,7 +88,10 @@ export class StarlightTOC extends HTMLElement {
88
88
  let timeout: NodeJS.Timeout;
89
89
  window.addEventListener('resize', () => {
90
90
  // Disable intersection observer while window is resizing.
91
- if (observer) observer.disconnect();
91
+ if (observer) {
92
+ observer.disconnect();
93
+ observer = undefined;
94
+ }
92
95
  clearTimeout(timeout);
93
96
  timeout = setTimeout(() => this.onIdle(observe), 200);
94
97
  });
@@ -17,9 +17,10 @@ import { visit } from 'unist-util-visit';
17
17
  import type { StarlightConfig } from '../types';
18
18
  import type { createTranslationSystemFromFs } from '../utils/translations-fs';
19
19
  import { pathToLocale } from './shared/pathToLocale';
20
+ import { localeToLang } from './shared/localeToLang';
20
21
 
21
22
  interface AsidesOptions {
22
- starlightConfig: { locales: StarlightConfig['locales'] };
23
+ starlightConfig: Pick<StarlightConfig, 'defaultLocale' | 'locales'>;
23
24
  astroConfig: { root: AstroConfig['root']; srcDir: AstroConfig['srcDir'] };
24
25
  useTranslations: ReturnType<typeof createTranslationSystemFromFs>;
25
26
  }
@@ -151,7 +152,8 @@ function remarkAsides(options: AsidesOptions): Plugin<[], Root> {
151
152
 
152
153
  const transformer: Transformer<Root> = (tree, file) => {
153
154
  const locale = pathToLocale(file.history[0], options);
154
- const t = options.useTranslations(locale);
155
+ const lang = localeToLang(options.starlightConfig, locale);
156
+ const t = options.useTranslations(lang);
155
157
  visit(tree, (node, index, parent) => {
156
158
  if (!parent || index === undefined || !isNodeDirective(node)) {
157
159
  return;
@@ -22,7 +22,7 @@ function addTranslationsForLocale(
22
22
  useTranslations: ReturnType<typeof createTranslationSystemFromFs>
23
23
  ) {
24
24
  const lang = localeToLang(config, locale);
25
- const t = useTranslations(locale);
25
+ const t = useTranslations(lang);
26
26
  const translationKeys = [
27
27
  'expressiveCode.copyButtonCopied',
28
28
  'expressiveCode.copyButtonTooltip',
@@ -5,7 +5,10 @@ import { BuiltInDefaultLocale } from '../../utils/i18n';
5
5
  * Get the BCP-47 language tag for the given locale.
6
6
  * @param locale Locale string or `undefined` for the root locale.
7
7
  */
8
- export function localeToLang(config: StarlightConfig, locale: string | undefined): string {
8
+ export function localeToLang(
9
+ config: Pick<StarlightConfig, 'defaultLocale' | 'locales'>,
10
+ locale: string | undefined
11
+ ): string {
9
12
  const lang = locale ? config.locales?.[locale]?.lang : config.locales?.root?.lang;
10
13
  const defaultLang = config.defaultLocale?.lang || config.defaultLocale?.locale;
11
14
  return lang || defaultLang || BuiltInDefaultLocale.lang;
@@ -1,14 +1,6 @@
1
1
  import type { AstroConfig } from 'astro';
2
2
  import type { StarlightConfig } from '../../types';
3
-
4
- function slugToLocale(
5
- slug: string | undefined,
6
- localesConfig: StarlightConfig['locales']
7
- ): string | undefined {
8
- const locales = Object.keys(localesConfig || {});
9
- const baseSegment = slug?.split('/')[0];
10
- return baseSegment && locales.includes(baseSegment) ? baseSegment : undefined;
11
- }
3
+ import { slugToLocale } from './slugToLocale';
12
4
 
13
5
  /** Get current locale from the full file path. */
14
6
  export function pathToLocale(
@@ -17,7 +9,7 @@ export function pathToLocale(
17
9
  starlightConfig,
18
10
  astroConfig,
19
11
  }: {
20
- starlightConfig: { locales: StarlightConfig['locales'] };
12
+ starlightConfig: Pick<StarlightConfig, 'defaultLocale' | 'locales'>;
21
13
  astroConfig: { root: AstroConfig['root']; srcDir: AstroConfig['srcDir'] };
22
14
  }
23
15
  ): string | undefined {
@@ -31,5 +23,5 @@ export function pathToLocale(
31
23
  // Strip docs path leaving only content collection file ID.
32
24
  // Example: /Users/houston/repo/src/content/docs/en/guide.md => en/guide.md
33
25
  const slug = path?.replace(docsDir.pathname, '');
34
- return slugToLocale(slug, starlightConfig.locales);
26
+ return slugToLocale(slug, starlightConfig);
35
27
  }
@@ -0,0 +1,18 @@
1
+ import type { StarlightConfig } from '../../types';
2
+
3
+ /**
4
+ * Get the “locale” of a slug. This is the base path at which a language is served.
5
+ * For example, if French docs are in `src/content/docs/french/`, the locale is `french`.
6
+ * Root locale slugs will return `undefined`.
7
+ * @param slug A collection entry slug
8
+ */
9
+ export function slugToLocale(
10
+ slug: string | undefined,
11
+ config: Pick<StarlightConfig, 'defaultLocale' | 'locales'>
12
+ ): string | undefined {
13
+ const localesConfig = config.locales ?? {};
14
+ const baseSegment = slug?.split('/')[0];
15
+ if (baseSegment && localesConfig[baseSegment]) return baseSegment;
16
+ if (!localesConfig.root) return config.defaultLocale.locale;
17
+ return undefined;
18
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@astrojs/starlight",
3
- "version": "0.28.3",
3
+ "version": "0.28.5",
4
4
  "description": "Build beautiful, high-performance documentation websites with Astro",
5
5
  "keywords": [
6
6
  "docs",
@@ -3,7 +3,6 @@ import { getEntry } from 'astro:content';
3
3
  import config from 'virtual:starlight/user-config';
4
4
  import EmptyContent from '../../components/EmptyMarkdown.md';
5
5
  import type { Route, StarlightDocsEntry } from '../../utils/routing';
6
- import { useTranslations } from '../../utils/translations';
7
6
  import { BuiltInDefaultLocale } from '../../utils/i18n';
8
7
  import CommonPage from '../common.astro';
9
8
 
@@ -15,7 +14,6 @@ let locale = config.defaultLocale?.locale;
15
14
  if (locale === 'root') locale = undefined;
16
15
 
17
16
  const entryMeta = { dir, lang, locale };
18
- const t = useTranslations(locale);
19
17
 
20
18
  const fallbackEntry: StarlightDocsEntry = {
21
19
  slug: '404',
@@ -27,7 +25,7 @@ const fallbackEntry: StarlightDocsEntry = {
27
25
  template: 'splash',
28
26
  editUrl: false,
29
27
  head: [],
30
- hero: { tagline: t('404.text'), actions: [] },
28
+ hero: { tagline: Astro.locals.t('404.text'), actions: [] },
31
29
  pagefind: false,
32
30
  sidebar: { hidden: false, attrs: {} },
33
31
  draft: false,
@@ -1,6 +1,6 @@
1
1
  .sl-markdown-content
2
- :not(a, strong, em, del, span, input, code)
3
- + :not(a, strong, em, del, span, input, code, :where(.not-content *)) {
2
+ :not(a, strong, em, del, span, input, code, br)
3
+ + :not(a, strong, em, del, span, input, code, br, :where(.not-content *)) {
4
4
  margin-top: 1rem;
5
5
  }
6
6
 
@@ -18,12 +18,12 @@
18
18
  "page.lastUpdated": "最終更新日:",
19
19
  "page.previousLink": "前へ",
20
20
  "page.nextLink": "次へ",
21
- "page.draft": "This content is a draft and will not be included in production builds.",
21
+ "page.draft": "このコンテンツは下書きです。プロダクションビルドには含まれません。",
22
22
  "404.text": "ページが見つかりません。 URL を確認するか、検索バーを使用してみてください。",
23
23
  "aside.note": "ノート",
24
24
  "aside.tip": "ヒント",
25
25
  "aside.caution": "注意",
26
26
  "aside.danger": "危険",
27
27
  "fileTree.directory": "ディレクトリ",
28
- "builtWithStarlight.label": "Built with Starlight"
28
+ "builtWithStarlight.label": "Starlightで作成"
29
29
  }
@@ -1,7 +1,5 @@
1
1
  ---
2
2
  import { AstroError } from 'astro/errors';
3
- import { slugToLocaleData, urlToSlug } from '../utils/slugs';
4
- import { useTranslations } from '../utils/translations';
5
3
  import Icon from './Icon.astro';
6
4
 
7
5
  const asideVariants = ['note', 'tip', 'caution', 'danger'] as const;
@@ -23,8 +21,7 @@ if (!asideVariants.includes(type)) {
23
21
  }
24
22
 
25
23
  if (!title) {
26
- const { locale } = slugToLocaleData(urlToSlug(Astro.url));
27
- title = useTranslations(locale)(`aside.${type}`);
24
+ title = Astro.locals.t(`aside.${type}`);
28
25
  }
29
26
  ---
30
27
 
@@ -1,14 +1,8 @@
1
1
  ---
2
- import { stripLeadingAndTrailingSlashes } from '../utils/path';
3
- import { slugToLocaleData } from '../utils/slugs';
4
- import { useTranslations } from '../utils/translations';
5
2
  import { processFileTree } from './rehype-file-tree';
6
3
 
7
- const slug = stripLeadingAndTrailingSlashes(Astro.url.pathname);
8
- const t = useTranslations(slugToLocaleData(slug).locale);
9
-
10
4
  const fileTreeHtml = await Astro.slots.render('default');
11
- const html = processFileTree(fileTreeHtml, t('fileTree.directory'));
5
+ const html = processFileTree(fileTreeHtml, Astro.locals.t('fileTree.directory'));
12
6
  ---
13
7
 
14
8
  <starlight-file-tree set:html={html} class="not-content" data-pagefind-ignore />
@@ -49,14 +49,14 @@ export function createTranslationSystem<T extends i18nSchemaOutput>(
49
49
  });
50
50
 
51
51
  /**
52
- * Generate a utility function that returns UI strings for the given `locale`.
52
+ * Generate a utility function that returns UI strings for the given language.
53
53
  *
54
54
  * Also includes a few utility methods:
55
55
  * - `all()` method for getting the entire dictionary.
56
56
  * - `exists()` method for checking if a key exists in the dictionary.
57
57
  * - `dir()` method for getting the text direction of the locale.
58
58
  *
59
- * @param {string | undefined} [locale]
59
+ * @param {string | undefined} [lang]
60
60
  * @example
61
61
  * const t = useTranslations('en');
62
62
  * const label = t('search.label');
@@ -68,8 +68,8 @@ export function createTranslationSystem<T extends i18nSchemaOutput>(
68
68
  * const dir = t.dir();
69
69
  * // => 'ltr'
70
70
  */
71
- return (locale: string | undefined) => {
72
- const lang = localeToLang(locale, config.locales, config.defaultLocale);
71
+ return (lang: string | undefined) => {
72
+ lang ??= config.defaultLocale?.lang || BuiltInDefaultLocale.lang;
73
73
 
74
74
  const t = i18n.getFixedT(lang, I18nextNamespace) as I18nT;
75
75
  t.all = () => i18n.getResourceBundle(lang, I18nextNamespace);
@@ -163,7 +163,7 @@ function linkFromInternalSidebarLinkItem(
163
163
  throw new AstroError(
164
164
  `The slug \`"${item.slug}"\` specified in the Starlight sidebar config does not exist.`,
165
165
  'Update the Starlight config to reference a valid entry slug in the docs content collection.\n' +
166
- 'Learn more about Astro content collection slugs at https://docs.astro.build/en/reference/api-reference/#getentry'
166
+ 'Learn more about Astro content collection slugs at https://docs.astro.build/en/reference/modules/astro-content/#getentry'
167
167
  );
168
168
  }
169
169
  }
@@ -62,7 +62,7 @@ export function generateRouteData({
62
62
  };
63
63
  }
64
64
 
65
- export function getToC({ entry, locale, headings }: PageProps) {
65
+ export function getToC({ entry, lang, headings }: PageProps) {
66
66
  const tocConfig =
67
67
  entry.data.template === 'splash'
68
68
  ? false
@@ -70,7 +70,7 @@ export function getToC({ entry, locale, headings }: PageProps) {
70
70
  ? entry.data.tableOfContents
71
71
  : config.tableOfContents;
72
72
  if (!tocConfig) return;
73
- const t = useTranslations(locale);
73
+ const t = useTranslations(lang);
74
74
  return {
75
75
  ...tocConfig,
76
76
  items: generateToC(headings, { ...tocConfig, title: t('tableOfContents.overview') }),
package/utils/slugs.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import config from 'virtual:starlight/user-config';
2
2
  import { BuiltInDefaultLocale } from './i18n';
3
3
  import { stripTrailingSlash } from './path';
4
+ import { slugToLocale as getLocaleFromSlug } from '../integrations/shared/slugToLocale';
4
5
 
5
6
  export interface LocaleData {
6
7
  /** Writing direction. */
@@ -18,10 +19,7 @@ export interface LocaleData {
18
19
  * @param slug A collection entry slug
19
20
  */
20
21
  function slugToLocale(slug: string): string | undefined {
21
- const locales = Object.keys(config.locales || {});
22
- const baseSegment = slug.split('/')[0];
23
- if (baseSegment && locales.includes(baseSegment)) return baseSegment;
24
- return undefined;
22
+ return getLocaleFromSlug(slug, config);
25
23
  }
26
24
 
27
25
  /** Get locale information for a given slug. */
@@ -14,25 +14,21 @@ export type UserI18nKeys = keyof RemoveIndexSignature<UserI18nSchema>;
14
14
 
15
15
  /** Get all translation data from the i18n collection, keyed by `id`, which matches locale. */
16
16
  async function loadTranslations() {
17
- let userTranslations: Record<string, UserI18nSchema> = {};
18
17
  // Briefly override `console.warn()` to silence logging when a project has no i18n collection.
19
18
  const warn = console.warn;
20
19
  console.warn = () => {};
21
- try {
22
- // Load the user’s i18n collection and ignore the error if it doesn’t exist.
23
- userTranslations = Object.fromEntries(
24
- // @ts-ignore — may be an error in projects without an i18n collection
25
- (await getCollection('i18n')).map(({ id, data }) => [id, data] as const)
26
- );
27
- } catch {}
20
+ const userTranslations: Record<string, UserI18nSchema> = Object.fromEntries(
21
+ // @ts-ignore may be a type error in projects without an i18n collection
22
+ (await getCollection('i18n')).map(({ id, data }) => [id, data] as const)
23
+ );
28
24
  // Restore the original warn implementation.
29
25
  console.warn = warn;
30
26
  return userTranslations;
31
27
  }
32
28
 
33
29
  /**
34
- * Generate a utility function that returns UI strings for the given `locale`.
35
- * @param {string | undefined} [locale]
30
+ * Generate a utility function that returns UI strings for the given language.
31
+ * @param {string | undefined} [lang]
36
32
  * @example
37
33
  * const t = useTranslations('en');
38
34
  * const label = t('search.label'); // => 'Search'