From 5132831e0c8e9bb4deed376ec3395dcba844fba7 Mon Sep 17 00:00:00 2001
From: liruifengv
Date: Wed, 9 Aug 2023 15:20:26 +0800
Subject: [PATCH 01/29] Make aside label canbe translate.
---
docs/src/content/docs/zh/getting-started.mdx | 2 +-
packages/starlight/integrations/asides.ts | 73 +++++++++++++++++---
packages/starlight/schemas/i18n.ts | 4 ++
packages/starlight/translations/ar.json | 6 +-
packages/starlight/translations/cs.json | 6 +-
packages/starlight/translations/da.json | 6 +-
packages/starlight/translations/de.json | 6 +-
packages/starlight/translations/en.json | 6 +-
packages/starlight/translations/es.json | 6 +-
packages/starlight/translations/fr.json | 6 +-
packages/starlight/translations/it.json | 6 +-
packages/starlight/translations/ja.json | 6 +-
packages/starlight/translations/nb.json | 6 +-
packages/starlight/translations/nl.json | 6 +-
packages/starlight/translations/pt.json | 6 +-
packages/starlight/translations/tr.json | 6 +-
packages/starlight/translations/zh.json | 6 +-
17 files changed, 138 insertions(+), 25 deletions(-)
diff --git a/docs/src/content/docs/zh/getting-started.mdx b/docs/src/content/docs/zh/getting-started.mdx
index 486fde140e1..235066371cd 100644
--- a/docs/src/content/docs/zh/getting-started.mdx
+++ b/docs/src/content/docs/zh/getting-started.mdx
@@ -88,7 +88,7 @@ description: This is a page in my Starlight-powered site
## 更新 Starlight
-:::tip[提示]
+:::tip
由于 Starlight 是 beta 软件,所以会经常更新和改进。请务必定期更新 Starlight!
:::
diff --git a/packages/starlight/integrations/asides.ts b/packages/starlight/integrations/asides.ts
index 3af3b6715fb..d78f504abfd 100644
--- a/packages/starlight/integrations/asides.ts
+++ b/packages/starlight/integrations/asides.ts
@@ -5,6 +5,63 @@ import remarkDirective from 'remark-directive';
import type { Plugin, Transformer } from 'unified';
import { remove } from 'unist-util-remove';
import { visit } from 'unist-util-visit';
+import builtinTranslations from '../translations';
+import type { StarlightConfig } from '../types';
+
+
+/** Build a dictionary by layering preferred translation sources. */
+function buildDictionary(
+ base: (typeof builtinTranslations)[string],
+ ...dictionaries: (CollectionEntry<'i18n'>['data'] | undefined)[]
+) {
+ const dictionary = { ...base };
+ // Iterate over alternate dictionaries to avoid overwriting preceding values with `undefined`.
+ for (const dict of dictionaries) {
+ for (const key in dict) {
+ const value = dict[key as keyof typeof dict];
+ if (value) dictionary[key as keyof typeof dict] = value;
+ }
+ }
+ return dictionary;
+}
+
+// TODO get defaultLocale
+const defaultLocale = 'root';
+
+/** All translation data from the i18n collection, keyed by `id`, which matches locale. */
+let userTranslations = {};
+try {
+ // Load the user’s i18n collection and ignore the error if it doesn’t exist.
+ // TODO How to get user i18n collection
+} catch {}
+
+/** Default map of UI strings based on Starlight and user-configured defaults. */
+const defaults = buildDictionary(
+ builtinTranslations.en!,
+ userTranslations.en,
+ builtinTranslations[defaultLocale],
+ userTranslations[defaultLocale]
+);
+
+function localeToLang(locale: string | undefined): string {
+ return 'zh';
+}
+
+/**
+ * Generate a utility function that returns UI strings for the given `locale`.
+ * @param {string | undefined} [locale]
+ * @example
+ * const t = useTranslations('en');
+ * const label = t('search.label'); // => 'Search'
+ */
+export function useTranslations(locale: string | undefined) {
+ const lang = localeToLang(locale);
+ const dictionary = buildDictionary(defaults, builtinTranslations[lang], userTranslations[lang]);
+ const t = (key: K) => dictionary[key];
+ t.pick = (startOfKey: string) =>
+ Object.fromEntries(Object.entries(dictionary).filter(([k]) => k.startsWith(startOfKey)));
+ return t;
+}
/** Hacky function that generates an mdast HTML tree ready for conversion to HTML by rehype. */
function h(el: string, attrs: Properties = {}, children: any[] = []): P {
@@ -54,14 +111,9 @@ function remarkAsides(): Plugin<[], Root> {
type Variant = 'note' | 'tip' | 'caution' | 'danger';
const variants = new Set(['note', 'tip', 'caution', 'danger']);
const isAsideVariant = (s: string): s is Variant => variants.has(s);
-
- // TODO: hook these up for i18n once the design for translating strings is ready
- const defaultTitles = {
- note: 'Note',
- tip: 'Tip',
- caution: 'Caution',
- danger: 'Danger',
- };
+ // TODO how to get current locale
+ const locale = 'en';
+ const t = useTranslations(locale);
const iconPaths = {
// Information icon
@@ -96,18 +148,19 @@ function remarkAsides(): Plugin<[], Root> {
};
const transformer: Transformer = (tree) => {
+
+ // console.log('Astro.props', Astro.props);
visit(tree, (node, index, parent) => {
if (!parent || index === null || node.type !== 'containerDirective') {
return;
}
const variant = node.name;
if (!isAsideVariant(variant)) return;
-
// remark-directive converts a container’s “label” to a paragraph in
// its children, but we want to pass it as the title prop to
* ```
*/
-function remarkAsides(): Plugin<[], Root> {
+function remarkAsides(config: StarlightConfig): Plugin<[], Root> {
type Variant = 'note' | 'tip' | 'caution' | 'danger';
const variants = new Set(['note', 'tip', 'caution', 'danger']);
const isAsideVariant = (s: string): s is Variant => variants.has(s);
@@ -160,9 +159,10 @@ function remarkAsides(): Plugin<[], Root> {
};
const transformer: Transformer = (tree, file) => {
+ const lang = getLanguageFromPath(file.history[0], config);
+ console.log('lang', lang)
+ const t = useTranslations(lang, config);
- const lang = getLanguageFromPath(file.history[0]);
- const t = useTranslations(lang);
visit(tree, (node, index, parent) => {
if (!parent || index === null || node.type !== 'containerDirective') {
return;
@@ -173,7 +173,7 @@ function remarkAsides(): Plugin<[], Root> {
// its children, but we want to pass it as the title prop to
* ```
*/
-function remarkAsides(config: StarlightConfig): Plugin<[], Root> {
+function remarkAsides(config: StarlightConfig, useTranslations: any): Plugin<[], Root> {
type Variant = 'note' | 'tip' | 'caution' | 'danger';
const variants = new Set(['note', 'tip', 'caution', 'danger']);
const isAsideVariant = (s: string): s is Variant => variants.has(s);
@@ -179,8 +101,8 @@ function remarkAsides(config: StarlightConfig): Plugin<[], Root> {
};
const transformer: Transformer = (tree, file) => {
- const lang = getLanguageFromPath(file.history[0], config);
- const t = useTranslations(lang, config);
+ const locale = getLocaleFromPath(file.history[0], config);
+ const t = useTranslations(locale);
visit(tree, (node, index, parent) => {
if (!parent || index === null || node.type !== 'containerDirective') {
@@ -188,6 +110,7 @@ function remarkAsides(config: StarlightConfig): Plugin<[], Root> {
}
const variant = node.name;
if (!isAsideVariant(variant)) return;
+
// remark-directive converts a container’s “label” to a paragraph in
// its children, but we want to pass it as the title prop to
* ```
*/
-function remarkAsides(config: StarlightConfig, useTranslations: any): Plugin<[], Root> {
+function remarkAsides(
+ config: StarlightConfig,
+ useTranslations: ReturnType
+): Plugin<[], Root> {
type Variant = 'note' | 'tip' | 'caution' | 'danger';
const variants = new Set(['note', 'tip', 'caution', 'danger']);
const isAsideVariant = (s: string): s is Variant => variants.has(s);
From b47405393b3bbb2f37780d586f8fa9565145e2b1 Mon Sep 17 00:00:00 2001
From: liruifengv
Date: Fri, 3 Nov 2023 22:28:21 +0800
Subject: [PATCH 16/29] Update packages/starlight/integrations/asides.ts
Co-authored-by: Chris Swithinbank
---
packages/starlight/integrations/asides.ts | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/packages/starlight/integrations/asides.ts b/packages/starlight/integrations/asides.ts
index 40f6c83ac61..e60007f42d2 100644
--- a/packages/starlight/integrations/asides.ts
+++ b/packages/starlight/integrations/asides.ts
@@ -169,6 +169,9 @@ function remarkAsides(
type RemarkPlugins = NonNullable['remarkPlugins']>;
-export function starlightAsides(opts: StarlightConfig, useTranslations: any): RemarkPlugins {
+export function starlightAsides(
+ opts: StarlightConfig,
+ useTranslations: ReturnType
+): RemarkPlugins {
return [remarkDirective, remarkAsides(opts, useTranslations)];
}
From 83af8daecb1aaea04e036f4967d600ac65fb9dd0 Mon Sep 17 00:00:00 2001
From: liruifengv
Date: Fri, 3 Nov 2023 22:54:57 +0800
Subject: [PATCH 17/29] Fix type & getLocaleFromPath function
---
packages/starlight/index.ts | 2 +-
packages/starlight/integrations/asides.ts | 54 ++++++++++++++++-------
2 files changed, 39 insertions(+), 17 deletions(-)
diff --git a/packages/starlight/index.ts b/packages/starlight/index.ts
index 12b93a5798f..6fba9f0830c 100644
--- a/packages/starlight/index.ts
+++ b/packages/starlight/index.ts
@@ -49,7 +49,7 @@ export default function StarlightIntegration(opts: StarlightUserConfig): AstroIn
plugins: [vitePluginStarlightUserConfig(userConfig, config)],
},
markdown: {
- remarkPlugins: [...starlightAsides(userConfig, useTranslations)],
+ remarkPlugins: [...starlightAsides(userConfig, config, useTranslations)],
rehypePlugins: [rehypeRtlCodeSupport()],
shikiConfig:
// Configure Shiki theme if the user is using the default github-dark theme.
diff --git a/packages/starlight/integrations/asides.ts b/packages/starlight/integrations/asides.ts
index e60007f42d2..1dbb63d5c16 100644
--- a/packages/starlight/integrations/asides.ts
+++ b/packages/starlight/integrations/asides.ts
@@ -1,4 +1,4 @@
-import type { AstroUserConfig } from 'astro';
+import type { AstroConfig, AstroUserConfig } from 'astro';
import { h as _h, s as _s, type Properties } from 'hastscript';
import type { Paragraph as P, Root } from 'mdast';
import remarkDirective from 'remark-directive';
@@ -6,19 +6,39 @@ import type { Plugin, Transformer } from 'unified';
import { remove } from 'unist-util-remove';
import { visit } from 'unist-util-visit';
import type { StarlightConfig } from '../types';
+import type { createTranslationSystemFromFs } from '../utils/translations-fs';
+
+function pathToLocale(slug: string, config: StarlightConfig): string | undefined {
+ const locales = Object.keys(config.locales || {});
+ const baseSegment = slug.split('/')[0];
+ if (baseSegment && locales.includes(baseSegment)) return baseSegment;
+ return undefined;
+}
+
+function localeToLang(locale: string | undefined, config: StarlightConfig): string {
+ const lang = locale ? config.locales?.[locale]?.lang : config.locales?.root?.lang;
+ const defaultLang = config.defaultLocale?.lang || config.defaultLocale?.locale;
+ return lang || defaultLang || 'en';
+}
/** get current lang from file full path */
-function getLocaleFromPath(path: string, config: StarlightConfig): string {
- // format path to unix style path
- const formatPath = path.replace(/\\/g, '/');
- const parts = formatPath.split('/');
- const langIndex = parts.findIndex((part) => /^[a-z]{2}(-[a-z]{2})?$/.test(part));
- if (langIndex !== -1) {
- return parts[langIndex] || 'en';
- } else {
- const defaultLocale = config.defaultLocale?.lang || config.defaultLocale?.locale;
- return defaultLocale || 'en';
- }
+function getLocaleFromPath(
+ unformattedPath: string,
+ starlightConfig: StarlightConfig,
+ astroConfig: AstroConfig
+): string {
+ const srcDir = new URL(astroConfig.srcDir, astroConfig.root);
+ const docsDir = new URL('content/docs/', srcDir);
+ const path = unformattedPath
+ // Format path to unix style path.
+ .replace(/\\/g, '/')
+ // Strip docs path leaving only content collection file ID.
+ // Example: /Users/houston/repo/src/content/docs/en/guide.md => en/guide.md
+ .replace(docsDir.pathname, '');
+ const locale = pathToLocale(path, starlightConfig);
+ return locale || 'en';
+ // maybe don't need this
+ // return localeToLang(locale, starlightConfig);
}
/** Hacky function that generates an mdast HTML tree ready for conversion to HTML by rehype. */
@@ -66,7 +86,8 @@ function s(el: string, attrs: Properties = {}, children: any[] = []): P {
* ```
*/
function remarkAsides(
- config: StarlightConfig,
+ starlightConfig: StarlightConfig,
+ astroConfig: AstroConfig,
useTranslations: ReturnType
): Plugin<[], Root> {
type Variant = 'note' | 'tip' | 'caution' | 'danger';
@@ -106,7 +127,7 @@ function remarkAsides(
};
const transformer: Transformer = (tree, file) => {
- const locale = getLocaleFromPath(file.history[0], config);
+ const locale = getLocaleFromPath(file.history[0],starlightConfig, astroConfig);
const t = useTranslations(locale);
visit(tree, (node, index, parent) => {
if (!parent || index === null || node.type !== 'containerDirective') {
@@ -170,8 +191,9 @@ function remarkAsides(
type RemarkPlugins = NonNullable['remarkPlugins']>;
export function starlightAsides(
- opts: StarlightConfig,
+ starlightConfig: StarlightConfig,
+ astroConfig: AstroConfig,
useTranslations: ReturnType
): RemarkPlugins {
- return [remarkDirective, remarkAsides(opts, useTranslations)];
+ return [remarkDirective, remarkAsides(starlightConfig, astroConfig, useTranslations)];
}
From c408ea667a9064541812500d77b2c6269e4b5098 Mon Sep 17 00:00:00 2001
From: liruifengv
Date: Fri, 3 Nov 2023 22:56:06 +0800
Subject: [PATCH 18/29] format
---
packages/starlight/integrations/asides.ts | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/packages/starlight/integrations/asides.ts b/packages/starlight/integrations/asides.ts
index 1dbb63d5c16..3fe4614443a 100644
--- a/packages/starlight/integrations/asides.ts
+++ b/packages/starlight/integrations/asides.ts
@@ -36,8 +36,8 @@ function getLocaleFromPath(
// Example: /Users/houston/repo/src/content/docs/en/guide.md => en/guide.md
.replace(docsDir.pathname, '');
const locale = pathToLocale(path, starlightConfig);
- return locale || 'en';
- // maybe don't need this
+ return locale || 'en';
+ // maybe don't need this
// return localeToLang(locale, starlightConfig);
}
@@ -87,7 +87,7 @@ function s(el: string, attrs: Properties = {}, children: any[] = []): P {
*/
function remarkAsides(
starlightConfig: StarlightConfig,
- astroConfig: AstroConfig,
+ astroConfig: AstroConfig,
useTranslations: ReturnType
): Plugin<[], Root> {
type Variant = 'note' | 'tip' | 'caution' | 'danger';
@@ -127,7 +127,7 @@ function remarkAsides(
};
const transformer: Transformer = (tree, file) => {
- const locale = getLocaleFromPath(file.history[0],starlightConfig, astroConfig);
+ const locale = getLocaleFromPath(file.history[0], starlightConfig, astroConfig);
const t = useTranslations(locale);
visit(tree, (node, index, parent) => {
if (!parent || index === null || node.type !== 'containerDirective') {
@@ -192,7 +192,7 @@ type RemarkPlugins = NonNullable['remar
export function starlightAsides(
starlightConfig: StarlightConfig,
- astroConfig: AstroConfig,
+ astroConfig: AstroConfig,
useTranslations: ReturnType
): RemarkPlugins {
return [remarkDirective, remarkAsides(starlightConfig, astroConfig, useTranslations)];
From fc29e1037c8124480bf8082fec65c9d699904b9d Mon Sep 17 00:00:00 2001
From: liruifengv
Date: Fri, 3 Nov 2023 22:59:41 +0800
Subject: [PATCH 19/29] fix return type
---
packages/starlight/integrations/asides.ts | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/packages/starlight/integrations/asides.ts b/packages/starlight/integrations/asides.ts
index 3fe4614443a..7cb34fc095e 100644
--- a/packages/starlight/integrations/asides.ts
+++ b/packages/starlight/integrations/asides.ts
@@ -26,7 +26,7 @@ function getLocaleFromPath(
unformattedPath: string,
starlightConfig: StarlightConfig,
astroConfig: AstroConfig
-): string {
+): string | undefined {
const srcDir = new URL(astroConfig.srcDir, astroConfig.root);
const docsDir = new URL('content/docs/', srcDir);
const path = unformattedPath
@@ -36,7 +36,7 @@ function getLocaleFromPath(
// Example: /Users/houston/repo/src/content/docs/en/guide.md => en/guide.md
.replace(docsDir.pathname, '');
const locale = pathToLocale(path, starlightConfig);
- return locale || 'en';
+ return locale;
// maybe don't need this
// return localeToLang(locale, starlightConfig);
}
From 69e7925d2115f0f2da27a2acf4f6a63451d6ef11 Mon Sep 17 00:00:00 2001
From: liruifengv
Date: Sat, 4 Nov 2023 13:47:49 +0800
Subject: [PATCH 20/29] delete unuse
---
packages/starlight/integrations/asides.ts | 8 --------
1 file changed, 8 deletions(-)
diff --git a/packages/starlight/integrations/asides.ts b/packages/starlight/integrations/asides.ts
index 7cb34fc095e..f3cec1a8a95 100644
--- a/packages/starlight/integrations/asides.ts
+++ b/packages/starlight/integrations/asides.ts
@@ -15,12 +15,6 @@ function pathToLocale(slug: string, config: StarlightConfig): string | undefined
return undefined;
}
-function localeToLang(locale: string | undefined, config: StarlightConfig): string {
- const lang = locale ? config.locales?.[locale]?.lang : config.locales?.root?.lang;
- const defaultLang = config.defaultLocale?.lang || config.defaultLocale?.locale;
- return lang || defaultLang || 'en';
-}
-
/** get current lang from file full path */
function getLocaleFromPath(
unformattedPath: string,
@@ -37,8 +31,6 @@ function getLocaleFromPath(
.replace(docsDir.pathname, '');
const locale = pathToLocale(path, starlightConfig);
return locale;
- // maybe don't need this
- // return localeToLang(locale, starlightConfig);
}
/** Hacky function that generates an mdast HTML tree ready for conversion to HTML by rehype. */
From 3a56b9e16324dd71d4c71ac79b52555daec90518 Mon Sep 17 00:00:00 2001
From: Chris Swithinbank
Date: Thu, 16 Nov 2023 22:43:43 +0100
Subject: [PATCH 21/29] refactor: Use destructured objects instead of multiple
arguments in aside function signatures
---
packages/starlight/index.ts | 2 +-
packages/starlight/integrations/asides.ts | 29 ++++++++++-------------
2 files changed, 14 insertions(+), 17 deletions(-)
diff --git a/packages/starlight/index.ts b/packages/starlight/index.ts
index 6fba9f0830c..93a1e9c743f 100644
--- a/packages/starlight/index.ts
+++ b/packages/starlight/index.ts
@@ -49,7 +49,7 @@ export default function StarlightIntegration(opts: StarlightUserConfig): AstroIn
plugins: [vitePluginStarlightUserConfig(userConfig, config)],
},
markdown: {
- remarkPlugins: [...starlightAsides(userConfig, config, useTranslations)],
+ remarkPlugins: [...starlightAsides({ starlightConfig: userConfig, astroConfig: config, useTranslations })],
rehypePlugins: [rehypeRtlCodeSupport()],
shikiConfig:
// Configure Shiki theme if the user is using the default github-dark theme.
diff --git a/packages/starlight/integrations/asides.ts b/packages/starlight/integrations/asides.ts
index f3cec1a8a95..6264e98f3b3 100644
--- a/packages/starlight/integrations/asides.ts
+++ b/packages/starlight/integrations/asides.ts
@@ -8,7 +8,13 @@ import { visit } from 'unist-util-visit';
import type { StarlightConfig } from '../types';
import type { createTranslationSystemFromFs } from '../utils/translations-fs';
-function pathToLocale(slug: string, config: StarlightConfig): string | undefined {
+interface AsidesOptions {
+ starlightConfig: { locales: StarlightConfig['locales'] };
+ astroConfig: { root: AstroConfig['root']; srcDir: AstroConfig['srcDir'] };
+ useTranslations: ReturnType;
+}
+
+function pathToLocale(slug: string, config: AsidesOptions['starlightConfig']): string | undefined {
const locales = Object.keys(config.locales || {});
const baseSegment = slug.split('/')[0];
if (baseSegment && locales.includes(baseSegment)) return baseSegment;
@@ -18,8 +24,7 @@ function pathToLocale(slug: string, config: StarlightConfig): string | undefined
/** get current lang from file full path */
function getLocaleFromPath(
unformattedPath: string,
- starlightConfig: StarlightConfig,
- astroConfig: AstroConfig
+ { starlightConfig, astroConfig }: AsidesOptions
): string | undefined {
const srcDir = new URL(astroConfig.srcDir, astroConfig.root);
const docsDir = new URL('content/docs/', srcDir);
@@ -77,11 +82,7 @@ function s(el: string, attrs: Properties = {}, children: any[] = []): P {
*
* ```
*/
-function remarkAsides(
- starlightConfig: StarlightConfig,
- astroConfig: AstroConfig,
- useTranslations: ReturnType
-): Plugin<[], Root> {
+function remarkAsides(options: AsidesOptions): Plugin<[], Root> {
type Variant = 'note' | 'tip' | 'caution' | 'danger';
const variants = new Set(['note', 'tip', 'caution', 'danger']);
const isAsideVariant = (s: string): s is Variant => variants.has(s);
@@ -119,8 +120,8 @@ function remarkAsides(
};
const transformer: Transformer = (tree, file) => {
- const locale = getLocaleFromPath(file.history[0], starlightConfig, astroConfig);
- const t = useTranslations(locale);
+ const locale = getLocaleFromPath(file.history[0], options);
+ const t = options.useTranslations(locale);
visit(tree, (node, index, parent) => {
if (!parent || index === null || node.type !== 'containerDirective') {
return;
@@ -182,10 +183,6 @@ function remarkAsides(
type RemarkPlugins = NonNullable['remarkPlugins']>;
-export function starlightAsides(
- starlightConfig: StarlightConfig,
- astroConfig: AstroConfig,
- useTranslations: ReturnType
-): RemarkPlugins {
- return [remarkDirective, remarkAsides(starlightConfig, astroConfig, useTranslations)];
+export function starlightAsides(options: AsidesOptions): RemarkPlugins {
+ return [remarkDirective, remarkAsides(options)];
}
From 25686a48b297f9eaefb91838814c9ddd01ec6cc0 Mon Sep 17 00:00:00 2001
From: Chris Swithinbank
Date: Thu, 16 Nov 2023 22:44:32 +0100
Subject: [PATCH 22/29] Handle missing file path in vFile history
---
packages/starlight/integrations/asides.ts | 11 +++++++----
1 file changed, 7 insertions(+), 4 deletions(-)
diff --git a/packages/starlight/integrations/asides.ts b/packages/starlight/integrations/asides.ts
index 6264e98f3b3..a97a610ae0a 100644
--- a/packages/starlight/integrations/asides.ts
+++ b/packages/starlight/integrations/asides.ts
@@ -14,23 +14,26 @@ interface AsidesOptions {
useTranslations: ReturnType;
}
-function pathToLocale(slug: string, config: AsidesOptions['starlightConfig']): string | undefined {
+function pathToLocale(
+ slug: string | undefined,
+ config: AsidesOptions['starlightConfig']
+): string | undefined {
const locales = Object.keys(config.locales || {});
- const baseSegment = slug.split('/')[0];
+ const baseSegment = slug?.split('/')[0];
if (baseSegment && locales.includes(baseSegment)) return baseSegment;
return undefined;
}
/** get current lang from file full path */
function getLocaleFromPath(
- unformattedPath: string,
+ unformattedPath: string | undefined,
{ starlightConfig, astroConfig }: AsidesOptions
): string | undefined {
const srcDir = new URL(astroConfig.srcDir, astroConfig.root);
const docsDir = new URL('content/docs/', srcDir);
const path = unformattedPath
// Format path to unix style path.
- .replace(/\\/g, '/')
+ ?.replace(/\\/g, '/')
// Strip docs path leaving only content collection file ID.
// Example: /Users/houston/repo/src/content/docs/en/guide.md => en/guide.md
.replace(docsDir.pathname, '');
From 0d9415c78435c63b2d9b096e1a4361b963d6d9a4 Mon Sep 17 00:00:00 2001
From: Chris Swithinbank
Date: Thu, 16 Nov 2023 22:45:09 +0100
Subject: [PATCH 23/29] Fix existing tests
---
.../__tests__/remark-rehype/asides.test.ts | 18 +++++++++++++++++-
1 file changed, 17 insertions(+), 1 deletion(-)
diff --git a/packages/starlight/__tests__/remark-rehype/asides.test.ts b/packages/starlight/__tests__/remark-rehype/asides.test.ts
index 57fe4191840..586badc4b4a 100644
--- a/packages/starlight/__tests__/remark-rehype/asides.test.ts
+++ b/packages/starlight/__tests__/remark-rehype/asides.test.ts
@@ -1,9 +1,25 @@
import { createMarkdownProcessor } from '@astrojs/markdown-remark';
import { describe, expect, test } from 'vitest';
import { starlightAsides } from '../../integrations/asides';
+import { createTranslationSystemFromFs } from '../../utils/translations-fs';
+
+const useTranslations = createTranslationSystemFromFs(
+ {
+ locales: { en: { label: 'English', dir: 'ltr' } },
+ defaultLocale: { label: 'English', locale: 'en', dir: 'ltr' },
+ },
+ // Using non-existent `_src/` to ignore custom files in this test fixture.
+ { srcDir: new URL('./_src/', import.meta.url) }
+);
const processor = await createMarkdownProcessor({
- remarkPlugins: [...starlightAsides()],
+ remarkPlugins: [
+ ...starlightAsides({
+ starlightConfig: { locales: {} },
+ astroConfig: { root: new URL(import.meta.url), srcDir: new URL('./_src/', import.meta.url) },
+ useTranslations,
+ }),
+ ],
});
test('generates
`);
+ });
+});
From a73892647e855ab50b30ef1884b8432fce787ef4 Mon Sep 17 00:00:00 2001
From: Chris Swithinbank
Date: Thu, 16 Nov 2023 23:41:22 +0100
Subject: [PATCH 27/29] Add test for no locales scenario
---
.../__tests__/remark-rehype/asides.test.ts | 17 +++++++++++++++++
1 file changed, 17 insertions(+)
diff --git a/packages/starlight/__tests__/remark-rehype/asides.test.ts b/packages/starlight/__tests__/remark-rehype/asides.test.ts
index b61a72bea6d..b3f792166ba 100644
--- a/packages/starlight/__tests__/remark-rehype/asides.test.ts
+++ b/packages/starlight/__tests__/remark-rehype/asides.test.ts
@@ -125,3 +125,20 @@ Some text
expect(res.code).includes(`${label}`);
});
});
+
+test('runs without locales config', async () => {
+ const processor = await createMarkdownProcessor({
+ remarkPlugins: [
+ ...starlightAsides({
+ starlightConfig: { locales: undefined },
+ astroConfig: {
+ root: new URL(import.meta.url),
+ srcDir: new URL('./_src/', import.meta.url),
+ },
+ useTranslations,
+ }),
+ ],
+ });
+ const res = await processor.render(':::note\nTest\n::');
+ expect(res.code.includes('aria-label=Note"'));
+});
From a33185cc73e25a33695be8e3076f67090ff7a90f Mon Sep 17 00:00:00 2001
From: Chris Swithinbank
Date: Thu, 16 Nov 2023 23:52:05 +0100
Subject: [PATCH 28/29] nit: Tweak schema field descriptions
---
packages/starlight/schemas/i18n.ts | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/packages/starlight/schemas/i18n.ts b/packages/starlight/schemas/i18n.ts
index 4dde0a46c25..d1cd39750b8 100644
--- a/packages/starlight/schemas/i18n.ts
+++ b/packages/starlight/schemas/i18n.ts
@@ -89,10 +89,10 @@ function starlightI18nSchema() {
.describe('Label shown on the “next page” pagination arrow in the page footer.'),
'404.text': z.string().describe('Text shown on Starlight’s default 404 page'),
- 'aside.tip': z.string().describe('Text shown on the aside tip'),
- 'aside.note': z.string().describe('Text shown on the aside note'),
- 'aside.caution': z.string().describe('Text shown on the aside warning'),
- 'aside.danger': z.string().describe('Text shown on the aside danger'),
+ 'aside.tip': z.string().describe('Text shown on the tip aside variant'),
+ 'aside.note': z.string().describe('Text shown on the note aside variant'),
+ 'aside.caution': z.string().describe('Text shown on the warning aside variant'),
+ 'aside.danger': z.string().describe('Text shown on the danger aside variant'),
})
.partial();
}
From 27141e2257f6fca34aea6944ebf71ac37636884f Mon Sep 17 00:00:00 2001
From: liruifengv
Date: Fri, 17 Nov 2023 12:16:15 +0800
Subject: [PATCH 29/29] reset getting-started & components
---
docs/src/content/docs/zh-cn/getting-started.mdx | 2 +-
docs/src/content/docs/zh-cn/guides/components.mdx | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/docs/src/content/docs/zh-cn/getting-started.mdx b/docs/src/content/docs/zh-cn/getting-started.mdx
index 057cd78b7f7..d2d06db0cb9 100644
--- a/docs/src/content/docs/zh-cn/getting-started.mdx
+++ b/docs/src/content/docs/zh-cn/getting-started.mdx
@@ -83,7 +83,7 @@ description: This is a page in my Starlight-powered site
## 更新 Starlight
-:::tip
+:::tip[提示]
由于 Starlight 是 beta 软件,所以会经常更新和改进。请务必定期更新 Starlight!
:::
diff --git a/docs/src/content/docs/zh-cn/guides/components.mdx b/docs/src/content/docs/zh-cn/guides/components.mdx
index 2ace98d400e..66082ba6bf8 100644
--- a/docs/src/content/docs/zh-cn/guides/components.mdx
+++ b/docs/src/content/docs/zh-cn/guides/components.mdx
@@ -112,7 +112,7 @@ import { Card, CardGrid } from '@astrojs/starlight/components';
-:::tip
+:::tip[提示]
在主页上使用卡片网格来显示项目的关键特性。
添加 `stagger` 属性来垂直移动第二列的卡片,并增加视觉效果: