@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
package/dist/plugin.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
const HOOKS = [
|
|
2
|
+
"configResolved",
|
|
3
|
+
"buildStart",
|
|
4
|
+
"pagesGenerated",
|
|
5
|
+
"pageCollected",
|
|
6
|
+
"pageTransformed",
|
|
7
|
+
"pageActions",
|
|
8
|
+
"generate",
|
|
9
|
+
"buildEnd",
|
|
10
|
+
];
|
|
11
|
+
export function validateFolioPlugin(plugin, label = "Plugin") {
|
|
12
|
+
if (!plugin || typeof plugin !== "object") {
|
|
13
|
+
throw new Error(`[folio] ${label} must be an object`);
|
|
14
|
+
}
|
|
15
|
+
const candidate = plugin;
|
|
16
|
+
if (typeof candidate.name !== "string" || !candidate.name.trim()) {
|
|
17
|
+
throw new Error(`[folio] ${label} must have a non-empty name`);
|
|
18
|
+
}
|
|
19
|
+
for (const hook of HOOKS) {
|
|
20
|
+
if (candidate[hook] !== undefined && typeof candidate[hook] !== "function") {
|
|
21
|
+
throw new Error(`[folio] Plugin "${candidate.name}" hook "${hook}" must be a function`);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
export function validateFolioPlugins(plugins) {
|
|
26
|
+
if (plugins === undefined)
|
|
27
|
+
return;
|
|
28
|
+
if (!Array.isArray(plugins)) {
|
|
29
|
+
throw new Error("[folio] DocsConfig.plugins must be an array");
|
|
30
|
+
}
|
|
31
|
+
const names = new Set();
|
|
32
|
+
for (const [index, plugin] of plugins.entries()) {
|
|
33
|
+
validateFolioPlugin(plugin, `Plugin at index ${index}`);
|
|
34
|
+
if (names.has(plugin.name)) {
|
|
35
|
+
throw new Error(`[folio] Plugin name "${plugin.name}" is registered more than once`);
|
|
36
|
+
}
|
|
37
|
+
names.add(plugin.name);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
export function defineFolioPlugin(plugin) {
|
|
41
|
+
validateFolioPlugin(plugin, "Plugin registration");
|
|
42
|
+
return plugin;
|
|
43
|
+
}
|
|
44
|
+
export function createFolioPlugin(factory, options) {
|
|
45
|
+
if (typeof factory !== "function") {
|
|
46
|
+
throw new Error("[folio] Plugin factory must be a function");
|
|
47
|
+
}
|
|
48
|
+
const plugin = options === undefined ? factory() : factory(options);
|
|
49
|
+
validateFolioPlugin(plugin, "Plugin factory result");
|
|
50
|
+
return plugin;
|
|
51
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type Component } from "solid-js";
|
|
2
|
+
import type { PageData } from "../../../types.js";
|
|
3
|
+
interface SiteLanguageSwitcherProps {
|
|
4
|
+
currentPage?: PageData;
|
|
5
|
+
pages?: readonly PageData[];
|
|
6
|
+
onNavigate?: (url: string) => void;
|
|
7
|
+
class?: string;
|
|
8
|
+
}
|
|
9
|
+
export declare const DocsSiteLanguageSwitcher: Component<SiteLanguageSwitcherProps>;
|
|
10
|
+
export {};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { For, Show } 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 { findLocalizedPage } from "../navigation.js";
|
|
8
|
+
export const DocsSiteLanguageSwitcher = (props) => {
|
|
9
|
+
const siteLocale = useSiteLocale();
|
|
10
|
+
const targetPage = (locale) => {
|
|
11
|
+
if (!props.currentPage || !props.pages)
|
|
12
|
+
return undefined;
|
|
13
|
+
return findLocalizedPage(props.pages, props.currentPage, locale);
|
|
14
|
+
};
|
|
15
|
+
const selectLocale = (locale) => {
|
|
16
|
+
const page = targetPage(locale);
|
|
17
|
+
if (!page)
|
|
18
|
+
return;
|
|
19
|
+
siteLocale.setLocale(locale);
|
|
20
|
+
if (typeof window !== "undefined" && window.location.pathname !== page.url)
|
|
21
|
+
props.onNavigate?.(page.url);
|
|
22
|
+
};
|
|
23
|
+
return (<Show when={siteLocale.locales().length > 1}>
|
|
24
|
+
<DropdownMenu placement="bottom-end">
|
|
25
|
+
<DropdownMenuTrigger as={Button} variant="ghost" size="sm" class={cn("shrink-0 gap-1", props.class)} aria-label={siteLocale.t("navigation.chooseLanguage")}>
|
|
26
|
+
<Languages class="size-3.5"/>
|
|
27
|
+
<span class="hidden sm:inline">{siteLocale.locale()}</span>
|
|
28
|
+
<ChevronDown class="size-3.5"/>
|
|
29
|
+
</DropdownMenuTrigger>
|
|
30
|
+
<DropdownMenuContent>
|
|
31
|
+
<For each={siteLocale.locales()}>
|
|
32
|
+
{(locale) => (<DropdownMenuItem disabled={!targetPage(locale)} onClick={() => selectLocale(locale)}>
|
|
33
|
+
{locale.toUpperCase()}
|
|
34
|
+
</DropdownMenuItem>)}
|
|
35
|
+
</For>
|
|
36
|
+
</DropdownMenuContent>
|
|
37
|
+
</DropdownMenu>
|
|
38
|
+
</Show>);
|
|
39
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const SITE_LOCALE_STORAGE_KEY = "folio-site-locale";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const SITE_LOCALE_STORAGE_KEY = "folio-site-locale";
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type FolioPlugin } from "../../plugin.js";
|
|
2
|
+
export * from "./site-locale.js";
|
|
3
|
+
export interface I18nPluginOptions {
|
|
4
|
+
defaultLocale: string;
|
|
5
|
+
locales: readonly string[];
|
|
6
|
+
frontmatterKey?: string;
|
|
7
|
+
translations?: {
|
|
8
|
+
directory?: string;
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
export declare function createI18nPlugin(options: I18nPluginOptions): FolioPlugin;
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { createFolioPlugin } from "../../plugin.js";
|
|
2
|
+
export * from "./site-locale.js";
|
|
3
|
+
function normalizeLocale(value, label) {
|
|
4
|
+
const locale = value.trim();
|
|
5
|
+
if (!locale)
|
|
6
|
+
throw new Error(`[folio:i18n] ${label} must not be empty`);
|
|
7
|
+
return locale;
|
|
8
|
+
}
|
|
9
|
+
function normalizeOptions(options) {
|
|
10
|
+
const defaultLocale = normalizeLocale(options.defaultLocale, "defaultLocale");
|
|
11
|
+
const locales = options.locales.map((locale, index) => normalizeLocale(locale, `locales[${index}]`));
|
|
12
|
+
if (locales.length === 0)
|
|
13
|
+
throw new Error("[folio:i18n] locales must contain at least one locale");
|
|
14
|
+
if (!locales.includes(defaultLocale)) {
|
|
15
|
+
throw new Error(`[folio:i18n] defaultLocale "${defaultLocale}" must be included in locales`);
|
|
16
|
+
}
|
|
17
|
+
if (new Set(locales).size !== locales.length) {
|
|
18
|
+
throw new Error("[folio:i18n] locales must not contain duplicates");
|
|
19
|
+
}
|
|
20
|
+
return {
|
|
21
|
+
defaultLocale,
|
|
22
|
+
locales,
|
|
23
|
+
frontmatterKey: options.frontmatterKey?.trim() || "locale",
|
|
24
|
+
translations: {
|
|
25
|
+
directory: options.translations?.directory?.trim() || "locales",
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
function routeFromLocaleDirectory(page, locale, defaultLocale) {
|
|
30
|
+
const sourcePath = page.sourcePath?.split("/") ?? [];
|
|
31
|
+
const sourceLocale = sourcePath[0];
|
|
32
|
+
if (sourceLocale !== locale)
|
|
33
|
+
return page.url;
|
|
34
|
+
const segments = page.url.split("/").filter(Boolean);
|
|
35
|
+
if (locale === defaultLocale)
|
|
36
|
+
segments.shift();
|
|
37
|
+
return segments.length ? `/${segments.join("/")}` : "/";
|
|
38
|
+
}
|
|
39
|
+
function slugFromUrl(url) {
|
|
40
|
+
return url === "/" ? "index" : url.slice(1).replace(/\//g, "-");
|
|
41
|
+
}
|
|
42
|
+
export function createI18nPlugin(options) {
|
|
43
|
+
const resolved = normalizeOptions(options);
|
|
44
|
+
return createFolioPlugin(() => ({
|
|
45
|
+
name: "i18n",
|
|
46
|
+
config: {
|
|
47
|
+
uiLocale: {
|
|
48
|
+
defaultLocale: resolved.defaultLocale,
|
|
49
|
+
directory: resolved.translations.directory,
|
|
50
|
+
},
|
|
51
|
+
},
|
|
52
|
+
pagesGenerated: (pages) => pages.map((page) => {
|
|
53
|
+
const sourceLocale = page.sourcePath?.split("/")[0];
|
|
54
|
+
if (sourceLocale && resolved.locales.includes(sourceLocale)) {
|
|
55
|
+
const url = routeFromLocaleDirectory(page, sourceLocale, resolved.defaultLocale);
|
|
56
|
+
return {
|
|
57
|
+
...page,
|
|
58
|
+
slug: slugFromUrl(url),
|
|
59
|
+
url,
|
|
60
|
+
frontmatter: {
|
|
61
|
+
...page.frontmatter,
|
|
62
|
+
[resolved.frontmatterKey]: sourceLocale,
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
const declaredLocale = page.frontmatter[resolved.frontmatterKey];
|
|
67
|
+
if (declaredLocale !== undefined && String(declaredLocale) !== resolved.defaultLocale) {
|
|
68
|
+
throw new Error(`[folio:i18n] non-default locale "${String(declaredLocale)}" must be stored under a matching locale directory`);
|
|
69
|
+
}
|
|
70
|
+
return page;
|
|
71
|
+
}),
|
|
72
|
+
pageTransformed: (page) => {
|
|
73
|
+
const rawLocale = page.frontmatter[resolved.frontmatterKey];
|
|
74
|
+
const locale = rawLocale === undefined
|
|
75
|
+
? resolved.defaultLocale
|
|
76
|
+
: normalizeLocale(String(rawLocale), `frontmatter.${resolved.frontmatterKey}`);
|
|
77
|
+
if (!resolved.locales.includes(locale)) {
|
|
78
|
+
throw new Error(`[folio:i18n] unsupported locale "${locale}"; expected one of ${resolved.locales.join(", ")}`);
|
|
79
|
+
}
|
|
80
|
+
return {
|
|
81
|
+
...page,
|
|
82
|
+
frontmatter: {
|
|
83
|
+
...page.frontmatter,
|
|
84
|
+
[resolved.frontmatterKey]: locale,
|
|
85
|
+
},
|
|
86
|
+
};
|
|
87
|
+
},
|
|
88
|
+
}));
|
|
89
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { PageData, SidebarItem } from "../../types.js";
|
|
2
|
+
export declare function getPageLocale(page: PageData): string | undefined;
|
|
3
|
+
export declare function getPageTranslationKey(page: PageData): string;
|
|
4
|
+
export declare function findLocalizedPage(pages: readonly PageData[], page: PageData | undefined, locale: string): PageData | undefined;
|
|
5
|
+
export declare function localizeSidebarTree(tree: readonly SidebarItem[], pages: readonly PageData[], locale: string): SidebarItem[];
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
export function getPageLocale(page) {
|
|
2
|
+
const locale = page.frontmatter.locale;
|
|
3
|
+
return typeof locale === "string" ? locale : undefined;
|
|
4
|
+
}
|
|
5
|
+
export function getPageTranslationKey(page) {
|
|
6
|
+
const locale = getPageLocale(page);
|
|
7
|
+
const sourcePath = page.sourcePath || page.url;
|
|
8
|
+
const prefix = locale ? `${locale}/` : "";
|
|
9
|
+
return locale && sourcePath.startsWith(prefix) ? sourcePath.slice(prefix.length) : sourcePath;
|
|
10
|
+
}
|
|
11
|
+
export function findLocalizedPage(pages, page, locale) {
|
|
12
|
+
if (!page)
|
|
13
|
+
return undefined;
|
|
14
|
+
const key = getPageTranslationKey(page);
|
|
15
|
+
return pages.find((candidate) => getPageLocale(candidate) === locale && getPageTranslationKey(candidate) === key);
|
|
16
|
+
}
|
|
17
|
+
export function localizeSidebarTree(tree, pages, locale) {
|
|
18
|
+
const localizedPages = new Map(pages
|
|
19
|
+
.filter((page) => getPageLocale(page) === locale)
|
|
20
|
+
.map((page) => [getPageTranslationKey(page), page]));
|
|
21
|
+
const pageByHref = new Map(pages.map((page) => [page.url, page]));
|
|
22
|
+
const localize = (item) => {
|
|
23
|
+
const sourcePage = item.href ? pageByHref.get(item.href) : undefined;
|
|
24
|
+
const localizedPage = sourcePage ? localizedPages.get(getPageTranslationKey(sourcePage)) : undefined;
|
|
25
|
+
return {
|
|
26
|
+
...item,
|
|
27
|
+
title: localizedPage?.title || item.title,
|
|
28
|
+
href: localizedPage?.url || item.href,
|
|
29
|
+
items: item.items?.map(localize),
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
return tree.map(localize);
|
|
33
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { type Accessor, type ParentComponent } from "solid-js";
|
|
2
|
+
import type { PageData } from "../../types.js";
|
|
3
|
+
import type { DocsUiLocaleConfig, SiteTranslationKey } from "./site-locale.js";
|
|
4
|
+
interface SiteLocaleContextValue {
|
|
5
|
+
locale: Accessor<string>;
|
|
6
|
+
locales: Accessor<string[]>;
|
|
7
|
+
setLocale: (locale: string) => void;
|
|
8
|
+
t: (key: SiteTranslationKey, values?: Record<string, string>) => string;
|
|
9
|
+
}
|
|
10
|
+
interface SiteLocaleProviderProps {
|
|
11
|
+
config?: DocsUiLocaleConfig;
|
|
12
|
+
pages?: readonly PageData[];
|
|
13
|
+
currentPage?: PageData;
|
|
14
|
+
currentPath?: string;
|
|
15
|
+
onNavigate?: (url: string) => void;
|
|
16
|
+
}
|
|
17
|
+
export declare const SiteLocaleProvider: ParentComponent<SiteLocaleProviderProps>;
|
|
18
|
+
export declare function useSiteLocale(): SiteLocaleContextValue;
|
|
19
|
+
export {};
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { createContext, createEffect, createSignal, useContext } from "solid-js";
|
|
2
|
+
import { findLocalizedPage, getPageLocale } from "./navigation.js";
|
|
3
|
+
import { SITE_LOCALE_STORAGE_KEY } from "./constants.js";
|
|
4
|
+
// Dev mode can load the bundled client and source theme files in one realm.
|
|
5
|
+
// Keep one context identity across those module copies without coupling the
|
|
6
|
+
// server resolver to a plugin-specific filesystem path.
|
|
7
|
+
const runtimeGlobal = globalThis;
|
|
8
|
+
const SiteLocaleContext = runtimeGlobal.__folioSiteLocaleContext
|
|
9
|
+
|| (runtimeGlobal.__folioSiteLocaleContext = createContext());
|
|
10
|
+
export const SiteLocaleProvider = (props) => {
|
|
11
|
+
const configuredInitial = props.config?.locale || props.config?.defaultLocale || "en";
|
|
12
|
+
const currentPage = () => props.currentPage || props.pages?.find((page) => page.url === props.currentPath);
|
|
13
|
+
const pageInitial = () => {
|
|
14
|
+
const page = currentPage();
|
|
15
|
+
return (page ? getPageLocale(page) : undefined) || configuredInitial;
|
|
16
|
+
};
|
|
17
|
+
const [locale, setLocale] = createSignal(pageInitial());
|
|
18
|
+
const locales = () => Object.keys(props.config?.translations || {}).sort();
|
|
19
|
+
const setSiteLocale = (next) => {
|
|
20
|
+
if (locales().includes(next))
|
|
21
|
+
setLocale(next);
|
|
22
|
+
};
|
|
23
|
+
createEffect(() => {
|
|
24
|
+
const currentPath = props.currentPath;
|
|
25
|
+
if (typeof window === "undefined")
|
|
26
|
+
return;
|
|
27
|
+
const saved = window.localStorage.getItem(SITE_LOCALE_STORAGE_KEY);
|
|
28
|
+
const preferredLocale = saved && locales().includes(saved) ? saved : pageInitial();
|
|
29
|
+
if (!currentPath) {
|
|
30
|
+
setSiteLocale(preferredLocale);
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
const currentPage = props.pages?.find((page) => page.url === currentPath);
|
|
34
|
+
const localizedPage = findLocalizedPage(props.pages || [], currentPage, preferredLocale);
|
|
35
|
+
if (!localizedPage && currentPage) {
|
|
36
|
+
setSiteLocale(getPageLocale(currentPage) || configuredInitial);
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
setSiteLocale(preferredLocale);
|
|
40
|
+
if (localizedPage && localizedPage.url !== currentPath)
|
|
41
|
+
props.onNavigate?.(localizedPage.url);
|
|
42
|
+
});
|
|
43
|
+
const t = (key, values = {}) => {
|
|
44
|
+
const translations = props.config?.translations || {};
|
|
45
|
+
const defaultLocale = props.config?.defaultLocale || "en";
|
|
46
|
+
const text = translations[locale()]?.[key] || translations[defaultLocale]?.[key] || key;
|
|
47
|
+
return Object.entries(values).reduce((result, [name, value]) => result.replaceAll(`{${name}}`, value), text);
|
|
48
|
+
};
|
|
49
|
+
const value = {
|
|
50
|
+
locale,
|
|
51
|
+
locales,
|
|
52
|
+
setLocale: (next) => {
|
|
53
|
+
setSiteLocale(next);
|
|
54
|
+
window.localStorage.setItem(SITE_LOCALE_STORAGE_KEY, next);
|
|
55
|
+
},
|
|
56
|
+
t,
|
|
57
|
+
};
|
|
58
|
+
return <SiteLocaleContext.Provider value={value}>{props.children}</SiteLocaleContext.Provider>;
|
|
59
|
+
};
|
|
60
|
+
export function useSiteLocale() {
|
|
61
|
+
const context = useContext(SiteLocaleContext);
|
|
62
|
+
if (!context)
|
|
63
|
+
throw new Error("useSiteLocale must be used within a SiteLocaleProvider");
|
|
64
|
+
return context;
|
|
65
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export { SITE_LOCALE_STORAGE_KEY } from "./constants.js";
|
|
2
|
+
export declare const SITE_TRANSLATION_KEYS: readonly ["navigation.search", "navigation.searchLabel", "navigation.toggleSidebar", "navigation.closeSidebar", "navigation.documentation", "navigation.primary", "navigation.github", "navigation.chooseLanguage", "navigation.toggleNavigation", "navigation.toggleSidebarRail", "navigation.sidebar", "navigation.collapse", "navigation.expand", "navigation.breadcrumb", "actions.viewSource", "actions.copyPage", "actions.copyAsMarkdown", "actions.learnMore", "actions.copyDocumentationPage", "actions.openInProvider", "actions.copied", "actions.prompt", "actions.copyContext", "actions.copyCliCommand", "actions.copyCode", "actions.exitFullscreen", "actions.expandFullscreen", "tableOfContents.title", "search.placeholder", "search.empty", "search.pages", "search.by", "search.commandPlaceholder", "search.noResults", "pagination.previous", "pagination.next", "theme.toggle", "theme.light", "theme.dark", "theme.system", "theme.mode", "theme.brandAccentColor", "theme.borderRadius", "viewer.preview", "viewer.code", "viewer.desktopViewport", "viewer.tabletViewport", "viewer.mobileViewport", "viewer.desktopSize", "viewer.tabletSize", "viewer.mobileSize", "viewer.toggleBackgroundGrid", "viewer.canvasGrid", "viewer.resetPreviewCanvas", "viewer.resetCanvas", "viewer.copyAiContext", "viewer.copyCodeForAi", "viewer.copyCliInstallation", "viewer.fullscreenPreview", "viewer.exitFullscreen", "code.copied", "code.copy", "command.navigate", "command.select", "command.close", "image.avatar", "status.loading", "status.loadingLabel", "accessibility.callout", "errors.pageNotFound", "errors.pageNotFoundDescription", "apiTable.property", "apiTable.type", "apiTable.default", "apiTable.description"];
|
|
3
|
+
export type SiteTranslationKey = (typeof SITE_TRANSLATION_KEYS)[number];
|
|
4
|
+
export type SiteTranslations = Record<SiteTranslationKey, string>;
|
|
5
|
+
export interface DocsUiLocaleConfig {
|
|
6
|
+
defaultLocale?: string;
|
|
7
|
+
directory?: string;
|
|
8
|
+
locale?: string;
|
|
9
|
+
translations?: Record<string, Partial<SiteTranslations>>;
|
|
10
|
+
}
|
|
11
|
+
export declare function loadSiteTranslations(rootDir: string, config?: DocsUiLocaleConfig): Promise<DocsUiLocaleConfig>;
|
|
12
|
+
export declare function getSiteTranslation(config: DocsUiLocaleConfig | undefined, key: SiteTranslationKey): string;
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import fs from "fs-extra";
|
|
3
|
+
export { SITE_LOCALE_STORAGE_KEY } from "./constants.js";
|
|
4
|
+
export const SITE_TRANSLATION_KEYS = [
|
|
5
|
+
"navigation.search",
|
|
6
|
+
"navigation.searchLabel",
|
|
7
|
+
"navigation.toggleSidebar",
|
|
8
|
+
"navigation.closeSidebar",
|
|
9
|
+
"navigation.documentation",
|
|
10
|
+
"navigation.primary",
|
|
11
|
+
"navigation.github",
|
|
12
|
+
"navigation.chooseLanguage",
|
|
13
|
+
"navigation.toggleNavigation",
|
|
14
|
+
"navigation.toggleSidebarRail",
|
|
15
|
+
"navigation.sidebar",
|
|
16
|
+
"navigation.collapse",
|
|
17
|
+
"navigation.expand",
|
|
18
|
+
"navigation.breadcrumb",
|
|
19
|
+
"actions.viewSource",
|
|
20
|
+
"actions.copyPage",
|
|
21
|
+
"actions.copyAsMarkdown",
|
|
22
|
+
"actions.learnMore",
|
|
23
|
+
"actions.copyDocumentationPage",
|
|
24
|
+
"actions.openInProvider",
|
|
25
|
+
"actions.copied",
|
|
26
|
+
"actions.prompt",
|
|
27
|
+
"actions.copyContext",
|
|
28
|
+
"actions.copyCliCommand",
|
|
29
|
+
"actions.copyCode",
|
|
30
|
+
"actions.exitFullscreen",
|
|
31
|
+
"actions.expandFullscreen",
|
|
32
|
+
"tableOfContents.title",
|
|
33
|
+
"search.placeholder",
|
|
34
|
+
"search.empty",
|
|
35
|
+
"search.pages",
|
|
36
|
+
"search.by",
|
|
37
|
+
"search.commandPlaceholder",
|
|
38
|
+
"search.noResults",
|
|
39
|
+
"pagination.previous",
|
|
40
|
+
"pagination.next",
|
|
41
|
+
"theme.toggle",
|
|
42
|
+
"theme.light",
|
|
43
|
+
"theme.dark",
|
|
44
|
+
"theme.system",
|
|
45
|
+
"theme.mode",
|
|
46
|
+
"theme.brandAccentColor",
|
|
47
|
+
"theme.borderRadius",
|
|
48
|
+
"viewer.preview",
|
|
49
|
+
"viewer.code",
|
|
50
|
+
"viewer.desktopViewport",
|
|
51
|
+
"viewer.tabletViewport",
|
|
52
|
+
"viewer.mobileViewport",
|
|
53
|
+
"viewer.desktopSize",
|
|
54
|
+
"viewer.tabletSize",
|
|
55
|
+
"viewer.mobileSize",
|
|
56
|
+
"viewer.toggleBackgroundGrid",
|
|
57
|
+
"viewer.canvasGrid",
|
|
58
|
+
"viewer.resetPreviewCanvas",
|
|
59
|
+
"viewer.resetCanvas",
|
|
60
|
+
"viewer.copyAiContext",
|
|
61
|
+
"viewer.copyCodeForAi",
|
|
62
|
+
"viewer.copyCliInstallation",
|
|
63
|
+
"viewer.fullscreenPreview",
|
|
64
|
+
"viewer.exitFullscreen",
|
|
65
|
+
"code.copied",
|
|
66
|
+
"code.copy",
|
|
67
|
+
"command.navigate",
|
|
68
|
+
"command.select",
|
|
69
|
+
"command.close",
|
|
70
|
+
"image.avatar",
|
|
71
|
+
"status.loading",
|
|
72
|
+
"status.loadingLabel",
|
|
73
|
+
"accessibility.callout",
|
|
74
|
+
"errors.pageNotFound",
|
|
75
|
+
"errors.pageNotFoundDescription",
|
|
76
|
+
"apiTable.property",
|
|
77
|
+
"apiTable.type",
|
|
78
|
+
"apiTable.default",
|
|
79
|
+
"apiTable.description",
|
|
80
|
+
];
|
|
81
|
+
const KEY_SET = new Set(SITE_TRANSLATION_KEYS);
|
|
82
|
+
function validateLocale(locale, value) {
|
|
83
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
84
|
+
throw new Error(`[folio] Translation file for locale "${locale}" must contain a JSON object`);
|
|
85
|
+
}
|
|
86
|
+
const entries = Object.entries(value);
|
|
87
|
+
for (const [key, translation] of entries) {
|
|
88
|
+
if (!KEY_SET.has(key))
|
|
89
|
+
throw new Error(`[folio] Unknown site translation key "${key}" in locale "${locale}"`);
|
|
90
|
+
if (typeof translation !== "string")
|
|
91
|
+
throw new Error(`[folio] Translation "${locale}.${key}" must be a string`);
|
|
92
|
+
}
|
|
93
|
+
return value;
|
|
94
|
+
}
|
|
95
|
+
export async function loadSiteTranslations(rootDir, config = {}) {
|
|
96
|
+
const defaultLocale = config.defaultLocale || "en";
|
|
97
|
+
const directory = config.directory || "locales";
|
|
98
|
+
const localeDir = path.resolve(rootDir, directory);
|
|
99
|
+
const translations = {};
|
|
100
|
+
if (await fs.pathExists(localeDir)) {
|
|
101
|
+
const files = (await fs.readdir(localeDir))
|
|
102
|
+
.filter((file) => file.endsWith(".json"))
|
|
103
|
+
.sort();
|
|
104
|
+
for (const file of files) {
|
|
105
|
+
const locale = path.basename(file, ".json");
|
|
106
|
+
translations[locale] = validateLocale(locale, await fs.readJson(path.join(localeDir, file)));
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
if (!translations[defaultLocale])
|
|
110
|
+
translations[defaultLocale] = {};
|
|
111
|
+
return { ...config, defaultLocale, directory, locale: config.locale || defaultLocale, translations };
|
|
112
|
+
}
|
|
113
|
+
export function getSiteTranslation(config, key) {
|
|
114
|
+
const locale = config?.locale || config?.defaultLocale || "en";
|
|
115
|
+
const defaultLocale = config?.defaultLocale || "en";
|
|
116
|
+
return config?.translations?.[locale]?.[key]
|
|
117
|
+
|| config?.translations?.[defaultLocale]?.[key]
|
|
118
|
+
|| key;
|
|
119
|
+
}
|
|
@@ -1,13 +1,25 @@
|
|
|
1
|
-
import type { DocsConfig } from "../types.js";
|
|
2
|
-
export
|
|
1
|
+
import type { DocsConfig, PageData } from "../types.js";
|
|
2
|
+
export declare const LOCAL_SEARCH_PROVIDER = "local";
|
|
3
|
+
export interface SearchContext {
|
|
4
|
+
query: string;
|
|
5
|
+
pages: PageData[];
|
|
6
|
+
}
|
|
7
|
+
export interface SearchAdapter {
|
|
8
|
+
name: string;
|
|
9
|
+
search: (context: SearchContext) => PageData[] | Promise<PageData[]>;
|
|
10
|
+
}
|
|
11
|
+
export type ConfiguredSearchProvider = NonNullable<DocsConfig["search"]>["provider"];
|
|
3
12
|
export interface ResolvedSearchProvider {
|
|
4
|
-
requested:
|
|
5
|
-
active:
|
|
13
|
+
requested: string;
|
|
14
|
+
active: string;
|
|
6
15
|
fallback: boolean;
|
|
16
|
+
implementation: SearchAdapter;
|
|
7
17
|
}
|
|
18
|
+
export declare const localSearchAdapter: SearchAdapter;
|
|
8
19
|
/**
|
|
9
20
|
* Resolve the configured search provider to an implementation available in
|
|
10
21
|
* the default theme. External indexes can be added later without changing
|
|
11
22
|
* the configuration contract or leaving the search dialog non-functional.
|
|
12
23
|
*/
|
|
13
24
|
export declare function resolveSearchProvider(search?: DocsConfig["search"]): ResolvedSearchProvider;
|
|
25
|
+
export declare function searchPages(provider: ResolvedSearchProvider, query: string, pages: PageData[]): PageData[] | Promise<PageData[]>;
|
package/dist/search/provider.js
CHANGED
|
@@ -1,13 +1,38 @@
|
|
|
1
|
+
export const LOCAL_SEARCH_PROVIDER = "local";
|
|
2
|
+
export const localSearchAdapter = {
|
|
3
|
+
name: LOCAL_SEARCH_PROVIDER,
|
|
4
|
+
search({ query, pages }) {
|
|
5
|
+
const normalizedQuery = query.trim().toLowerCase();
|
|
6
|
+
if (!normalizedQuery)
|
|
7
|
+
return pages;
|
|
8
|
+
return pages.filter((page) => [page.title, page.url, page.description]
|
|
9
|
+
.filter(Boolean)
|
|
10
|
+
.some((value) => value.toLowerCase().includes(normalizedQuery)));
|
|
11
|
+
},
|
|
12
|
+
};
|
|
1
13
|
/**
|
|
2
14
|
* Resolve the configured search provider to an implementation available in
|
|
3
15
|
* the default theme. External indexes can be added later without changing
|
|
4
16
|
* the configuration contract or leaving the search dialog non-functional.
|
|
5
17
|
*/
|
|
6
18
|
export function resolveSearchProvider(search) {
|
|
7
|
-
const
|
|
19
|
+
const configuredProvider = search?.provider;
|
|
20
|
+
if (configuredProvider && typeof configuredProvider !== "string") {
|
|
21
|
+
return {
|
|
22
|
+
requested: configuredProvider.name,
|
|
23
|
+
active: configuredProvider.name,
|
|
24
|
+
fallback: false,
|
|
25
|
+
implementation: configuredProvider,
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
const requested = configuredProvider?.trim() || LOCAL_SEARCH_PROVIDER;
|
|
8
29
|
return {
|
|
9
30
|
requested,
|
|
10
|
-
active:
|
|
11
|
-
fallback: requested !==
|
|
31
|
+
active: LOCAL_SEARCH_PROVIDER,
|
|
32
|
+
fallback: requested !== LOCAL_SEARCH_PROVIDER,
|
|
33
|
+
implementation: localSearchAdapter,
|
|
12
34
|
};
|
|
13
35
|
}
|
|
36
|
+
export function searchPages(provider, query, pages) {
|
|
37
|
+
return provider.implementation.search({ query, pages });
|
|
38
|
+
}
|
package/dist/server/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { type ViteDevServer } from "vite";
|
|
2
2
|
import type { DocsConfig } from "../types.js";
|
|
3
|
+
import { type FolioBuildSession } from "./plugin/lifecycle.js";
|
|
3
4
|
export interface DocsServerOptions {
|
|
4
5
|
root?: string;
|
|
5
6
|
docsDir?: string;
|
|
@@ -8,10 +9,12 @@ export interface DocsServerOptions {
|
|
|
8
9
|
open?: boolean;
|
|
9
10
|
config?: DocsConfig;
|
|
10
11
|
outDir?: string;
|
|
12
|
+
/** Shared plugin lifecycle for one production build. */
|
|
13
|
+
lifecycleSession?: FolioBuildSession;
|
|
11
14
|
}
|
|
12
15
|
export declare function getClientDir(): string;
|
|
13
16
|
export declare function createDocsServer(options?: DocsServerOptions): Promise<ViteDevServer>;
|
|
14
17
|
export declare function buildDocs(options?: DocsServerOptions): Promise<void>;
|
|
15
18
|
export declare function createDocsRequestHandler(options?: DocsServerOptions): Promise<(request: Request) => Promise<Response>>;
|
|
16
19
|
export declare function previewDocs(options?: DocsServerOptions): Promise<void>;
|
|
17
|
-
export * from "./plugin.js";
|
|
20
|
+
export * from "./plugin/index.js";
|