@nikala-ui/folio 0.13.2 → 0.15.0
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/README.md +16 -0
- package/dist/cli/commands/init.js +37 -2
- package/dist/client/app.jsx +6 -3
- package/dist/client/components/docs-layout-shell.d.ts +1 -0
- package/dist/client/components/docs-layout-shell.jsx +1 -1
- package/dist/client/components/docs-page-content.jsx +9 -5
- package/dist/client/navigation/page-navigation.js +10 -27
- package/dist/client/public/algolia-logo.svg +15 -0
- package/dist/client/public/favicon.ico +0 -0
- package/dist/client/public/logo.svg +11 -0
- package/dist/client/routing/use-docs-router.d.ts +1 -0
- package/dist/client/routing/use-docs-router.js +12 -3
- package/dist/client/style.css +139 -55
- package/dist/components/mdx-components.jsx +4 -1
- package/dist/components/ui/api-table.jsx +6 -4
- package/dist/components/ui/code-block.jsx +6 -4
- package/dist/components/ui/command.jsx +9 -5
- package/dist/components/ui/list.jsx +3 -1
- package/dist/components/ui/logo.d.ts +4 -1
- package/dist/components/ui/logo.jsx +8 -22
- package/dist/components/ui/package-manager-tabs.jsx +12 -2
- package/dist/components/ui/pager.jsx +4 -2
- package/dist/components/ui/section-heading.jsx +1 -1
- package/dist/components/ui/sidebar.jsx +6 -3
- package/dist/components/ui/spinner.jsx +3 -1
- package/dist/components/ui/theme-toggle.jsx +14 -12
- package/dist/config.d.ts +1 -0
- package/dist/config.js +84 -29
- package/dist/core/content-scanner.d.ts +2 -1
- package/dist/core/content-scanner.js +2 -9
- package/dist/core/plugin/index.d.ts +2 -0
- package/dist/core/plugin/index.js +1 -0
- package/dist/core/plugin/lifecycle-error.d.ts +19 -0
- package/dist/core/plugin/lifecycle-error.js +28 -0
- package/dist/core/plugin/lifecycle-snapshot.d.ts +1 -0
- package/dist/core/plugin/lifecycle-snapshot.js +156 -0
- package/dist/core/plugin/lifecycle.d.ts +34 -0
- package/dist/core/plugin/lifecycle.js +170 -0
- package/dist/core/route-tree.d.ts +3 -2
- package/dist/core/route-tree.js +40 -10
- package/dist/index.d.ts +7 -3
- package/dist/index.js +4 -3
- package/dist/plugin.d.ts +60 -0
- package/dist/plugin.js +51 -0
- package/dist/plugins/i18n/components/site-language-switcher.d.ts +10 -0
- package/dist/plugins/i18n/components/site-language-switcher.jsx +39 -0
- package/dist/plugins/i18n/constants.d.ts +1 -0
- package/dist/plugins/i18n/constants.js +1 -0
- package/dist/plugins/i18n/index.d.ts +11 -0
- package/dist/plugins/i18n/index.js +89 -0
- package/dist/plugins/i18n/navigation.d.ts +5 -0
- package/dist/plugins/i18n/navigation.js +33 -0
- package/dist/plugins/i18n/runtime.d.ts +19 -0
- package/dist/plugins/i18n/runtime.jsx +65 -0
- package/dist/plugins/i18n/site-locale.d.ts +12 -0
- package/dist/plugins/i18n/site-locale.js +119 -0
- package/dist/search/provider.d.ts +16 -4
- package/dist/search/provider.js +28 -3
- package/dist/server/index.d.ts +4 -1
- package/dist/server/index.js +73 -33
- package/dist/server/module-serialization.d.ts +6 -0
- package/dist/server/module-serialization.js +26 -0
- package/dist/server/plugin/components.d.ts +7 -0
- package/dist/server/plugin/components.js +82 -0
- package/dist/server/plugin/constants.d.ts +17 -0
- package/dist/server/plugin/constants.js +21 -0
- package/dist/server/{plugin.d.ts → plugin/index.d.ts} +3 -1
- package/dist/server/plugin/index.js +166 -0
- package/dist/server/plugin/lifecycle.d.ts +32 -0
- package/dist/server/plugin/lifecycle.js +110 -0
- package/dist/server/plugin/styles.d.ts +3 -0
- package/dist/server/plugin/styles.js +22 -0
- package/dist/server/plugin/virtual-modules.d.ts +11 -0
- package/dist/server/plugin/virtual-modules.js +88 -0
- package/dist/server/seo.d.ts +4 -0
- package/dist/server/seo.js +87 -0
- package/dist/themes/default/content/breadcrumbs.jsx +5 -2
- package/dist/themes/default/content/table-of-contents.jsx +3 -1
- package/dist/themes/default/layout.jsx +42 -14
- package/dist/themes/default/navbar.jsx +14 -7
- package/dist/themes/default/navigation/mobile-table-of-contents.jsx +3 -1
- package/dist/themes/default/navigation/sidebar-tree.d.ts +2 -0
- package/dist/themes/default/navigation/sidebar-tree.jsx +11 -2
- package/dist/themes/default/overlays/search-dialog.jsx +37 -11
- package/dist/themes/default/sidebar.jsx +7 -5
- package/dist/themes/types.d.ts +7 -2
- package/dist/types.d.ts +38 -1
- package/dist/utils/format-title.d.ts +1 -0
- package/dist/utils/format-title.js +11 -0
- package/dist/vendor/docs-src/components/mdx-components.tsx +4 -1
- package/dist/vendor/docs-src/components/ui/api-table.tsx +6 -4
- package/dist/vendor/docs-src/components/ui/code-block.tsx +6 -4
- package/dist/vendor/docs-src/components/ui/command.tsx +9 -5
- package/dist/vendor/docs-src/components/ui/list.tsx +4 -2
- package/dist/vendor/docs-src/components/ui/logo.tsx +19 -48
- package/dist/vendor/docs-src/components/ui/package-manager-tabs.tsx +12 -2
- package/dist/vendor/docs-src/components/ui/pager.tsx +4 -2
- package/dist/vendor/docs-src/components/ui/section-heading.tsx +5 -1
- package/dist/vendor/docs-src/components/ui/sidebar.tsx +7 -4
- package/dist/vendor/docs-src/components/ui/spinner.tsx +3 -1
- package/dist/vendor/docs-src/components/ui/theme-toggle.tsx +15 -13
- package/dist/vendor/docs-src/plugins/i18n/components/site-language-switcher.tsx +56 -0
- package/dist/vendor/docs-src/plugins/i18n/constants.ts +1 -0
- package/dist/vendor/docs-src/plugins/i18n/index.ts +110 -0
- package/dist/vendor/docs-src/plugins/i18n/navigation.ts +49 -0
- package/dist/vendor/docs-src/plugins/i18n/runtime.tsx +91 -0
- package/dist/vendor/docs-src/plugins/i18n/site-locale.ts +140 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +19 -4
- package/dist/server/plugin.js +0 -521
|
@@ -7,6 +7,7 @@ import {
|
|
|
7
7
|
CardDescription,
|
|
8
8
|
} from "@/components/ui/card";
|
|
9
9
|
import { cn } from "@/lib/cn";
|
|
10
|
+
import { useSiteLocale } from "../../plugins/i18n/runtime.jsx";
|
|
10
11
|
|
|
11
12
|
export interface PagerItem {
|
|
12
13
|
title: string;
|
|
@@ -25,6 +26,7 @@ export interface PagerLinkProps extends JSX.AnchorHTMLAttributes<HTMLAnchorEleme
|
|
|
25
26
|
*/
|
|
26
27
|
export const PagerLink: Component<PagerLinkProps> = (props) => {
|
|
27
28
|
const [local, rest] = splitProps(props, ["title", "href", "type", "class"]);
|
|
29
|
+
const siteLocale = useSiteLocale();
|
|
28
30
|
|
|
29
31
|
const isNext = () => local.type === "next";
|
|
30
32
|
|
|
@@ -43,10 +45,10 @@ export const PagerLink: Component<PagerLinkProps> = (props) => {
|
|
|
43
45
|
<CardDescription class="flex items-center gap-1 text-xs font-medium text-muted-foreground">
|
|
44
46
|
<Show when={!isNext()}>
|
|
45
47
|
<ChevronLeft class="size-3.5 transition-transform group-hover:-translate-x-0.5" />
|
|
46
|
-
<span>
|
|
48
|
+
<span>{siteLocale.t("pagination.previous")}</span>
|
|
47
49
|
</Show>
|
|
48
50
|
<Show when={isNext()}>
|
|
49
|
-
<span>
|
|
51
|
+
<span>{siteLocale.t("pagination.next")}</span>
|
|
50
52
|
<ChevronRight class="size-3.5 transition-transform group-hover:translate-x-0.5" />
|
|
51
53
|
</Show>
|
|
52
54
|
</CardDescription>
|
|
@@ -69,7 +69,11 @@ export const SectionHeading: Component<SectionHeadingProps> = (props) => {
|
|
|
69
69
|
|
|
70
70
|
return (
|
|
71
71
|
<div class={cn("space-y-2", local.class)} {...rest}>
|
|
72
|
-
<div class={cn(
|
|
72
|
+
<div class={cn(
|
|
73
|
+
"flex justify-between gap-2",
|
|
74
|
+
v() === "page" ? "flex-col items-start sm:flex-row sm:items-center" : "items-center",
|
|
75
|
+
v() === "section" && "w-full",
|
|
76
|
+
)}>
|
|
73
77
|
<div class="flex min-w-0 items-center gap-2">
|
|
74
78
|
<Show when={v() === "page"}>
|
|
75
79
|
<h1 class={headingVariants({ variant: v() })}>
|
|
@@ -19,6 +19,7 @@ import { Tooltip, TooltipTrigger, TooltipContent } from "@/components/ui/tooltip
|
|
|
19
19
|
import { Skeleton } from "@/components/ui/skeleton";
|
|
20
20
|
import { Separator } from "@/components/ui/separator";
|
|
21
21
|
import { cn } from "@/lib/cn";
|
|
22
|
+
import { useSiteLocale } from "../../plugins/i18n/runtime.jsx";
|
|
22
23
|
|
|
23
24
|
/* --- Constants --- */
|
|
24
25
|
const SIDEBAR_COOKIE_NAME = "nikala_sidebar_state";
|
|
@@ -245,11 +246,12 @@ export interface SidebarTriggerProps extends JSX.ButtonHTMLAttributes<HTMLButton
|
|
|
245
246
|
export const SidebarTrigger: Component<SidebarTriggerProps> = (props) => {
|
|
246
247
|
const [local, rest] = splitProps(props, ["class", "onClick"]);
|
|
247
248
|
const { toggleSidebar, side, isMobile, openMobile, setOpenMobile } = useSidebar();
|
|
249
|
+
const siteLocale = useSiteLocale();
|
|
248
250
|
|
|
249
251
|
return (
|
|
250
252
|
<button
|
|
251
253
|
type="button"
|
|
252
|
-
aria-label="
|
|
254
|
+
aria-label={siteLocale.t("navigation.sidebar")}
|
|
253
255
|
onClick={(e) => {
|
|
254
256
|
if (typeof local.onClick === "function") local.onClick(e);
|
|
255
257
|
if (isMobile()) setOpenMobile(!openMobile());
|
|
@@ -264,7 +266,7 @@ export const SidebarTrigger: Component<SidebarTriggerProps> = (props) => {
|
|
|
264
266
|
<Show when={side() === "right"} fallback={<PanelLeft class="size-4" />}>
|
|
265
267
|
<PanelRight class="size-4" />
|
|
266
268
|
</Show>
|
|
267
|
-
<span class="sr-only">
|
|
269
|
+
<span class="sr-only">{siteLocale.t("navigation.sidebar")}</span>
|
|
268
270
|
</button>
|
|
269
271
|
);
|
|
270
272
|
};
|
|
@@ -277,15 +279,16 @@ export interface SidebarRailProps extends JSX.ButtonHTMLAttributes<HTMLButtonEle
|
|
|
277
279
|
export const SidebarRail: Component<SidebarRailProps> = (props) => {
|
|
278
280
|
const [local, rest] = splitProps(props, ["class"]);
|
|
279
281
|
const { toggleSidebar } = useSidebar();
|
|
282
|
+
const siteLocale = useSiteLocale();
|
|
280
283
|
|
|
281
284
|
return (
|
|
282
285
|
<button
|
|
283
286
|
type="button"
|
|
284
287
|
data-sidebar="rail"
|
|
285
|
-
aria-label="
|
|
288
|
+
aria-label={siteLocale.t("navigation.toggleSidebarRail")}
|
|
286
289
|
tabIndex={-1}
|
|
287
290
|
onClick={toggleSidebar}
|
|
288
|
-
title="
|
|
291
|
+
title={siteLocale.t("navigation.sidebar")}
|
|
289
292
|
class={cn(
|
|
290
293
|
"absolute inset-y-0 z-20 hidden w-4 -translate-x-1/2 transition-all ease-linear after:absolute after:inset-y-0 after:left-1/2 after:w-[2px] hover:after:bg-border group-data-[side=left]:-right-4 group-data-[side=right]:left-0 sm:flex cursor-w-resize",
|
|
291
294
|
local.class
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { splitProps, type Component, type JSX } from "solid-js";
|
|
2
2
|
import { cva, type VariantProps } from "class-variance-authority";
|
|
3
3
|
import { cn } from "@/lib/cn";
|
|
4
|
+
import { useSiteLocale } from "../../plugins/i18n/runtime.jsx";
|
|
4
5
|
|
|
5
6
|
export const spinnerVariants = cva(
|
|
6
7
|
"inline-block animate-spin rounded-lg border-2 border-current border-r-transparent text-primary",
|
|
@@ -29,11 +30,12 @@ export interface SpinnerProps
|
|
|
29
30
|
/** A compact, accessible loading indicator for async UI states. */
|
|
30
31
|
export const Spinner: Component<SpinnerProps> = (props) => {
|
|
31
32
|
const [local, rest] = splitProps(props, ["size", "label", "class"]);
|
|
33
|
+
const siteLocale = useSiteLocale();
|
|
32
34
|
|
|
33
35
|
return (
|
|
34
36
|
<span
|
|
35
37
|
role="status"
|
|
36
|
-
aria-label={local.label || "
|
|
38
|
+
aria-label={local.label || siteLocale.t("status.loadingLabel")}
|
|
37
39
|
class={cn(spinnerVariants({ size: local.size }), local.class)}
|
|
38
40
|
{...rest}
|
|
39
41
|
/>
|
|
@@ -21,6 +21,7 @@ import {
|
|
|
21
21
|
DropdownMenuSeparator,
|
|
22
22
|
} from "./dropdown-menu";
|
|
23
23
|
import { cn } from "@/lib/cn";
|
|
24
|
+
import { useSiteLocale } from "../../plugins/i18n/runtime.jsx";
|
|
24
25
|
|
|
25
26
|
export interface ThemeToggleProps
|
|
26
27
|
extends Omit<JSX.ButtonHTMLAttributes<HTMLButtonElement>, "onClick"> {
|
|
@@ -56,6 +57,7 @@ const RADIUS_OPTIONS: { value: Radius; label: string }[] = [
|
|
|
56
57
|
* Interactive UI theme switcher supporting button, mini (dropdown), and max (customizer panel) modes with View Transition animations.
|
|
57
58
|
*/
|
|
58
59
|
export const ThemeToggle: Component<ThemeToggleProps> = (props) => {
|
|
60
|
+
const siteLocale = useSiteLocale();
|
|
59
61
|
const [local, rest] = splitProps(props, ["mode", "effect", "variant", "size", "class"]);
|
|
60
62
|
const { theme, setTheme, accent, setAccent, radius, setRadius } = useTheme();
|
|
61
63
|
|
|
@@ -94,7 +96,7 @@ export const ThemeToggle: Component<ThemeToggleProps> = (props) => {
|
|
|
94
96
|
size={local.size || "icon"}
|
|
95
97
|
onClick={handleToggleClick}
|
|
96
98
|
class={cn("relative cursor-pointer", local.class)}
|
|
97
|
-
aria-label="
|
|
99
|
+
aria-label={siteLocale.t("theme.toggle")}
|
|
98
100
|
{...rest}
|
|
99
101
|
>
|
|
100
102
|
<Show
|
|
@@ -103,7 +105,7 @@ export const ThemeToggle: Component<ThemeToggleProps> = (props) => {
|
|
|
103
105
|
>
|
|
104
106
|
<Moon class="h-4 w-4 text-foreground transition-transform" />
|
|
105
107
|
</Show>
|
|
106
|
-
<span class="sr-only">
|
|
108
|
+
<span class="sr-only">{siteLocale.t("theme.toggle")}</span>
|
|
107
109
|
</Button>
|
|
108
110
|
}
|
|
109
111
|
>
|
|
@@ -122,7 +124,7 @@ export const ThemeToggle: Component<ThemeToggleProps> = (props) => {
|
|
|
122
124
|
>
|
|
123
125
|
<Moon class="h-4 w-4 text-foreground transition-transform" />
|
|
124
126
|
</Show>
|
|
125
|
-
<span class="sr-only">
|
|
127
|
+
<span class="sr-only">{siteLocale.t("theme.toggle")}</span>
|
|
126
128
|
</DropdownMenuTrigger>
|
|
127
129
|
|
|
128
130
|
<Show
|
|
@@ -132,17 +134,17 @@ export const ThemeToggle: Component<ThemeToggleProps> = (props) => {
|
|
|
132
134
|
<DropdownMenuContent>
|
|
133
135
|
<DropdownMenuItem onClick={(e: MouseEvent) => changeThemeWithEffect("light", e)}>
|
|
134
136
|
<Sun class="mr-2 h-4 w-4 text-muted-foreground" />
|
|
135
|
-
|
|
137
|
+
{siteLocale.t("theme.light")}
|
|
136
138
|
</DropdownMenuItem>
|
|
137
139
|
|
|
138
140
|
<DropdownMenuItem onClick={(e: MouseEvent) => changeThemeWithEffect("dark", e)}>
|
|
139
141
|
<Moon class="mr-2 h-4 w-4 text-muted-foreground" />
|
|
140
|
-
|
|
142
|
+
{siteLocale.t("theme.dark")}
|
|
141
143
|
</DropdownMenuItem>
|
|
142
144
|
|
|
143
145
|
<DropdownMenuItem onClick={(e: MouseEvent) => changeThemeWithEffect("system", e)}>
|
|
144
146
|
<Monitor class="mr-2 h-4 w-4 text-muted-foreground" />
|
|
145
|
-
|
|
147
|
+
{siteLocale.t("theme.system")}
|
|
146
148
|
</DropdownMenuItem>
|
|
147
149
|
</DropdownMenuContent>
|
|
148
150
|
}
|
|
@@ -152,7 +154,7 @@ export const ThemeToggle: Component<ThemeToggleProps> = (props) => {
|
|
|
152
154
|
<div class="p-2 space-y-2">
|
|
153
155
|
<DropdownMenuGroup>
|
|
154
156
|
<DropdownMenuLabel class="px-0 pt-0 text-xs font-semibold uppercase tracking-wider text-muted-foreground">
|
|
155
|
-
|
|
157
|
+
{siteLocale.t("theme.mode")}
|
|
156
158
|
</DropdownMenuLabel>
|
|
157
159
|
<div class="grid grid-cols-3 gap-1 my-1.5">
|
|
158
160
|
<Button
|
|
@@ -161,7 +163,7 @@ export const ThemeToggle: Component<ThemeToggleProps> = (props) => {
|
|
|
161
163
|
onClick={(e: MouseEvent) => changeThemeWithEffect("light", e)}
|
|
162
164
|
class="h-8 text-xs cursor-pointer"
|
|
163
165
|
>
|
|
164
|
-
|
|
166
|
+
{siteLocale.t("theme.light")}
|
|
165
167
|
</Button>
|
|
166
168
|
<Button
|
|
167
169
|
variant={theme() === "dark" ? "default" : "outline"}
|
|
@@ -169,7 +171,7 @@ export const ThemeToggle: Component<ThemeToggleProps> = (props) => {
|
|
|
169
171
|
onClick={(e: MouseEvent) => changeThemeWithEffect("dark", e)}
|
|
170
172
|
class="h-8 text-xs cursor-pointer"
|
|
171
173
|
>
|
|
172
|
-
|
|
174
|
+
{siteLocale.t("theme.dark")}
|
|
173
175
|
</Button>
|
|
174
176
|
<Button
|
|
175
177
|
variant={theme() === "system" ? "default" : "outline"}
|
|
@@ -177,7 +179,7 @@ export const ThemeToggle: Component<ThemeToggleProps> = (props) => {
|
|
|
177
179
|
onClick={(e: MouseEvent) => changeThemeWithEffect("system", e)}
|
|
178
180
|
class="h-8 text-xs cursor-pointer"
|
|
179
181
|
>
|
|
180
|
-
|
|
182
|
+
{siteLocale.t("theme.system")}
|
|
181
183
|
</Button>
|
|
182
184
|
</div>
|
|
183
185
|
</DropdownMenuGroup>
|
|
@@ -186,7 +188,7 @@ export const ThemeToggle: Component<ThemeToggleProps> = (props) => {
|
|
|
186
188
|
|
|
187
189
|
<DropdownMenuGroup>
|
|
188
190
|
<DropdownMenuLabel class="px-0 text-xs font-semibold uppercase tracking-wider text-muted-foreground">
|
|
189
|
-
|
|
191
|
+
{siteLocale.t("theme.brandAccentColor")}
|
|
190
192
|
</DropdownMenuLabel>
|
|
191
193
|
<div class="flex flex-wrap gap-1.5 my-1.5">
|
|
192
194
|
<For each={ACCENT_OPTIONS}>
|
|
@@ -212,7 +214,7 @@ export const ThemeToggle: Component<ThemeToggleProps> = (props) => {
|
|
|
212
214
|
|
|
213
215
|
<DropdownMenuGroup>
|
|
214
216
|
<DropdownMenuLabel class="px-0 text-xs font-semibold uppercase tracking-wider text-muted-foreground">
|
|
215
|
-
|
|
217
|
+
{siteLocale.t("theme.borderRadius")}
|
|
216
218
|
</DropdownMenuLabel>
|
|
217
219
|
<div class="grid grid-cols-5 gap-1 my-1.5">
|
|
218
220
|
<For each={RADIUS_OPTIONS}>
|
|
@@ -235,4 +237,4 @@ export const ThemeToggle: Component<ThemeToggleProps> = (props) => {
|
|
|
235
237
|
</DropdownMenu>
|
|
236
238
|
</Show>
|
|
237
239
|
);
|
|
238
|
-
};
|
|
240
|
+
};
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { For, Show, type Component } from "solid-js";
|
|
2
|
+
import { Languages, ChevronDown } from "lucide-solid";
|
|
3
|
+
import { Button } from "@/components/ui/button";
|
|
4
|
+
import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from "@/components/ui/dropdown-menu";
|
|
5
|
+
import { cn } from "@/lib/cn";
|
|
6
|
+
import { useSiteLocale } from "../runtime.jsx";
|
|
7
|
+
import type { PageData } from "../../../types.js";
|
|
8
|
+
import { findLocalizedPage } from "../navigation.js";
|
|
9
|
+
|
|
10
|
+
interface SiteLanguageSwitcherProps {
|
|
11
|
+
currentPage?: PageData;
|
|
12
|
+
pages?: readonly PageData[];
|
|
13
|
+
onNavigate?: (url: string) => void;
|
|
14
|
+
class?: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export const DocsSiteLanguageSwitcher: Component<SiteLanguageSwitcherProps> = (props) => {
|
|
18
|
+
const siteLocale = useSiteLocale();
|
|
19
|
+
const targetPage = (locale: string) => {
|
|
20
|
+
if (!props.currentPage || !props.pages) return undefined;
|
|
21
|
+
return findLocalizedPage(props.pages, props.currentPage, locale);
|
|
22
|
+
};
|
|
23
|
+
const selectLocale = (locale: string) => {
|
|
24
|
+
const page = targetPage(locale);
|
|
25
|
+
if (!page) return;
|
|
26
|
+
siteLocale.setLocale(locale);
|
|
27
|
+
if (typeof window !== "undefined" && window.location.pathname !== page.url) props.onNavigate?.(page.url);
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
return (
|
|
31
|
+
<Show when={siteLocale.locales().length > 1}>
|
|
32
|
+
<DropdownMenu placement="bottom-end">
|
|
33
|
+
<DropdownMenuTrigger
|
|
34
|
+
as={Button}
|
|
35
|
+
variant="ghost"
|
|
36
|
+
size="sm"
|
|
37
|
+
class={cn("shrink-0 gap-1", props.class)}
|
|
38
|
+
aria-label={siteLocale.t("navigation.chooseLanguage")}
|
|
39
|
+
>
|
|
40
|
+
<Languages class="size-3.5" />
|
|
41
|
+
<span class="hidden sm:inline">{siteLocale.locale()}</span>
|
|
42
|
+
<ChevronDown class="size-3.5" />
|
|
43
|
+
</DropdownMenuTrigger>
|
|
44
|
+
<DropdownMenuContent>
|
|
45
|
+
<For each={siteLocale.locales()}>
|
|
46
|
+
{(locale) => (
|
|
47
|
+
<DropdownMenuItem disabled={!targetPage(locale)} onClick={() => selectLocale(locale)}>
|
|
48
|
+
{locale.toUpperCase()}
|
|
49
|
+
</DropdownMenuItem>
|
|
50
|
+
)}
|
|
51
|
+
</For>
|
|
52
|
+
</DropdownMenuContent>
|
|
53
|
+
</DropdownMenu>
|
|
54
|
+
</Show>
|
|
55
|
+
);
|
|
56
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const SITE_LOCALE_STORAGE_KEY = "folio-site-locale";
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { createFolioPlugin, type FolioPlugin } from "../../plugin.js";
|
|
2
|
+
|
|
3
|
+
export * from "./site-locale.js";
|
|
4
|
+
|
|
5
|
+
export interface I18nPluginOptions {
|
|
6
|
+
defaultLocale: string;
|
|
7
|
+
locales: readonly string[];
|
|
8
|
+
frontmatterKey?: string;
|
|
9
|
+
translations?: {
|
|
10
|
+
directory?: string;
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function normalizeLocale(value: string, label: string): string {
|
|
15
|
+
const locale = value.trim();
|
|
16
|
+
if (!locale) throw new Error(`[folio:i18n] ${label} must not be empty`);
|
|
17
|
+
return locale;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function normalizeOptions(options: I18nPluginOptions): Required<I18nPluginOptions> {
|
|
21
|
+
const defaultLocale = normalizeLocale(options.defaultLocale, "defaultLocale");
|
|
22
|
+
const locales = options.locales.map((locale, index) => normalizeLocale(locale, `locales[${index}]`));
|
|
23
|
+
if (locales.length === 0) throw new Error("[folio:i18n] locales must contain at least one locale");
|
|
24
|
+
if (!locales.includes(defaultLocale)) {
|
|
25
|
+
throw new Error(`[folio:i18n] defaultLocale "${defaultLocale}" must be included in locales`);
|
|
26
|
+
}
|
|
27
|
+
if (new Set(locales).size !== locales.length) {
|
|
28
|
+
throw new Error("[folio:i18n] locales must not contain duplicates");
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return {
|
|
32
|
+
defaultLocale,
|
|
33
|
+
locales,
|
|
34
|
+
frontmatterKey: options.frontmatterKey?.trim() || "locale",
|
|
35
|
+
translations: {
|
|
36
|
+
directory: options.translations?.directory?.trim() || "locales",
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function routeFromLocaleDirectory(page: { url: string; sourcePath?: string }, locale: string, defaultLocale: string): string {
|
|
42
|
+
const sourcePath = page.sourcePath?.split("/") ?? [];
|
|
43
|
+
const sourceLocale = sourcePath[0];
|
|
44
|
+
if (sourceLocale !== locale) return page.url;
|
|
45
|
+
|
|
46
|
+
const segments = page.url.split("/").filter(Boolean);
|
|
47
|
+
if (locale === defaultLocale) segments.shift();
|
|
48
|
+
return segments.length ? `/${segments.join("/")}` : "/";
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function slugFromUrl(url: string): string {
|
|
52
|
+
return url === "/" ? "index" : url.slice(1).replace(/\//g, "-");
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function createI18nPlugin(options: I18nPluginOptions): FolioPlugin {
|
|
56
|
+
const resolved = normalizeOptions(options);
|
|
57
|
+
|
|
58
|
+
return createFolioPlugin(() => ({
|
|
59
|
+
name: "i18n",
|
|
60
|
+
config: {
|
|
61
|
+
uiLocale: {
|
|
62
|
+
defaultLocale: resolved.defaultLocale,
|
|
63
|
+
directory: resolved.translations.directory,
|
|
64
|
+
},
|
|
65
|
+
},
|
|
66
|
+
pagesGenerated: (pages) => pages.map((page) => {
|
|
67
|
+
const sourceLocale = page.sourcePath?.split("/")[0];
|
|
68
|
+
if (sourceLocale && resolved.locales.includes(sourceLocale)) {
|
|
69
|
+
const url = routeFromLocaleDirectory(page, sourceLocale, resolved.defaultLocale);
|
|
70
|
+
return {
|
|
71
|
+
...page,
|
|
72
|
+
slug: slugFromUrl(url),
|
|
73
|
+
url,
|
|
74
|
+
frontmatter: {
|
|
75
|
+
...page.frontmatter,
|
|
76
|
+
[resolved.frontmatterKey]: sourceLocale,
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const declaredLocale = page.frontmatter[resolved.frontmatterKey];
|
|
82
|
+
if (declaredLocale !== undefined && String(declaredLocale) !== resolved.defaultLocale) {
|
|
83
|
+
throw new Error(
|
|
84
|
+
`[folio:i18n] non-default locale "${String(declaredLocale)}" must be stored under a matching locale directory`,
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
return page;
|
|
88
|
+
}),
|
|
89
|
+
pageTransformed: (page) => {
|
|
90
|
+
const rawLocale = page.frontmatter[resolved.frontmatterKey];
|
|
91
|
+
const locale = rawLocale === undefined
|
|
92
|
+
? resolved.defaultLocale
|
|
93
|
+
: normalizeLocale(String(rawLocale), `frontmatter.${resolved.frontmatterKey}`);
|
|
94
|
+
|
|
95
|
+
if (!resolved.locales.includes(locale)) {
|
|
96
|
+
throw new Error(
|
|
97
|
+
`[folio:i18n] unsupported locale "${locale}"; expected one of ${resolved.locales.join(", ")}`,
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return {
|
|
102
|
+
...page,
|
|
103
|
+
frontmatter: {
|
|
104
|
+
...page.frontmatter,
|
|
105
|
+
[resolved.frontmatterKey]: locale,
|
|
106
|
+
},
|
|
107
|
+
};
|
|
108
|
+
},
|
|
109
|
+
}));
|
|
110
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { PageData, SidebarItem } from "../../types.js";
|
|
2
|
+
|
|
3
|
+
export function getPageLocale(page: PageData): string | undefined {
|
|
4
|
+
const locale = page.frontmatter.locale;
|
|
5
|
+
return typeof locale === "string" ? locale : undefined;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export function getPageTranslationKey(page: PageData): string {
|
|
9
|
+
const locale = getPageLocale(page);
|
|
10
|
+
const sourcePath = page.sourcePath || page.url;
|
|
11
|
+
const prefix = locale ? `${locale}/` : "";
|
|
12
|
+
return locale && sourcePath.startsWith(prefix) ? sourcePath.slice(prefix.length) : sourcePath;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function findLocalizedPage(
|
|
16
|
+
pages: readonly PageData[],
|
|
17
|
+
page: PageData | undefined,
|
|
18
|
+
locale: string,
|
|
19
|
+
): PageData | undefined {
|
|
20
|
+
if (!page) return undefined;
|
|
21
|
+
const key = getPageTranslationKey(page);
|
|
22
|
+
return pages.find((candidate) => getPageLocale(candidate) === locale && getPageTranslationKey(candidate) === key);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function localizeSidebarTree(
|
|
26
|
+
tree: readonly SidebarItem[],
|
|
27
|
+
pages: readonly PageData[],
|
|
28
|
+
locale: string,
|
|
29
|
+
): SidebarItem[] {
|
|
30
|
+
const localizedPages = new Map(
|
|
31
|
+
pages
|
|
32
|
+
.filter((page) => getPageLocale(page) === locale)
|
|
33
|
+
.map((page) => [getPageTranslationKey(page), page]),
|
|
34
|
+
);
|
|
35
|
+
const pageByHref = new Map(pages.map((page) => [page.url, page]));
|
|
36
|
+
|
|
37
|
+
const localize = (item: SidebarItem): SidebarItem => {
|
|
38
|
+
const sourcePage = item.href ? pageByHref.get(item.href) : undefined;
|
|
39
|
+
const localizedPage = sourcePage ? localizedPages.get(getPageTranslationKey(sourcePage)) : undefined;
|
|
40
|
+
return {
|
|
41
|
+
...item,
|
|
42
|
+
title: localizedPage?.title || item.title,
|
|
43
|
+
href: localizedPage?.url || item.href,
|
|
44
|
+
items: item.items?.map(localize),
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
return tree.map(localize);
|
|
49
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { createContext, createEffect, createSignal, useContext, type Accessor, type ParentComponent } from "solid-js";
|
|
2
|
+
import type { PageData } from "../../types.js";
|
|
3
|
+
import { findLocalizedPage, getPageLocale } from "./navigation.js";
|
|
4
|
+
import { SITE_LOCALE_STORAGE_KEY } from "./constants.js";
|
|
5
|
+
import type { DocsUiLocaleConfig, SiteTranslationKey } from "./site-locale.js";
|
|
6
|
+
|
|
7
|
+
interface SiteLocaleContextValue {
|
|
8
|
+
locale: Accessor<string>;
|
|
9
|
+
locales: Accessor<string[]>;
|
|
10
|
+
setLocale: (locale: string) => void;
|
|
11
|
+
t: (key: SiteTranslationKey, values?: Record<string, string>) => string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
type SiteLocaleRuntimeGlobal = typeof globalThis & {
|
|
15
|
+
__folioSiteLocaleContext?: ReturnType<typeof createContext<SiteLocaleContextValue | undefined>>;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
// Dev mode can load the bundled client and source theme files in one realm.
|
|
19
|
+
// Keep one context identity across those module copies without coupling the
|
|
20
|
+
// server resolver to a plugin-specific filesystem path.
|
|
21
|
+
const runtimeGlobal = globalThis as SiteLocaleRuntimeGlobal;
|
|
22
|
+
const SiteLocaleContext = runtimeGlobal.__folioSiteLocaleContext
|
|
23
|
+
|| (runtimeGlobal.__folioSiteLocaleContext = createContext<SiteLocaleContextValue>());
|
|
24
|
+
|
|
25
|
+
interface SiteLocaleProviderProps {
|
|
26
|
+
config?: DocsUiLocaleConfig;
|
|
27
|
+
pages?: readonly PageData[];
|
|
28
|
+
currentPage?: PageData;
|
|
29
|
+
currentPath?: string;
|
|
30
|
+
onNavigate?: (url: string) => void;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export const SiteLocaleProvider: ParentComponent<SiteLocaleProviderProps> = (props) => {
|
|
34
|
+
const configuredInitial = props.config?.locale || props.config?.defaultLocale || "en";
|
|
35
|
+
const currentPage = () => props.currentPage || props.pages?.find((page) => page.url === props.currentPath);
|
|
36
|
+
const pageInitial = () => {
|
|
37
|
+
const page = currentPage();
|
|
38
|
+
return (page ? getPageLocale(page) : undefined) || configuredInitial;
|
|
39
|
+
};
|
|
40
|
+
const [locale, setLocale] = createSignal(pageInitial());
|
|
41
|
+
const locales = () => Object.keys(props.config?.translations || {}).sort();
|
|
42
|
+
const setSiteLocale = (next: string) => {
|
|
43
|
+
if (locales().includes(next)) setLocale(next);
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
createEffect(() => {
|
|
47
|
+
const currentPath = props.currentPath;
|
|
48
|
+
if (typeof window === "undefined") return;
|
|
49
|
+
|
|
50
|
+
const saved = window.localStorage.getItem(SITE_LOCALE_STORAGE_KEY);
|
|
51
|
+
const preferredLocale = saved && locales().includes(saved) ? saved : pageInitial();
|
|
52
|
+
if (!currentPath) {
|
|
53
|
+
setSiteLocale(preferredLocale);
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const currentPage = props.pages?.find((page) => page.url === currentPath);
|
|
58
|
+
const localizedPage = findLocalizedPage(props.pages || [], currentPage, preferredLocale);
|
|
59
|
+
if (!localizedPage && currentPage) {
|
|
60
|
+
setSiteLocale(getPageLocale(currentPage) || configuredInitial);
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
setSiteLocale(preferredLocale);
|
|
64
|
+
if (localizedPage && localizedPage.url !== currentPath) props.onNavigate?.(localizedPage.url);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
const t = (key: SiteTranslationKey, values: Record<string, string> = {}) => {
|
|
68
|
+
const translations = props.config?.translations || {};
|
|
69
|
+
const defaultLocale = props.config?.defaultLocale || "en";
|
|
70
|
+
const text = translations[locale()]?.[key] || translations[defaultLocale]?.[key] || key;
|
|
71
|
+
return Object.entries(values).reduce((result, [name, value]) => result.replaceAll(`{${name}}`, value), text);
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
const value: SiteLocaleContextValue = {
|
|
75
|
+
locale,
|
|
76
|
+
locales,
|
|
77
|
+
setLocale: (next) => {
|
|
78
|
+
setSiteLocale(next);
|
|
79
|
+
window.localStorage.setItem(SITE_LOCALE_STORAGE_KEY, next);
|
|
80
|
+
},
|
|
81
|
+
t,
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
return <SiteLocaleContext.Provider value={value}>{props.children}</SiteLocaleContext.Provider>;
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
export function useSiteLocale(): SiteLocaleContextValue {
|
|
88
|
+
const context = useContext(SiteLocaleContext);
|
|
89
|
+
if (!context) throw new Error("useSiteLocale must be used within a SiteLocaleProvider");
|
|
90
|
+
return context;
|
|
91
|
+
}
|