@i18n-micro/vitepress 1.0.1

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,74 @@
1
+ # `@i18n-micro/vitepress`
2
+
3
+ VitePress bindings for [i18n-micro](https://github.com/s00d/nuxt-i18n-micro): runtime JSON dictionaries, `$t` / `<I18nT>` inside markdown, and an optional `<I18nSwitcher>` — on top of VitePress built-in `locales`.
4
+
5
+ ## What this is / is not
6
+
7
+ | Need | Use |
8
+ |------|-----|
9
+ | Duplicate markdown per language, URL prefixes | **VitePress `locales`** |
10
+ | Default theme chrome labels (docFooter, search UI) | **`vitepress-i18n`** or hand-written `themeConfig` |
11
+ | Runtime `t` / plural / components in MD & custom theme | **`@i18n-micro/vitepress`** (this package) |
12
+
13
+ ## Quick start
14
+
15
+ ```bash
16
+ pnpm add @i18n-micro/vitepress
17
+ ```
18
+
19
+ ### Config (`/config` — Node)
20
+
21
+ ```ts
22
+ import { defineConfig } from 'vitepress'
23
+ import { withI18nMicro, createI18nRoutingFromAdapter } from '@i18n-micro/vitepress/config'
24
+
25
+ const locales = [
26
+ { code: 'en', iso: 'en-US', displayName: 'English' },
27
+ { code: 'fr', iso: 'fr-FR', displayName: 'Français' },
28
+ ]
29
+
30
+ export default defineConfig(
31
+ withI18nMicro(
32
+ {
33
+ locales: {
34
+ root: { label: 'English', lang: 'en' },
35
+ fr: { label: 'Français', lang: 'fr', link: '/fr/' },
36
+ },
37
+ themeConfig: {
38
+ i18nRouting: createI18nRoutingFromAdapter({
39
+ defaultLocale: 'en',
40
+ localeCodes: locales.map((l) => l.code),
41
+ }),
42
+ },
43
+ },
44
+ {
45
+ locale: 'en',
46
+ defaultLocale: 'en',
47
+ locales,
48
+ translationDir: 'locales', // root + optional pages/**
49
+ },
50
+ ),
51
+ )
52
+ ```
53
+
54
+ ### Theme
55
+
56
+ ```ts
57
+ import DefaultTheme from 'vitepress/theme'
58
+ import { defineI18nTheme } from '@i18n-micro/vitepress'
59
+
60
+ export default defineI18nTheme(DefaultTheme)
61
+ ```
62
+
63
+ ### In markdown
64
+
65
+ ```md
66
+ {{ $t('cta.readMore') }}
67
+
68
+ <I18nT keypath="greeting" :params="{ name: 'VitePress' }" />
69
+ <I18nLink to="/guide/demo">Demo</I18nLink>
70
+ ```
71
+
72
+ ## License
73
+
74
+ MIT
package/client.d.ts ADDED
@@ -0,0 +1,18 @@
1
+ declare module 'virtual:i18n-micro/config' {
2
+ export const config: {
3
+ defaultLocale: string
4
+ fallbackLocale: string
5
+ locales: import('@i18n-micro/types').Locale[]
6
+ localeCodes: string[]
7
+ missingWarn: boolean
8
+ syncWithVitePress: boolean
9
+ translationDir: string
10
+ disablePageLocales: boolean
11
+ localeKeyToCode: Record<string, string>
12
+ }
13
+ }
14
+
15
+ declare module 'virtual:i18n-micro/messages' {
16
+ export const messages: Record<string, import('@i18n-micro/types').Translations>
17
+ export const routeMessages: Record<string, Record<string, import('@i18n-micro/types').Translations>>
18
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const a=require("./with-i18n-micro-01q-vPO9.cjs"),t=require("./i18n-routing-D9iYAKDq.cjs");exports.applyLoadedTranslations=a.applyLoadedTranslations;exports.listTranslationFiles=a.listTranslationFiles;exports.loadMessages=a.loadMessages;exports.loadTranslationBuckets=a.loadTranslationBuckets;exports.warnLocaleMismatch=a.warnLocaleMismatch;exports.withI18nMicro=a.withI18nMicro;exports.createI18nRoutingFromAdapter=t.createI18nRoutingFromAdapter;
2
+ //# sourceMappingURL=config.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":""}
@@ -0,0 +1,205 @@
1
+ import { I18nRoutingStrategy } from '@i18n-micro/vue';
2
+ import { Locale } from '@i18n-micro/types';
3
+ import { Plugin as Plugin_2 } from 'vite';
4
+ import { PluralFunc } from '@i18n-micro/types';
5
+ import { TranslationFileBuckets } from '@i18n-micro/utils/parse-path';
6
+ import { Translations } from '@i18n-micro/types';
7
+
8
+ /**
9
+ * Apply page dictionaries onto an i18n instance (merged with root).
10
+ */
11
+ export declare function applyLoadedTranslations(i18n: {
12
+ addTranslations: (locale: string, translations: Translations, merge?: boolean) => void;
13
+ addRouteTranslations: (locale: string, routeName: string, translations: Translations, merge?: boolean) => void;
14
+ }, loaded: LoadedTranslations): void;
15
+
16
+ /**
17
+ * Bridge so VitePress navbar language menu and `<I18nSwitcher>` share path logic.
18
+ * Pass the result to `themeConfig.i18nRouting`.
19
+ *
20
+ * `targetLocale` is a **VitePress locale key** (`root` / `fr`). URL prefixes use that key;
21
+ * `localeKeyToCode` is only for resolving the i18n code when callers need it elsewhere.
22
+ *
23
+ * Returns a **self-contained** function (no closures) so VitePress can serialize it
24
+ * into site data via `Function#toString()` + `new Function`.
25
+ */
26
+ export declare function createI18nRoutingFromAdapter(adapterOrOptions: VitePressRouterAdapter | I18nRoutingFromAdapterOptions): VitePressI18nRoutingFn;
27
+
28
+ export declare interface I18nRoutingFromAdapterOptions {
29
+ defaultLocale: string;
30
+ localeCodes: string[];
31
+ localeKeyToCode?: Record<string, string>;
32
+ }
33
+
34
+ /**
35
+ * List JSON translation files under `translationDir` (absolute + relative paths).
36
+ */
37
+ export declare function listTranslationFiles(options: LoadMessagesOptions): TranslationFileRef[];
38
+
39
+ export declare type LoadedTranslations = TranslationFileBuckets<Translations>;
40
+
41
+ /**
42
+ * Load root-level locale JSON only (`en.json`, `fr.json`, …).
43
+ * Prefer `loadTranslationBuckets` when page locales are needed.
44
+ *
45
+ * Node.js-only (`node:fs`). For bundler-friendly loading prefer
46
+ * `withI18nMicro` virtual modules or `messagesFromGlob` in the theme.
47
+ */
48
+ export declare function loadMessages(options: LoadMessagesOptions): Record<string, Translations>;
49
+
50
+ export declare interface LoadMessagesOptions {
51
+ /** Directory with locale JSON (`en.json`, `pages/guide/demo/en.json`, …). */
52
+ translationDir: string;
53
+ rootDir?: string;
54
+ /**
55
+ * When true, treat `pages/**` files as root-level dictionaries.
56
+ * @default false
57
+ */
58
+ disablePageLocales?: boolean;
59
+ }
60
+
61
+ /**
62
+ * Load root + page-scoped dictionaries (`pages/** /xx.json`).
63
+ * Node.js-only (`node:fs`).
64
+ */
65
+ export declare function loadTranslationBuckets(options: LoadMessagesOptions): LoadedTranslations;
66
+
67
+ export declare interface TranslationFileRef {
68
+ /** Path relative to `translationDir` using `/` separators. */
69
+ relativePath: string;
70
+ absolutePath: string;
71
+ }
72
+
73
+ export declare interface VirtualI18nConfig {
74
+ defaultLocale: string;
75
+ fallbackLocale: string;
76
+ locales: Locale[];
77
+ localeCodes: string[];
78
+ missingWarn: boolean;
79
+ syncWithVitePress: boolean;
80
+ translationDir: string;
81
+ disablePageLocales: boolean;
82
+ localeKeyToCode: Record<string, string>;
83
+ }
84
+
85
+ declare interface VitePressI18nOptions {
86
+ locale: string;
87
+ fallbackLocale?: string;
88
+ locales?: Locale[];
89
+ defaultLocale?: string;
90
+ messages?: Record<string, Translations>;
91
+ /**
92
+ * Page-scoped dictionaries keyed by route name (`guide-demo`), then locale.
93
+ * Loaded from `locales/pages/**` when using `withI18nMicro`.
94
+ */
95
+ routeMessages?: Record<string, Record<string, Translations>>;
96
+ plural?: PluralFunc;
97
+ missingWarn?: boolean;
98
+ missingHandler?: (locale: string, key: string, routeName: string) => void;
99
+ /**
100
+ * Sync i18n locale from the VitePress URL path on every navigation.
101
+ * @default true
102
+ */
103
+ syncWithVitePress?: boolean;
104
+ /**
105
+ * Map VitePress locale keys to i18n codes (`root` → default locale code).
106
+ */
107
+ localeKeyToCode?: Record<string, string>;
108
+ }
109
+
110
+ /**
111
+ * Minimal shapes used by VitePress `themeConfig.i18nRouting`.
112
+ * Kept loose so we do not hard-depend on VitePress internal DefaultTheme types at compile time.
113
+ */
114
+ export declare interface VitePressI18nRoutingData {
115
+ site?: {
116
+ value?: {
117
+ locales?: Record<string, {
118
+ link?: string;
119
+ lang?: string;
120
+ }>;
121
+ };
122
+ };
123
+ localeIndex?: {
124
+ value?: string;
125
+ };
126
+ }
127
+
128
+ export declare type VitePressI18nRoutingFn = (data: VitePressI18nRoutingData, route: VitePressI18nRoutingRoute, targetLocale: string) => string;
129
+
130
+ export declare interface VitePressI18nRoutingRoute {
131
+ path: string;
132
+ hash?: string;
133
+ query?: string;
134
+ data?: {
135
+ relativePath?: string;
136
+ };
137
+ }
138
+
139
+ declare interface VitePressRouterAdapter extends I18nRoutingStrategy {
140
+ getLocaleFromPath: (path: string) => string;
141
+ switchLocalePath: (path: string, newLocale: string) => string;
142
+ localizePath: (path: string, locale: string) => string;
143
+ removeLocaleFromPath: (path: string) => string;
144
+ /** Resolve VitePress locale key (`root` / `fr`) to i18n code. */
145
+ codeFromLocaleKey: (localeKey: string) => string;
146
+ /** Resolve i18n code to VitePress locale key. */
147
+ localeKeyFromCode: (code: string) => string;
148
+ localeCodes: string[];
149
+ defaultLocale: string;
150
+ /** Snapshot of mapping used when building the adapter (for `i18nRouting` serialization). */
151
+ localeKeyToCode: Record<string, string>;
152
+ /** URL path prefixes (VitePress locale keys except `root`). */
153
+ urlPrefixes: string[];
154
+ }
155
+
156
+ /**
157
+ * Minimal VitePress / Vite user config shape we merge into.
158
+ * Avoid importing `vitepress` types so the package stays usable as a pure library dep.
159
+ */
160
+ export declare interface VitePressUserConfigLike {
161
+ locales?: Record<string, unknown>;
162
+ vite?: {
163
+ plugins?: Plugin_2[] | Plugin_2[][];
164
+ [key: string]: unknown;
165
+ };
166
+ [key: string]: unknown;
167
+ }
168
+
169
+ export declare function warnLocaleMismatch(config: VitePressUserConfigLike, options: WithI18nMicroOptions): void;
170
+
171
+ /**
172
+ * Config helper (like `withMermaid`). Name is `withI18nMicro` on purpose —
173
+ * `withI18n` is already used by the unrelated `vitepress-i18n` package.
174
+ *
175
+ * Registers virtual modules:
176
+ * - `virtual:i18n-micro/config`
177
+ * - `virtual:i18n-micro/messages` (from `translationDir`, default `locales/`)
178
+ *
179
+ * Pair with `defineI18nTheme(DefaultTheme)` — no manual `import.meta.glob` in the theme.
180
+ *
181
+ * Import from `@i18n-micro/vitepress/config` (Node / config files only).
182
+ */
183
+ export declare function withI18nMicro<T extends VitePressUserConfigLike>(config: T, options: WithI18nMicroOptions): T;
184
+
185
+ export declare interface WithI18nMicroOptions extends VitePressI18nOptions {
186
+ /**
187
+ * Directory with locale JSON (`en.json`, `pages/guide/demo/en.json`, …), relative to Vite root
188
+ * (VitePress content / docs root). Used by `virtual:i18n-micro/messages`.
189
+ * @default 'locales'
190
+ */
191
+ translationDir?: string;
192
+ /**
193
+ * When true, treat `pages/**` as root-level dictionaries.
194
+ * @default false
195
+ */
196
+ disablePageLocales?: boolean;
197
+ /**
198
+ * When true, logs a warning if VitePress `locales` keys do not align with
199
+ * configured i18n locale codes.
200
+ * @default true
201
+ */
202
+ warnOnLocaleMismatch?: boolean;
203
+ }
204
+
205
+ export { }
@@ -0,0 +1,205 @@
1
+ import { I18nRoutingStrategy } from '@i18n-micro/vue';
2
+ import { Locale } from '@i18n-micro/types';
3
+ import { Plugin as Plugin_2 } from 'vite';
4
+ import { PluralFunc } from '@i18n-micro/types';
5
+ import { TranslationFileBuckets } from '@i18n-micro/utils/parse-path';
6
+ import { Translations } from '@i18n-micro/types';
7
+
8
+ /**
9
+ * Apply page dictionaries onto an i18n instance (merged with root).
10
+ */
11
+ export declare function applyLoadedTranslations(i18n: {
12
+ addTranslations: (locale: string, translations: Translations, merge?: boolean) => void;
13
+ addRouteTranslations: (locale: string, routeName: string, translations: Translations, merge?: boolean) => void;
14
+ }, loaded: LoadedTranslations): void;
15
+
16
+ /**
17
+ * Bridge so VitePress navbar language menu and `<I18nSwitcher>` share path logic.
18
+ * Pass the result to `themeConfig.i18nRouting`.
19
+ *
20
+ * `targetLocale` is a **VitePress locale key** (`root` / `fr`). URL prefixes use that key;
21
+ * `localeKeyToCode` is only for resolving the i18n code when callers need it elsewhere.
22
+ *
23
+ * Returns a **self-contained** function (no closures) so VitePress can serialize it
24
+ * into site data via `Function#toString()` + `new Function`.
25
+ */
26
+ export declare function createI18nRoutingFromAdapter(adapterOrOptions: VitePressRouterAdapter | I18nRoutingFromAdapterOptions): VitePressI18nRoutingFn;
27
+
28
+ export declare interface I18nRoutingFromAdapterOptions {
29
+ defaultLocale: string;
30
+ localeCodes: string[];
31
+ localeKeyToCode?: Record<string, string>;
32
+ }
33
+
34
+ /**
35
+ * List JSON translation files under `translationDir` (absolute + relative paths).
36
+ */
37
+ export declare function listTranslationFiles(options: LoadMessagesOptions): TranslationFileRef[];
38
+
39
+ export declare type LoadedTranslations = TranslationFileBuckets<Translations>;
40
+
41
+ /**
42
+ * Load root-level locale JSON only (`en.json`, `fr.json`, …).
43
+ * Prefer `loadTranslationBuckets` when page locales are needed.
44
+ *
45
+ * Node.js-only (`node:fs`). For bundler-friendly loading prefer
46
+ * `withI18nMicro` virtual modules or `messagesFromGlob` in the theme.
47
+ */
48
+ export declare function loadMessages(options: LoadMessagesOptions): Record<string, Translations>;
49
+
50
+ export declare interface LoadMessagesOptions {
51
+ /** Directory with locale JSON (`en.json`, `pages/guide/demo/en.json`, …). */
52
+ translationDir: string;
53
+ rootDir?: string;
54
+ /**
55
+ * When true, treat `pages/**` files as root-level dictionaries.
56
+ * @default false
57
+ */
58
+ disablePageLocales?: boolean;
59
+ }
60
+
61
+ /**
62
+ * Load root + page-scoped dictionaries (`pages/** /xx.json`).
63
+ * Node.js-only (`node:fs`).
64
+ */
65
+ export declare function loadTranslationBuckets(options: LoadMessagesOptions): LoadedTranslations;
66
+
67
+ export declare interface TranslationFileRef {
68
+ /** Path relative to `translationDir` using `/` separators. */
69
+ relativePath: string;
70
+ absolutePath: string;
71
+ }
72
+
73
+ export declare interface VirtualI18nConfig {
74
+ defaultLocale: string;
75
+ fallbackLocale: string;
76
+ locales: Locale[];
77
+ localeCodes: string[];
78
+ missingWarn: boolean;
79
+ syncWithVitePress: boolean;
80
+ translationDir: string;
81
+ disablePageLocales: boolean;
82
+ localeKeyToCode: Record<string, string>;
83
+ }
84
+
85
+ declare interface VitePressI18nOptions {
86
+ locale: string;
87
+ fallbackLocale?: string;
88
+ locales?: Locale[];
89
+ defaultLocale?: string;
90
+ messages?: Record<string, Translations>;
91
+ /**
92
+ * Page-scoped dictionaries keyed by route name (`guide-demo`), then locale.
93
+ * Loaded from `locales/pages/**` when using `withI18nMicro`.
94
+ */
95
+ routeMessages?: Record<string, Record<string, Translations>>;
96
+ plural?: PluralFunc;
97
+ missingWarn?: boolean;
98
+ missingHandler?: (locale: string, key: string, routeName: string) => void;
99
+ /**
100
+ * Sync i18n locale from the VitePress URL path on every navigation.
101
+ * @default true
102
+ */
103
+ syncWithVitePress?: boolean;
104
+ /**
105
+ * Map VitePress locale keys to i18n codes (`root` → default locale code).
106
+ */
107
+ localeKeyToCode?: Record<string, string>;
108
+ }
109
+
110
+ /**
111
+ * Minimal shapes used by VitePress `themeConfig.i18nRouting`.
112
+ * Kept loose so we do not hard-depend on VitePress internal DefaultTheme types at compile time.
113
+ */
114
+ export declare interface VitePressI18nRoutingData {
115
+ site?: {
116
+ value?: {
117
+ locales?: Record<string, {
118
+ link?: string;
119
+ lang?: string;
120
+ }>;
121
+ };
122
+ };
123
+ localeIndex?: {
124
+ value?: string;
125
+ };
126
+ }
127
+
128
+ export declare type VitePressI18nRoutingFn = (data: VitePressI18nRoutingData, route: VitePressI18nRoutingRoute, targetLocale: string) => string;
129
+
130
+ export declare interface VitePressI18nRoutingRoute {
131
+ path: string;
132
+ hash?: string;
133
+ query?: string;
134
+ data?: {
135
+ relativePath?: string;
136
+ };
137
+ }
138
+
139
+ declare interface VitePressRouterAdapter extends I18nRoutingStrategy {
140
+ getLocaleFromPath: (path: string) => string;
141
+ switchLocalePath: (path: string, newLocale: string) => string;
142
+ localizePath: (path: string, locale: string) => string;
143
+ removeLocaleFromPath: (path: string) => string;
144
+ /** Resolve VitePress locale key (`root` / `fr`) to i18n code. */
145
+ codeFromLocaleKey: (localeKey: string) => string;
146
+ /** Resolve i18n code to VitePress locale key. */
147
+ localeKeyFromCode: (code: string) => string;
148
+ localeCodes: string[];
149
+ defaultLocale: string;
150
+ /** Snapshot of mapping used when building the adapter (for `i18nRouting` serialization). */
151
+ localeKeyToCode: Record<string, string>;
152
+ /** URL path prefixes (VitePress locale keys except `root`). */
153
+ urlPrefixes: string[];
154
+ }
155
+
156
+ /**
157
+ * Minimal VitePress / Vite user config shape we merge into.
158
+ * Avoid importing `vitepress` types so the package stays usable as a pure library dep.
159
+ */
160
+ export declare interface VitePressUserConfigLike {
161
+ locales?: Record<string, unknown>;
162
+ vite?: {
163
+ plugins?: Plugin_2[] | Plugin_2[][];
164
+ [key: string]: unknown;
165
+ };
166
+ [key: string]: unknown;
167
+ }
168
+
169
+ export declare function warnLocaleMismatch(config: VitePressUserConfigLike, options: WithI18nMicroOptions): void;
170
+
171
+ /**
172
+ * Config helper (like `withMermaid`). Name is `withI18nMicro` on purpose —
173
+ * `withI18n` is already used by the unrelated `vitepress-i18n` package.
174
+ *
175
+ * Registers virtual modules:
176
+ * - `virtual:i18n-micro/config`
177
+ * - `virtual:i18n-micro/messages` (from `translationDir`, default `locales/`)
178
+ *
179
+ * Pair with `defineI18nTheme(DefaultTheme)` — no manual `import.meta.glob` in the theme.
180
+ *
181
+ * Import from `@i18n-micro/vitepress/config` (Node / config files only).
182
+ */
183
+ export declare function withI18nMicro<T extends VitePressUserConfigLike>(config: T, options: WithI18nMicroOptions): T;
184
+
185
+ export declare interface WithI18nMicroOptions extends VitePressI18nOptions {
186
+ /**
187
+ * Directory with locale JSON (`en.json`, `pages/guide/demo/en.json`, …), relative to Vite root
188
+ * (VitePress content / docs root). Used by `virtual:i18n-micro/messages`.
189
+ * @default 'locales'
190
+ */
191
+ translationDir?: string;
192
+ /**
193
+ * When true, treat `pages/**` as root-level dictionaries.
194
+ * @default false
195
+ */
196
+ disablePageLocales?: boolean;
197
+ /**
198
+ * When true, logs a warning if VitePress `locales` keys do not align with
199
+ * configured i18n locale codes.
200
+ * @default true
201
+ */
202
+ warnOnLocaleMismatch?: boolean;
203
+ }
204
+
205
+ export { }
@@ -0,0 +1,12 @@
1
+ import { a as o, l as r, b as t, c as e, w as l, d as n } from "./with-i18n-micro-DIAoqY71.js";
2
+ import { c } from "./i18n-routing-CcppCuuS.js";
3
+ export {
4
+ o as applyLoadedTranslations,
5
+ c as createI18nRoutingFromAdapter,
6
+ r as listTranslationFiles,
7
+ t as loadMessages,
8
+ e as loadTranslationBuckets,
9
+ l as warnLocaleMismatch,
10
+ n as withI18nMicro
11
+ };
12
+ //# sourceMappingURL=config.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;"}
@@ -0,0 +1,64 @@
1
+ function l(e) {
2
+ return "switchLocalePath" in e && typeof e.switchLocalePath == "function";
3
+ }
4
+ function a(e) {
5
+ const o = l(e) ? {
6
+ defaultLocale: e.defaultLocale,
7
+ localeCodes: e.localeCodes,
8
+ localeKeyToCode: e.localeKeyToCode ?? {}
9
+ } : e, t = o.defaultLocale, c = o.localeCodes, s = o.localeKeyToCode ?? {};
10
+ return new Function(
11
+ "data",
12
+ "route",
13
+ "targetLocale",
14
+ `
15
+ const defaultLocale = ${JSON.stringify(t)};
16
+ const localeCodes = ${JSON.stringify(c)};
17
+ const localeKeyToCode = ${JSON.stringify(s)};
18
+ const keyFromCode = (code) => {
19
+ for (const key of Object.keys(localeKeyToCode)) {
20
+ if (localeKeyToCode[key] === code) return key;
21
+ }
22
+ return code === defaultLocale ? 'root' : code;
23
+ };
24
+ const prefixes = [];
25
+ for (let i = 0; i < localeCodes.length; i++) {
26
+ const code = localeCodes[i];
27
+ if (code === defaultLocale) continue;
28
+ const key = keyFromCode(code);
29
+ const urlKey = key === 'root' ? code : key;
30
+ prefixes.push(urlKey);
31
+ if (urlKey !== code) prefixes.push(code);
32
+ }
33
+ // VitePress passes locale *keys* (root / fr). URL prefix is the key, not the i18n code.
34
+ const urlPrefix = targetLocale === 'root' ? null : targetLocale;
35
+ let path = (route && route.path) || '/';
36
+ const hash = (route && route.hash) || '';
37
+ const query = (route && route.query) || '';
38
+ const hashIndex = path.indexOf('#');
39
+ const queryIndex = path.indexOf('?');
40
+ let cut = path.length;
41
+ if (hashIndex >= 0) cut = Math.min(cut, hashIndex);
42
+ if (queryIndex >= 0) cut = Math.min(cut, queryIndex);
43
+ const pathname = path.slice(0, cut) || '/';
44
+ const extras = path.slice(cut);
45
+ const hadTrailingSlash = pathname === '/' || pathname.endsWith('/');
46
+ const segments = pathname.split('/').filter(Boolean);
47
+ if (segments[0] && prefixes.indexOf(segments[0]) >= 0) segments.shift();
48
+ if (urlPrefix) segments.unshift(urlPrefix);
49
+ let localized = segments.length === 0 ? '/' : '/' + segments.join('/');
50
+ if (localized !== '/' && hadTrailingSlash) localized += '/';
51
+ const q = extras.indexOf('?') >= 0
52
+ ? ''
53
+ : (query ? (query.charAt(0) === '?' ? query : '?' + query) : '');
54
+ const h = hash
55
+ ? (hash.charAt(0) === '#' ? hash : '#' + hash)
56
+ : '';
57
+ return localized + extras + q + h;
58
+ `
59
+ );
60
+ }
61
+ export {
62
+ a as c
63
+ };
64
+ //# sourceMappingURL=i18n-routing-CcppCuuS.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"i18n-routing-CcppCuuS.js","sources":["../src/router/i18n-routing.ts"],"sourcesContent":["import type { VitePressRouterAdapter } from './adapter'\n\n/**\n * Minimal shapes used by VitePress `themeConfig.i18nRouting`.\n * Kept loose so we do not hard-depend on VitePress internal DefaultTheme types at compile time.\n */\nexport interface VitePressI18nRoutingData {\n site?: {\n value?: {\n locales?: Record<string, { link?: string, lang?: string }>\n }\n }\n localeIndex?: { value?: string }\n}\n\nexport interface VitePressI18nRoutingRoute {\n path: string\n hash?: string\n query?: string\n data?: {\n relativePath?: string\n }\n}\n\nexport type VitePressI18nRoutingFn = (\n data: VitePressI18nRoutingData,\n route: VitePressI18nRoutingRoute,\n targetLocale: string,\n) => string\n\nexport interface I18nRoutingFromAdapterOptions {\n defaultLocale: string\n localeCodes: string[]\n localeKeyToCode?: Record<string, string>\n}\n\nfunction isAdapter(value: VitePressRouterAdapter | I18nRoutingFromAdapterOptions): value is VitePressRouterAdapter {\n return 'switchLocalePath' in value && typeof (value as VitePressRouterAdapter).switchLocalePath === 'function'\n}\n\n/**\n * Bridge so VitePress navbar language menu and `<I18nSwitcher>` share path logic.\n * Pass the result to `themeConfig.i18nRouting`.\n *\n * `targetLocale` is a **VitePress locale key** (`root` / `fr`). URL prefixes use that key;\n * `localeKeyToCode` is only for resolving the i18n code when callers need it elsewhere.\n *\n * Returns a **self-contained** function (no closures) so VitePress can serialize it\n * into site data via `Function#toString()` + `new Function`.\n */\nexport function createI18nRoutingFromAdapter(\n adapterOrOptions: VitePressRouterAdapter | I18nRoutingFromAdapterOptions,\n): VitePressI18nRoutingFn {\n const options: I18nRoutingFromAdapterOptions = isAdapter(adapterOrOptions)\n ? {\n defaultLocale: adapterOrOptions.defaultLocale,\n localeCodes: adapterOrOptions.localeCodes,\n localeKeyToCode: adapterOrOptions.localeKeyToCode ?? {},\n }\n : adapterOrOptions\n\n const defaultLocale = options.defaultLocale\n const localeCodes = options.localeCodes\n const localeKeyToCode = options.localeKeyToCode ?? {}\n\n // Inlined constants — required for VitePress themeConfig function serialization.\n // oxlint-disable-next-line typescript/no-implied-eval -- intentional for VP serializeFunctions\n return new Function(\n 'data',\n 'route',\n 'targetLocale',\n `\n const defaultLocale = ${JSON.stringify(defaultLocale)};\n const localeCodes = ${JSON.stringify(localeCodes)};\n const localeKeyToCode = ${JSON.stringify(localeKeyToCode)};\n const keyFromCode = (code) => {\n for (const key of Object.keys(localeKeyToCode)) {\n if (localeKeyToCode[key] === code) return key;\n }\n return code === defaultLocale ? 'root' : code;\n };\n const prefixes = [];\n for (let i = 0; i < localeCodes.length; i++) {\n const code = localeCodes[i];\n if (code === defaultLocale) continue;\n const key = keyFromCode(code);\n const urlKey = key === 'root' ? code : key;\n prefixes.push(urlKey);\n if (urlKey !== code) prefixes.push(code);\n }\n // VitePress passes locale *keys* (root / fr). URL prefix is the key, not the i18n code.\n const urlPrefix = targetLocale === 'root' ? null : targetLocale;\n let path = (route && route.path) || '/';\n const hash = (route && route.hash) || '';\n const query = (route && route.query) || '';\n const hashIndex = path.indexOf('#');\n const queryIndex = path.indexOf('?');\n let cut = path.length;\n if (hashIndex >= 0) cut = Math.min(cut, hashIndex);\n if (queryIndex >= 0) cut = Math.min(cut, queryIndex);\n const pathname = path.slice(0, cut) || '/';\n const extras = path.slice(cut);\n const hadTrailingSlash = pathname === '/' || pathname.endsWith('/');\n const segments = pathname.split('/').filter(Boolean);\n if (segments[0] && prefixes.indexOf(segments[0]) >= 0) segments.shift();\n if (urlPrefix) segments.unshift(urlPrefix);\n let localized = segments.length === 0 ? '/' : '/' + segments.join('/');\n if (localized !== '/' && hadTrailingSlash) localized += '/';\n const q = extras.indexOf('?') >= 0\n ? ''\n : (query ? (query.charAt(0) === '?' ? query : '?' + query) : '');\n const h = hash\n ? (hash.charAt(0) === '#' ? hash : '#' + hash)\n : '';\n return localized + extras + q + h;\n `,\n ) as VitePressI18nRoutingFn\n}\n"],"names":["isAdapter","value","createI18nRoutingFromAdapter","adapterOrOptions","options","defaultLocale","localeCodes","localeKeyToCode"],"mappings":"AAoCA,SAASA,EAAUC,GAAgG;AACjH,SAAO,sBAAsBA,KAAS,OAAQA,EAAiC,oBAAqB;AACtG;AAYO,SAASC,EACdC,GACwB;AACxB,QAAMC,IAAyCJ,EAAUG,CAAgB,IACrE;AAAA,IACE,eAAeA,EAAiB;AAAA,IAChC,aAAaA,EAAiB;AAAA,IAC9B,iBAAiBA,EAAiB,mBAAmB,CAAA;AAAA,EAAC,IAExDA,GAEEE,IAAgBD,EAAQ,eACxBE,IAAcF,EAAQ,aACtBG,IAAkBH,EAAQ,mBAAmB,CAAA;AAInD,SAAO,IAAI;AAAA,IACT;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,8BAC0B,KAAK,UAAUC,CAAa,CAAC;AAAA,4BAC/B,KAAK,UAAUC,CAAW,CAAC;AAAA,gCACvB,KAAK,UAAUC,CAAe,CAAC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAAA;AA2C/D;"}
@@ -0,0 +1,46 @@
1
+ "use strict";function a(e){return"switchLocalePath"in e&&typeof e.switchLocalePath=="function"}function l(e){const o=a(e)?{defaultLocale:e.defaultLocale,localeCodes:e.localeCodes,localeKeyToCode:e.localeKeyToCode??{}}:e,t=o.defaultLocale,c=o.localeCodes,s=o.localeKeyToCode??{};return new Function("data","route","targetLocale",`
2
+ const defaultLocale = ${JSON.stringify(t)};
3
+ const localeCodes = ${JSON.stringify(c)};
4
+ const localeKeyToCode = ${JSON.stringify(s)};
5
+ const keyFromCode = (code) => {
6
+ for (const key of Object.keys(localeKeyToCode)) {
7
+ if (localeKeyToCode[key] === code) return key;
8
+ }
9
+ return code === defaultLocale ? 'root' : code;
10
+ };
11
+ const prefixes = [];
12
+ for (let i = 0; i < localeCodes.length; i++) {
13
+ const code = localeCodes[i];
14
+ if (code === defaultLocale) continue;
15
+ const key = keyFromCode(code);
16
+ const urlKey = key === 'root' ? code : key;
17
+ prefixes.push(urlKey);
18
+ if (urlKey !== code) prefixes.push(code);
19
+ }
20
+ // VitePress passes locale *keys* (root / fr). URL prefix is the key, not the i18n code.
21
+ const urlPrefix = targetLocale === 'root' ? null : targetLocale;
22
+ let path = (route && route.path) || '/';
23
+ const hash = (route && route.hash) || '';
24
+ const query = (route && route.query) || '';
25
+ const hashIndex = path.indexOf('#');
26
+ const queryIndex = path.indexOf('?');
27
+ let cut = path.length;
28
+ if (hashIndex >= 0) cut = Math.min(cut, hashIndex);
29
+ if (queryIndex >= 0) cut = Math.min(cut, queryIndex);
30
+ const pathname = path.slice(0, cut) || '/';
31
+ const extras = path.slice(cut);
32
+ const hadTrailingSlash = pathname === '/' || pathname.endsWith('/');
33
+ const segments = pathname.split('/').filter(Boolean);
34
+ if (segments[0] && prefixes.indexOf(segments[0]) >= 0) segments.shift();
35
+ if (urlPrefix) segments.unshift(urlPrefix);
36
+ let localized = segments.length === 0 ? '/' : '/' + segments.join('/');
37
+ if (localized !== '/' && hadTrailingSlash) localized += '/';
38
+ const q = extras.indexOf('?') >= 0
39
+ ? ''
40
+ : (query ? (query.charAt(0) === '?' ? query : '?' + query) : '');
41
+ const h = hash
42
+ ? (hash.charAt(0) === '#' ? hash : '#' + hash)
43
+ : '';
44
+ return localized + extras + q + h;
45
+ `)}exports.createI18nRoutingFromAdapter=l;
46
+ //# sourceMappingURL=i18n-routing-D9iYAKDq.cjs.map