@nikala-ui/folio 0.14.0 → 0.15.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/README.md +16 -0
- package/dist/cli/commands/init.js +105 -51
- 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/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 +4 -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 -4
- 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 +9 -0
- package/dist/search/provider.js +4 -2
- package/dist/server/index.d.ts +4 -1
- package/dist/server/index.js +69 -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 +121 -0
- package/dist/server/seo.d.ts +4 -0
- package/dist/server/seo.js +87 -0
- package/dist/templates/env.d.ts +26 -0
- package/dist/templates/locales/en.json +77 -0
- package/dist/themes/default/components/docs-landing-body.d.ts +17 -0
- package/dist/themes/default/components/docs-landing-body.jsx +17 -0
- package/dist/themes/default/components/docs-page-body.d.ts +21 -0
- package/dist/themes/default/components/docs-page-body.jsx +36 -0
- package/dist/themes/default/components/page-actions.d.ts +15 -0
- package/dist/themes/default/components/page-actions.jsx +60 -0
- package/dist/themes/default/components/page-header.d.ts +7 -0
- package/dist/themes/default/components/page-header.jsx +3 -0
- package/dist/themes/default/components/sidebar-footer.d.ts +6 -0
- package/dist/themes/default/components/sidebar-footer.jsx +12 -0
- package/dist/themes/default/components/sidebar-header.d.ts +8 -0
- package/dist/themes/default/components/sidebar-header.jsx +17 -0
- package/dist/themes/default/components/sidebar-mobile-nav.d.ts +7 -0
- package/dist/themes/default/components/sidebar-mobile-nav.jsx +17 -0
- package/dist/themes/default/components/sidebar-promo.d.ts +8 -0
- package/dist/themes/default/components/sidebar-promo.jsx +20 -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/hooks/use-page-copy.d.ts +6 -0
- package/dist/themes/default/hooks/use-page-copy.js +26 -0
- package/dist/themes/default/layout.jsx +30 -127
- package/dist/themes/default/lib/layout-visibility.d.ts +8 -0
- package/dist/themes/default/lib/layout-visibility.js +24 -0
- package/dist/themes/default/navbar.jsx +14 -7
- package/dist/themes/default/navigation/mobile-table-of-contents.jsx +5 -3
- 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 +14 -3
- package/dist/themes/default/sidebar.jsx +21 -49
- package/dist/themes/types.d.ts +5 -0
- package/dist/types.d.ts +36 -0
- package/dist/utils/format-title.d.ts +1 -0
- package/dist/utils/format-title.js +11 -0
- package/dist/vendor/docs-src/client/page-actions.ts +69 -0
- 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 +4 -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/navigation/index.ts +2 -0
- package/dist/vendor/docs-src/navigation/repository-links.ts +43 -0
- package/dist/vendor/docs-src/navigation/sidebar-state.ts +22 -0
- package/dist/vendor/docs-src/navigation/sidebar-tree.ts +15 -0
- 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/vendor/docs-src/search/provider.ts +83 -0
- package/dist/vendor/docs-src/themes/default/components/docs-landing-body.tsx +30 -0
- package/dist/vendor/docs-src/themes/default/components/docs-page-body.tsx +53 -0
- package/dist/vendor/docs-src/themes/default/components/page-actions.tsx +105 -0
- package/dist/vendor/docs-src/themes/default/components/page-header.tsx +31 -0
- package/dist/vendor/docs-src/themes/default/components/sidebar-footer.tsx +21 -0
- package/dist/vendor/docs-src/themes/default/components/sidebar-header.tsx +30 -0
- package/dist/vendor/docs-src/themes/default/components/sidebar-mobile-nav.tsx +33 -0
- package/dist/vendor/docs-src/themes/default/components/sidebar-promo.tsx +37 -0
- package/dist/vendor/docs-src/themes/default/content/breadcrumbs.tsx +43 -0
- package/dist/vendor/docs-src/themes/default/content/pager.tsx +19 -0
- package/dist/vendor/docs-src/themes/default/content/table-of-contents.tsx +32 -0
- package/dist/vendor/docs-src/themes/default/hooks/use-page-copy.ts +29 -0
- package/dist/vendor/docs-src/themes/default/icons.ts +16 -0
- package/dist/vendor/docs-src/themes/default/index.ts +46 -0
- package/dist/vendor/docs-src/themes/default/layout.tsx +176 -0
- package/dist/vendor/docs-src/themes/default/lib/layout-visibility.ts +39 -0
- package/dist/vendor/docs-src/themes/default/navbar.tsx +130 -0
- package/dist/vendor/docs-src/themes/default/navigation/mobile-table-of-contents.tsx +61 -0
- package/dist/vendor/docs-src/themes/default/navigation/sidebar-tree.tsx +202 -0
- package/dist/vendor/docs-src/themes/default/overlays/search-dialog.tsx +106 -0
- package/dist/vendor/docs-src/themes/default/sidebar.tsx +86 -0
- package/dist/vendor/docs-src/themes/index.ts +4 -0
- package/dist/vendor/docs-src/themes/types.ts +87 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +11 -4
- package/dist/server/plugin.js +0 -521
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import { validateFolioPlugins } from "../../plugin.js";
|
|
2
|
+
import { FolioPluginHookError, } from "./lifecycle-error.js";
|
|
3
|
+
import { snapshot } from "./lifecycle-snapshot.js";
|
|
4
|
+
export { FolioPluginHookError } from "./lifecycle-error.js";
|
|
5
|
+
function routeIdentity(page) {
|
|
6
|
+
return `${page.slug}\u0000${page.url}`;
|
|
7
|
+
}
|
|
8
|
+
export class FolioPluginLifecycleManager {
|
|
9
|
+
plugins;
|
|
10
|
+
options;
|
|
11
|
+
pages;
|
|
12
|
+
constructor(options) {
|
|
13
|
+
validateFolioPlugins(options.plugins);
|
|
14
|
+
this.plugins = options.plugins ? [...options.plugins] : [];
|
|
15
|
+
this.options = options;
|
|
16
|
+
this.pages = [...(options.pages ?? [])].map((page) => snapshot(page));
|
|
17
|
+
}
|
|
18
|
+
getPages() {
|
|
19
|
+
return snapshot(this.pages);
|
|
20
|
+
}
|
|
21
|
+
async configResolved() { await this.runContextHook("configResolved"); }
|
|
22
|
+
async buildStart() { await this.runContextHook("buildStart"); }
|
|
23
|
+
async pagesGenerated(pages) {
|
|
24
|
+
let generated = pages.map((page) => snapshot(page));
|
|
25
|
+
for (const plugin of this.plugins) {
|
|
26
|
+
if (!plugin.pagesGenerated)
|
|
27
|
+
continue;
|
|
28
|
+
try {
|
|
29
|
+
const result = await plugin.pagesGenerated(snapshot(generated), this.context());
|
|
30
|
+
if (result !== undefined) {
|
|
31
|
+
if (!Array.isArray(result))
|
|
32
|
+
throw new Error("must return an array of pages");
|
|
33
|
+
generated = result.map((page) => snapshot(page));
|
|
34
|
+
}
|
|
35
|
+
this.validateGeneratedPages(generated);
|
|
36
|
+
}
|
|
37
|
+
catch (error) {
|
|
38
|
+
throw this.wrap(plugin, "pagesGenerated", error);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return snapshot(generated);
|
|
42
|
+
}
|
|
43
|
+
async pageCollected(page) {
|
|
44
|
+
const collected = snapshot(page);
|
|
45
|
+
this.pages = [...this.pages, collected];
|
|
46
|
+
for (const plugin of this.plugins) {
|
|
47
|
+
try {
|
|
48
|
+
await plugin.pageCollected?.(collected, this.context());
|
|
49
|
+
}
|
|
50
|
+
catch (error) {
|
|
51
|
+
throw this.wrap(plugin, "pageCollected", error, page);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
async pageTransformed(page) {
|
|
56
|
+
let transformed = snapshot(page);
|
|
57
|
+
for (const plugin of this.plugins) {
|
|
58
|
+
if (!plugin.pageTransformed)
|
|
59
|
+
continue;
|
|
60
|
+
const before = transformed;
|
|
61
|
+
try {
|
|
62
|
+
const result = await plugin.pageTransformed(before, this.context());
|
|
63
|
+
if (!result || typeof result !== "object")
|
|
64
|
+
throw new Error("must return a page object");
|
|
65
|
+
if (routeIdentity(result) !== routeIdentity(before)) {
|
|
66
|
+
throw new Error("must preserve the page route identity (slug and url)");
|
|
67
|
+
}
|
|
68
|
+
transformed = snapshot(result);
|
|
69
|
+
}
|
|
70
|
+
catch (error) {
|
|
71
|
+
throw this.wrap(plugin, "pageTransformed", error, page);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
const index = this.pages.findIndex((candidate) => routeIdentity(candidate) === routeIdentity(page));
|
|
75
|
+
if (index >= 0)
|
|
76
|
+
this.pages[index] = transformed;
|
|
77
|
+
return snapshot(transformed);
|
|
78
|
+
}
|
|
79
|
+
async pageActions(page) {
|
|
80
|
+
let actions = [...(page.pageActions ?? [])];
|
|
81
|
+
for (const plugin of this.plugins) {
|
|
82
|
+
if (!plugin.pageActions)
|
|
83
|
+
continue;
|
|
84
|
+
try {
|
|
85
|
+
const result = await plugin.pageActions(snapshot({ ...page, pageActions: actions }), this.context());
|
|
86
|
+
if (result === undefined)
|
|
87
|
+
continue;
|
|
88
|
+
if (!Array.isArray(result))
|
|
89
|
+
throw new Error("must return an array of page actions");
|
|
90
|
+
for (const action of result) {
|
|
91
|
+
if (!action || typeof action !== "object")
|
|
92
|
+
throw new Error("each page action must be an object");
|
|
93
|
+
if (typeof action.label !== "string" || !action.label.trim()) {
|
|
94
|
+
throw new Error("each page action must have a non-empty label");
|
|
95
|
+
}
|
|
96
|
+
if (typeof action.href !== "string" || !action.href.trim()) {
|
|
97
|
+
throw new Error("each page action must have a non-empty href");
|
|
98
|
+
}
|
|
99
|
+
if (action.external !== undefined && typeof action.external !== "boolean") {
|
|
100
|
+
throw new Error("page action external must be a boolean");
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
actions = [...actions, ...result.map((action) => ({ ...action }))];
|
|
104
|
+
}
|
|
105
|
+
catch (error) {
|
|
106
|
+
throw this.wrap(plugin, "pageActions", error, page);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
const result = snapshot({ ...page, pageActions: actions });
|
|
110
|
+
const index = this.pages.findIndex((candidate) => routeIdentity(candidate) === routeIdentity(page));
|
|
111
|
+
if (index >= 0)
|
|
112
|
+
this.pages[index] = result;
|
|
113
|
+
return result;
|
|
114
|
+
}
|
|
115
|
+
async generate() { await this.runContextHook("generate"); }
|
|
116
|
+
async buildEnd(result) {
|
|
117
|
+
for (const plugin of this.plugins) {
|
|
118
|
+
try {
|
|
119
|
+
await plugin.buildEnd?.(snapshot(result), this.context());
|
|
120
|
+
}
|
|
121
|
+
catch (error) {
|
|
122
|
+
throw this.wrap(plugin, "buildEnd", error);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
context() {
|
|
127
|
+
return snapshot({
|
|
128
|
+
config: this.options.config,
|
|
129
|
+
rootDir: this.options.rootDir,
|
|
130
|
+
contentDir: this.options.contentDir,
|
|
131
|
+
outputDir: this.options.outputDir,
|
|
132
|
+
pages: this.pages,
|
|
133
|
+
mode: this.options.mode,
|
|
134
|
+
logger: this.options.logger,
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
async runContextHook(hook) {
|
|
138
|
+
for (const plugin of this.plugins) {
|
|
139
|
+
try {
|
|
140
|
+
await plugin[hook]?.(this.context());
|
|
141
|
+
}
|
|
142
|
+
catch (error) {
|
|
143
|
+
throw this.wrap(plugin, hook, error);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
validateGeneratedPages(pages) {
|
|
148
|
+
const identities = new Set();
|
|
149
|
+
for (const page of pages) {
|
|
150
|
+
if (!page || typeof page !== "object")
|
|
151
|
+
throw new Error("each generated page must be an object");
|
|
152
|
+
if (typeof page.slug !== "string" || !page.slug.trim())
|
|
153
|
+
throw new Error("each generated page must have a slug");
|
|
154
|
+
if (typeof page.url !== "string" || !page.url.trim())
|
|
155
|
+
throw new Error("each generated page must have a url");
|
|
156
|
+
const identity = routeIdentity(page);
|
|
157
|
+
if (identities.has(identity))
|
|
158
|
+
throw new Error(`duplicate generated page route "${page.url}"`);
|
|
159
|
+
identities.add(identity);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
wrap(plugin, hook, error, page) {
|
|
163
|
+
return error instanceof FolioPluginHookError
|
|
164
|
+
? error
|
|
165
|
+
: new FolioPluginHookError(plugin.name, hook, error, this.options.mode, page);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
export function createFolioPluginLifecycleManager(options) {
|
|
169
|
+
return new FolioPluginLifecycleManager(options);
|
|
170
|
+
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { PageData, SidebarItem } from "../types.js";
|
|
2
|
-
|
|
2
|
+
import { flattenSidebarItems } from "../navigation/sidebar-tree.js";
|
|
3
|
+
export { flattenSidebarItems };
|
|
3
4
|
export declare function formatGroupName(segment: string): string;
|
|
4
5
|
export declare function buildSidebarTree(pages: PageData[], directories?: string[]): SidebarItem[];
|
|
5
6
|
/**
|
|
@@ -7,7 +8,7 @@ export declare function buildSidebarTree(pages: PageData[], directories?: string
|
|
|
7
8
|
* link against the scanned documentation routes.
|
|
8
9
|
*/
|
|
9
10
|
export declare function buildConfiguredSidebarTree(items: SidebarItem[], pages: PageData[]): SidebarItem[];
|
|
10
|
-
export declare function buildPagination(pages: PageData[], currentUrl: string): {
|
|
11
|
+
export declare function buildPagination(pages: PageData[], currentUrl: string, sidebarTree?: SidebarItem[]): {
|
|
11
12
|
prev?: {
|
|
12
13
|
title: string;
|
|
13
14
|
href: string;
|
package/dist/core/route-tree.js
CHANGED
|
@@ -1,24 +1,53 @@
|
|
|
1
|
-
import { formatTitleFromFilename } from "
|
|
1
|
+
import { formatTitleFromFilename } from "../utils/format-title.js";
|
|
2
2
|
import { flattenSidebarItems } from "../navigation/sidebar-tree.js";
|
|
3
|
-
export { flattenSidebarItems }
|
|
3
|
+
export { flattenSidebarItems };
|
|
4
4
|
export function formatGroupName(segment) {
|
|
5
5
|
return segment
|
|
6
6
|
.split(/[-_]/)
|
|
7
7
|
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
|
|
8
8
|
.join(" ");
|
|
9
9
|
}
|
|
10
|
+
function navigationSegments(page) {
|
|
11
|
+
const segments = page.url.slice(1).split("/").filter(Boolean);
|
|
12
|
+
const locale = typeof page.frontmatter.locale === "string" ? page.frontmatter.locale : undefined;
|
|
13
|
+
return locale && segments[0] === locale ? segments.slice(1) : segments;
|
|
14
|
+
}
|
|
15
|
+
function navigationDirectorySegments(directory, locales) {
|
|
16
|
+
const segments = directory.split("/").filter(Boolean);
|
|
17
|
+
return locales.has(segments[0]) ? segments.slice(1) : segments;
|
|
18
|
+
}
|
|
19
|
+
function translationKey(page) {
|
|
20
|
+
const locale = typeof page.frontmatter.locale === "string" ? page.frontmatter.locale : undefined;
|
|
21
|
+
const sourcePath = page.sourcePath || page.url;
|
|
22
|
+
const prefix = locale ? `${locale}/` : "";
|
|
23
|
+
return locale && sourcePath.startsWith(prefix) ? sourcePath.slice(prefix.length) : sourcePath;
|
|
24
|
+
}
|
|
25
|
+
function uniqueTranslationPages(pages) {
|
|
26
|
+
const seen = new Set();
|
|
27
|
+
return pages.filter((page) => {
|
|
28
|
+
const key = translationKey(page);
|
|
29
|
+
if (seen.has(key))
|
|
30
|
+
return false;
|
|
31
|
+
seen.add(key);
|
|
32
|
+
return true;
|
|
33
|
+
});
|
|
34
|
+
}
|
|
10
35
|
export function buildSidebarTree(pages, directories = []) {
|
|
11
36
|
const rootPages = [];
|
|
12
37
|
const categoryMap = new Map();
|
|
38
|
+
const locales = new Set(pages
|
|
39
|
+
.map((page) => typeof page.frontmatter.locale === "string" ? page.frontmatter.locale : undefined)
|
|
40
|
+
.filter((locale) => Boolean(locale)));
|
|
41
|
+
pages = uniqueTranslationPages(pages);
|
|
13
42
|
for (const directory of directories) {
|
|
14
|
-
const category = directory
|
|
43
|
+
const category = navigationDirectorySegments(directory, locales)[0];
|
|
15
44
|
if (category && !categoryMap.has(category))
|
|
16
45
|
categoryMap.set(category, []);
|
|
17
46
|
}
|
|
18
47
|
// Derive categories from pages as well. Tests and programmatic consumers
|
|
19
48
|
// may provide pages without a separately scanned directory list.
|
|
20
49
|
for (const page of pages) {
|
|
21
|
-
const segments = page
|
|
50
|
+
const segments = navigationSegments(page);
|
|
22
51
|
if (segments.length > 1 && !categoryMap.has(segments[0])) {
|
|
23
52
|
categoryMap.set(segments[0], []);
|
|
24
53
|
}
|
|
@@ -29,7 +58,7 @@ export function buildSidebarTree(pages, directories = []) {
|
|
|
29
58
|
// Keep it out of the sidebar so it is not duplicated as "Introduction".
|
|
30
59
|
continue;
|
|
31
60
|
}
|
|
32
|
-
const segments = page
|
|
61
|
+
const segments = navigationSegments(page);
|
|
33
62
|
const isCategoryIndex = segments.length === 1 && categoryMap.has(segments[0]);
|
|
34
63
|
// A directory index is the overview page for its category. Keep it in
|
|
35
64
|
// the category instead of rendering it as a second root-level item.
|
|
@@ -85,7 +114,7 @@ export function buildSidebarTree(pages, directories = []) {
|
|
|
85
114
|
return a.cat.localeCompare(b.cat);
|
|
86
115
|
});
|
|
87
116
|
for (const { cat, catPages } of categories) {
|
|
88
|
-
const categoryIndex = catPages.find((page) => page.
|
|
117
|
+
const categoryIndex = catPages.find((page) => navigationSegments(page).join("/") === cat);
|
|
89
118
|
const group = {
|
|
90
119
|
title: formatGroupName(cat),
|
|
91
120
|
href: categoryIndex?.url,
|
|
@@ -102,7 +131,7 @@ export function buildSidebarTree(pages, directories = []) {
|
|
|
102
131
|
return a.title.localeCompare(b.title);
|
|
103
132
|
});
|
|
104
133
|
for (const page of catPages) {
|
|
105
|
-
const segments = page
|
|
134
|
+
const segments = navigationSegments(page);
|
|
106
135
|
const isCategoryIndex = segments.length === 1;
|
|
107
136
|
if (isCategoryIndex) {
|
|
108
137
|
continue;
|
|
@@ -160,11 +189,12 @@ export function buildConfiguredSidebarTree(items, pages) {
|
|
|
160
189
|
});
|
|
161
190
|
return visit(items, "");
|
|
162
191
|
}
|
|
163
|
-
export function buildPagination(pages, currentUrl) {
|
|
192
|
+
export function buildPagination(pages, currentUrl, sidebarTree) {
|
|
164
193
|
const currentPage = pages.find((p) => p.url === currentUrl);
|
|
165
194
|
const navigationPages = pages.filter((page) => page.url !== "/");
|
|
166
|
-
const sidebar = buildSidebarTree(navigationPages);
|
|
167
|
-
const
|
|
195
|
+
const sidebar = sidebarTree || buildSidebarTree(navigationPages);
|
|
196
|
+
const visibleUrls = new Set(pages.filter((page) => page.frontmatter?.hidden !== true).map((page) => page.url));
|
|
197
|
+
const flattened = flattenSidebarItems(sidebar).filter((item) => visibleUrls.has(item.href));
|
|
168
198
|
const currentIndex = flattened.findIndex((item) => item.href === currentUrl);
|
|
169
199
|
if (currentIndex === -1) {
|
|
170
200
|
return {};
|
package/dist/index.d.ts
CHANGED
|
@@ -1,14 +1,17 @@
|
|
|
1
1
|
export * from "./types.js";
|
|
2
|
-
export
|
|
2
|
+
export * from "./themes/types.js";
|
|
3
|
+
export type { FolioBuildResult, FolioPage, FolioPlugin, FolioPluginConfig, FolioPluginContext, FolioPluginFactory, FolioPluginLogger, } from "./plugin.js";
|
|
4
|
+
export { createFolioPlugin, defineFolioPlugin, validateFolioPlugin, validateFolioPlugins, } from "./plugin.js";
|
|
5
|
+
export { createFolioPluginLifecycleManager, FolioPluginHookError, FolioPluginLifecycleManager, } from "./core/plugin/index.js";
|
|
6
|
+
export type { FolioPluginHookMetadata, FolioPluginLifecycleOptions, } from "./core/plugin/index.js";
|
|
7
|
+
export type { SearchAdapter, SearchAdapterRuntime, SearchContext } from "./search/provider.js";
|
|
3
8
|
export * from "./navigation/index.js";
|
|
4
9
|
export * from "./config.js";
|
|
5
10
|
export * from "./core/content-scanner.js";
|
|
6
11
|
export * from "./core/route-tree.js";
|
|
7
12
|
export * from "./mdx/compiler.js";
|
|
8
13
|
export * from "./mdx/highlighter.js";
|
|
9
|
-
export * from "./components/mdx-components.js";
|
|
10
|
-
export * from "./themes/index.js";
|
|
11
14
|
export * from "./server/index.js";
|
|
12
15
|
export * from "./search/provider.js";
|
|
13
|
-
import { nikalaDocsPlugin } from "./server/plugin.js";
|
|
16
|
+
import { nikalaDocsPlugin } from "./server/plugin/index.js";
|
|
14
17
|
export declare const nikalaDocs: typeof nikalaDocsPlugin;
|
package/dist/index.js
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
// packages/docs/src/index.ts
|
|
2
2
|
export * from "./types.js";
|
|
3
|
+
export * from "./themes/types.js";
|
|
4
|
+
export { createFolioPlugin, defineFolioPlugin, validateFolioPlugin, validateFolioPlugins, } from "./plugin.js";
|
|
5
|
+
export { createFolioPluginLifecycleManager, FolioPluginHookError, FolioPluginLifecycleManager, } from "./core/plugin/index.js";
|
|
3
6
|
export * from "./navigation/index.js";
|
|
4
7
|
export * from "./config.js";
|
|
5
8
|
export * from "./core/content-scanner.js";
|
|
6
9
|
export * from "./core/route-tree.js";
|
|
7
10
|
export * from "./mdx/compiler.js";
|
|
8
11
|
export * from "./mdx/highlighter.js";
|
|
9
|
-
export * from "./components/mdx-components.js";
|
|
10
|
-
export * from "./themes/index.js";
|
|
11
12
|
export * from "./server/index.js";
|
|
12
13
|
export * from "./search/provider.js";
|
|
13
|
-
import { nikalaDocsPlugin } from "./server/plugin.js";
|
|
14
|
+
import { nikalaDocsPlugin } from "./server/plugin/index.js";
|
|
14
15
|
export const nikalaDocs = nikalaDocsPlugin;
|
package/dist/plugin.d.ts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type { DocsConfig, DocsPageAction, PageData } from "./types.js";
|
|
2
|
+
export type FolioPage = PageData;
|
|
3
|
+
export interface FolioPluginLogger {
|
|
4
|
+
debug(message: string, ...args: unknown[]): void;
|
|
5
|
+
info(message: string, ...args: unknown[]): void;
|
|
6
|
+
warn(message: string, ...args: unknown[]): void;
|
|
7
|
+
error(message: string, ...args: unknown[]): void;
|
|
8
|
+
}
|
|
9
|
+
export interface FolioPluginContext {
|
|
10
|
+
/** Resolved consumer configuration, exposed as an immutable snapshot. */
|
|
11
|
+
readonly config: DocsConfig;
|
|
12
|
+
/** Absolute project root used by the integrating build tool. */
|
|
13
|
+
readonly rootDir: string;
|
|
14
|
+
/** Resolved content directory used by the integrating build tool. */
|
|
15
|
+
readonly contentDir: string;
|
|
16
|
+
/** Production output directory, when the integrating build has one. */
|
|
17
|
+
readonly outputDir?: string;
|
|
18
|
+
/** Pages collected or transformed before the current hook invocation. */
|
|
19
|
+
readonly pages: readonly FolioPage[];
|
|
20
|
+
/** Build session mode selected by the integrating build tool. */
|
|
21
|
+
readonly mode: "development" | "production";
|
|
22
|
+
/** Logger owned by the integrating build tool. */
|
|
23
|
+
readonly logger: FolioPluginLogger;
|
|
24
|
+
}
|
|
25
|
+
export interface FolioBuildResult {
|
|
26
|
+
/** Whether the integrating build completed successfully. */
|
|
27
|
+
success: boolean;
|
|
28
|
+
/** Output directory selected by the integrating build tool. */
|
|
29
|
+
outputDir: string;
|
|
30
|
+
/** Final page catalog exposed as an immutable snapshot to buildEnd. */
|
|
31
|
+
pages: readonly FolioPage[];
|
|
32
|
+
}
|
|
33
|
+
export interface FolioPlugin {
|
|
34
|
+
name: string;
|
|
35
|
+
/** Optional configuration defaults contributed by this plugin. */
|
|
36
|
+
config?: Partial<DocsConfig>;
|
|
37
|
+
/** Runs after configuration has been resolved and before content scanning. */
|
|
38
|
+
configResolved?: (context: FolioPluginContext) => void | Promise<void>;
|
|
39
|
+
/** Runs once before the current build or development scan starts. */
|
|
40
|
+
buildStart?: (context: FolioPluginContext) => void | Promise<void>;
|
|
41
|
+
/** Observes one scanned page before route and navigation structures are built. */
|
|
42
|
+
pageCollected?: (page: FolioPage, context: FolioPluginContext) => void | Promise<void>;
|
|
43
|
+
/** Expands the scanned catalog before pages are collected and routed. */
|
|
44
|
+
pagesGenerated?: (pages: readonly FolioPage[], context: FolioPluginContext) => readonly FolioPage[] | void | Promise<readonly FolioPage[] | void>;
|
|
45
|
+
/** Transforms page metadata while preserving its slug and URL. */
|
|
46
|
+
pageTransformed?: (page: FolioPage, context: FolioPluginContext) => FolioPage | Promise<FolioPage>;
|
|
47
|
+
/** Adds serializable links to the current page's heading action area. */
|
|
48
|
+
pageActions?: (page: FolioPage, context: FolioPluginContext) => readonly DocsPageAction[] | void | Promise<readonly DocsPageAction[] | void>;
|
|
49
|
+
/** Runs after artifacts have been generated by the integrating build tool. */
|
|
50
|
+
generate?: (context: FolioPluginContext) => void | Promise<void>;
|
|
51
|
+
/** Observes the final build result as an immutable snapshot. */
|
|
52
|
+
buildEnd?: (result: FolioBuildResult, context: FolioPluginContext) => void | Promise<void>;
|
|
53
|
+
}
|
|
54
|
+
export type FolioPluginFactory<Options = void> = [Options] extends [void] ? () => FolioPlugin : (options: Options) => FolioPlugin;
|
|
55
|
+
export type FolioPluginConfig = readonly FolioPlugin[];
|
|
56
|
+
export declare function validateFolioPlugin(plugin: unknown, label?: string): asserts plugin is FolioPlugin;
|
|
57
|
+
export declare function validateFolioPlugins(plugins: unknown): asserts plugins is FolioPluginConfig | undefined;
|
|
58
|
+
export declare function defineFolioPlugin(plugin: FolioPlugin): FolioPlugin;
|
|
59
|
+
export declare function createFolioPlugin(factory: () => FolioPlugin): FolioPlugin;
|
|
60
|
+
export declare function createFolioPlugin<Options>(factory: FolioPluginFactory<Options>, options: Options): FolioPlugin;
|
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 {};
|