@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.
Files changed (111) hide show
  1. package/README.md +16 -0
  2. package/dist/cli/commands/init.js +37 -2
  3. package/dist/client/app.jsx +6 -3
  4. package/dist/client/components/docs-layout-shell.d.ts +1 -0
  5. package/dist/client/components/docs-layout-shell.jsx +1 -1
  6. package/dist/client/components/docs-page-content.jsx +9 -5
  7. package/dist/client/navigation/page-navigation.js +10 -27
  8. package/dist/client/public/algolia-logo.svg +15 -0
  9. package/dist/client/public/favicon.ico +0 -0
  10. package/dist/client/public/logo.svg +11 -0
  11. package/dist/client/routing/use-docs-router.d.ts +1 -0
  12. package/dist/client/routing/use-docs-router.js +12 -3
  13. package/dist/client/style.css +139 -55
  14. package/dist/components/mdx-components.jsx +4 -1
  15. package/dist/components/ui/api-table.jsx +6 -4
  16. package/dist/components/ui/code-block.jsx +6 -4
  17. package/dist/components/ui/command.jsx +9 -5
  18. package/dist/components/ui/list.jsx +3 -1
  19. package/dist/components/ui/logo.d.ts +4 -1
  20. package/dist/components/ui/logo.jsx +8 -22
  21. package/dist/components/ui/package-manager-tabs.jsx +12 -2
  22. package/dist/components/ui/pager.jsx +4 -2
  23. package/dist/components/ui/section-heading.jsx +1 -1
  24. package/dist/components/ui/sidebar.jsx +6 -3
  25. package/dist/components/ui/spinner.jsx +3 -1
  26. package/dist/components/ui/theme-toggle.jsx +14 -12
  27. package/dist/config.d.ts +1 -0
  28. package/dist/config.js +84 -29
  29. package/dist/core/content-scanner.d.ts +2 -1
  30. package/dist/core/content-scanner.js +2 -9
  31. package/dist/core/plugin/index.d.ts +2 -0
  32. package/dist/core/plugin/index.js +1 -0
  33. package/dist/core/plugin/lifecycle-error.d.ts +19 -0
  34. package/dist/core/plugin/lifecycle-error.js +28 -0
  35. package/dist/core/plugin/lifecycle-snapshot.d.ts +1 -0
  36. package/dist/core/plugin/lifecycle-snapshot.js +156 -0
  37. package/dist/core/plugin/lifecycle.d.ts +34 -0
  38. package/dist/core/plugin/lifecycle.js +170 -0
  39. package/dist/core/route-tree.d.ts +3 -2
  40. package/dist/core/route-tree.js +40 -10
  41. package/dist/index.d.ts +7 -3
  42. package/dist/index.js +4 -3
  43. package/dist/plugin.d.ts +60 -0
  44. package/dist/plugin.js +51 -0
  45. package/dist/plugins/i18n/components/site-language-switcher.d.ts +10 -0
  46. package/dist/plugins/i18n/components/site-language-switcher.jsx +39 -0
  47. package/dist/plugins/i18n/constants.d.ts +1 -0
  48. package/dist/plugins/i18n/constants.js +1 -0
  49. package/dist/plugins/i18n/index.d.ts +11 -0
  50. package/dist/plugins/i18n/index.js +89 -0
  51. package/dist/plugins/i18n/navigation.d.ts +5 -0
  52. package/dist/plugins/i18n/navigation.js +33 -0
  53. package/dist/plugins/i18n/runtime.d.ts +19 -0
  54. package/dist/plugins/i18n/runtime.jsx +65 -0
  55. package/dist/plugins/i18n/site-locale.d.ts +12 -0
  56. package/dist/plugins/i18n/site-locale.js +119 -0
  57. package/dist/search/provider.d.ts +16 -4
  58. package/dist/search/provider.js +28 -3
  59. package/dist/server/index.d.ts +4 -1
  60. package/dist/server/index.js +73 -33
  61. package/dist/server/module-serialization.d.ts +6 -0
  62. package/dist/server/module-serialization.js +26 -0
  63. package/dist/server/plugin/components.d.ts +7 -0
  64. package/dist/server/plugin/components.js +82 -0
  65. package/dist/server/plugin/constants.d.ts +17 -0
  66. package/dist/server/plugin/constants.js +21 -0
  67. package/dist/server/{plugin.d.ts → plugin/index.d.ts} +3 -1
  68. package/dist/server/plugin/index.js +166 -0
  69. package/dist/server/plugin/lifecycle.d.ts +32 -0
  70. package/dist/server/plugin/lifecycle.js +110 -0
  71. package/dist/server/plugin/styles.d.ts +3 -0
  72. package/dist/server/plugin/styles.js +22 -0
  73. package/dist/server/plugin/virtual-modules.d.ts +11 -0
  74. package/dist/server/plugin/virtual-modules.js +88 -0
  75. package/dist/server/seo.d.ts +4 -0
  76. package/dist/server/seo.js +87 -0
  77. package/dist/themes/default/content/breadcrumbs.jsx +5 -2
  78. package/dist/themes/default/content/table-of-contents.jsx +3 -1
  79. package/dist/themes/default/layout.jsx +42 -14
  80. package/dist/themes/default/navbar.jsx +14 -7
  81. package/dist/themes/default/navigation/mobile-table-of-contents.jsx +3 -1
  82. package/dist/themes/default/navigation/sidebar-tree.d.ts +2 -0
  83. package/dist/themes/default/navigation/sidebar-tree.jsx +11 -2
  84. package/dist/themes/default/overlays/search-dialog.jsx +37 -11
  85. package/dist/themes/default/sidebar.jsx +7 -5
  86. package/dist/themes/types.d.ts +7 -2
  87. package/dist/types.d.ts +38 -1
  88. package/dist/utils/format-title.d.ts +1 -0
  89. package/dist/utils/format-title.js +11 -0
  90. package/dist/vendor/docs-src/components/mdx-components.tsx +4 -1
  91. package/dist/vendor/docs-src/components/ui/api-table.tsx +6 -4
  92. package/dist/vendor/docs-src/components/ui/code-block.tsx +6 -4
  93. package/dist/vendor/docs-src/components/ui/command.tsx +9 -5
  94. package/dist/vendor/docs-src/components/ui/list.tsx +4 -2
  95. package/dist/vendor/docs-src/components/ui/logo.tsx +19 -48
  96. package/dist/vendor/docs-src/components/ui/package-manager-tabs.tsx +12 -2
  97. package/dist/vendor/docs-src/components/ui/pager.tsx +4 -2
  98. package/dist/vendor/docs-src/components/ui/section-heading.tsx +5 -1
  99. package/dist/vendor/docs-src/components/ui/sidebar.tsx +7 -4
  100. package/dist/vendor/docs-src/components/ui/spinner.tsx +3 -1
  101. package/dist/vendor/docs-src/components/ui/theme-toggle.tsx +15 -13
  102. package/dist/vendor/docs-src/plugins/i18n/components/site-language-switcher.tsx +56 -0
  103. package/dist/vendor/docs-src/plugins/i18n/constants.ts +1 -0
  104. package/dist/vendor/docs-src/plugins/i18n/index.ts +110 -0
  105. package/dist/vendor/docs-src/plugins/i18n/navigation.ts +49 -0
  106. package/dist/vendor/docs-src/plugins/i18n/runtime.tsx +91 -0
  107. package/dist/vendor/docs-src/plugins/i18n/site-locale.ts +140 -0
  108. package/dist/version.d.ts +1 -1
  109. package/dist/version.js +1 -1
  110. package/package.json +19 -4
  111. package/dist/server/plugin.js +0 -521
@@ -2,6 +2,8 @@
2
2
  import path from "node:path";
3
3
  import fs from "fs-extra";
4
4
  import matter from "gray-matter";
5
+ import { formatTitleFromFilename } from "../utils/format-title.js";
6
+ export { formatTitleFromFilename };
5
7
  function isWhitespace(character) {
6
8
  if (!character)
7
9
  return false;
@@ -142,15 +144,6 @@ export function filePathToUrl(relativeFilePath) {
142
144
  }
143
145
  return cleanPath.startsWith("/") ? cleanPath : `/${cleanPath}`;
144
146
  }
145
- export function formatTitleFromFilename(filename) {
146
- const base = path.basename(filename, path.extname(filename));
147
- if (base === "index")
148
- return "Overview";
149
- return base
150
- .split(/[-_]/)
151
- .map((word) => word.charAt(0).toUpperCase() + word.slice(1))
152
- .join(" ");
153
- }
154
147
  export async function parseMdxFile(filePath, contentDir) {
155
148
  const raw = await fs.readFile(filePath, "utf-8");
156
149
  const parsed = matter(raw);
@@ -0,0 +1,2 @@
1
+ export { createFolioPluginLifecycleManager, FolioPluginHookError, FolioPluginLifecycleManager, } from "./lifecycle.js";
2
+ export type { FolioPluginHookMetadata, FolioPluginLifecycleOptions, LifecycleHook, } from "./lifecycle.js";
@@ -0,0 +1 @@
1
+ export { createFolioPluginLifecycleManager, FolioPluginHookError, FolioPluginLifecycleManager, } from "./lifecycle.js";
@@ -0,0 +1,19 @@
1
+ import type { FolioPage, FolioPluginContext } from "../../plugin.js";
2
+ export type LifecycleHook = "configResolved" | "buildStart" | "pagesGenerated" | "pageCollected" | "pageTransformed" | "pageActions" | "generate" | "buildEnd";
3
+ export interface FolioPluginHookMetadata {
4
+ pluginName: string;
5
+ hook: LifecycleHook;
6
+ mode: FolioPluginContext["mode"];
7
+ pageRoute?: string;
8
+ sourcePath?: string;
9
+ }
10
+ export declare class FolioPluginHookError extends Error {
11
+ readonly pluginName: string;
12
+ readonly hook: LifecycleHook;
13
+ readonly mode: FolioPluginContext["mode"];
14
+ readonly pageRoute?: string;
15
+ readonly sourcePath?: string;
16
+ readonly metadata: FolioPluginHookMetadata;
17
+ readonly cause: unknown;
18
+ constructor(pluginName: string, hook: LifecycleHook, cause: unknown, mode?: FolioPluginContext["mode"], page?: FolioPage);
19
+ }
@@ -0,0 +1,28 @@
1
+ export class FolioPluginHookError extends Error {
2
+ pluginName;
3
+ hook;
4
+ mode;
5
+ pageRoute;
6
+ sourcePath;
7
+ metadata;
8
+ cause;
9
+ constructor(pluginName, hook, cause, mode = "production", page) {
10
+ const detail = cause instanceof Error ? cause.message : String(cause);
11
+ const pageRoute = page?.url;
12
+ const sourcePath = page?.sourcePath ?? page?.filePath;
13
+ const location = [
14
+ pageRoute && `route "${pageRoute}"`,
15
+ sourcePath && `source "${sourcePath}"`,
16
+ `mode "${mode}"`,
17
+ ].join(", ");
18
+ super(`[folio] Plugin "${pluginName}" hook "${hook}" failed (${location}): ${detail}`, { cause });
19
+ this.name = "FolioPluginHookError";
20
+ this.pluginName = pluginName;
21
+ this.hook = hook;
22
+ this.mode = mode;
23
+ this.pageRoute = pageRoute;
24
+ this.sourcePath = sourcePath;
25
+ this.metadata = { pluginName, hook, mode, pageRoute, sourcePath };
26
+ this.cause = cause;
27
+ }
28
+ }
@@ -0,0 +1 @@
1
+ export declare function snapshot<T>(value: T): Readonly<T>;
@@ -0,0 +1,156 @@
1
+ const IMMUTABLE_MESSAGE = "Cannot mutate an immutable plugin snapshot";
2
+ class ImmutableMap extends Map {
3
+ constructor(entries) {
4
+ super();
5
+ for (const [key, value] of entries)
6
+ Map.prototype.set.call(this, key, value);
7
+ }
8
+ set() { throw new TypeError(IMMUTABLE_MESSAGE); }
9
+ delete() { throw new TypeError(IMMUTABLE_MESSAGE); }
10
+ clear() { throw new TypeError(IMMUTABLE_MESSAGE); }
11
+ }
12
+ class ImmutableSet extends Set {
13
+ constructor(values) {
14
+ super();
15
+ for (const value of values)
16
+ Set.prototype.add.call(this, value);
17
+ }
18
+ add() { throw new TypeError(IMMUTABLE_MESSAGE); }
19
+ delete() { throw new TypeError(IMMUTABLE_MESSAGE); }
20
+ clear() { throw new TypeError(IMMUTABLE_MESSAGE); }
21
+ }
22
+ function isPlainObject(value) {
23
+ const prototype = Object.getPrototypeOf(value);
24
+ return prototype === Object.prototype || prototype === null;
25
+ }
26
+ function immutableObject(value, seen) {
27
+ if (seen.has(value))
28
+ return seen.get(value);
29
+ let proxy;
30
+ proxy = new Proxy(value, {
31
+ get(target, property) {
32
+ const result = Reflect.get(target, property, target);
33
+ if (typeof result === "function")
34
+ return result.bind(proxy);
35
+ return clone(result, seen);
36
+ },
37
+ set() { throw new TypeError(IMMUTABLE_MESSAGE); },
38
+ deleteProperty() { throw new TypeError(IMMUTABLE_MESSAGE); },
39
+ defineProperty() { throw new TypeError(IMMUTABLE_MESSAGE); },
40
+ });
41
+ seen.set(value, proxy);
42
+ return proxy;
43
+ }
44
+ const dateMutators = new Set([
45
+ "setDate", "setFullYear", "setHours", "setMilliseconds", "setMinutes", "setMonth",
46
+ "setSeconds", "setTime", "setUTCDate", "setUTCFullYear", "setUTCHours",
47
+ "setUTCMilliseconds", "setUTCMinutes", "setUTCMonth", "setUTCSeconds", "setYear",
48
+ ]);
49
+ function immutableDate(value, seen) {
50
+ const target = new Date(value.getTime());
51
+ const proxy = new Proxy(target, {
52
+ get(current, property) {
53
+ const result = Reflect.get(current, property, current);
54
+ if (typeof result !== "function")
55
+ return clone(result, seen);
56
+ if (typeof property === "string" && dateMutators.has(property)) {
57
+ return () => { throw new TypeError(IMMUTABLE_MESSAGE); };
58
+ }
59
+ return result.bind(current);
60
+ },
61
+ set() { throw new TypeError(IMMUTABLE_MESSAGE); },
62
+ deleteProperty() { throw new TypeError(IMMUTABLE_MESSAGE); },
63
+ defineProperty() { throw new TypeError(IMMUTABLE_MESSAGE); },
64
+ });
65
+ seen.set(value, proxy);
66
+ Object.freeze(target);
67
+ return proxy;
68
+ }
69
+ function immutableRegExp(value, seen) {
70
+ const target = new RegExp(value.source, value.flags);
71
+ target.lastIndex = value.lastIndex;
72
+ const proxy = new Proxy(target, {
73
+ get(current, property) {
74
+ const result = Reflect.get(current, property, current);
75
+ if (typeof result !== "function")
76
+ return clone(result, seen);
77
+ if (property === "compile")
78
+ return () => { throw new TypeError(IMMUTABLE_MESSAGE); };
79
+ return (...args) => {
80
+ const working = new RegExp(current.source, current.flags);
81
+ working.lastIndex = current.lastIndex;
82
+ return Reflect.apply(result, working, args);
83
+ };
84
+ },
85
+ set() { throw new TypeError(IMMUTABLE_MESSAGE); },
86
+ deleteProperty() { throw new TypeError(IMMUTABLE_MESSAGE); },
87
+ defineProperty() { throw new TypeError(IMMUTABLE_MESSAGE); },
88
+ });
89
+ seen.set(value, proxy);
90
+ Object.freeze(target);
91
+ return proxy;
92
+ }
93
+ function clone(value, seen) {
94
+ if (value === null || typeof value !== "object")
95
+ return value;
96
+ if (seen.has(value))
97
+ return seen.get(value);
98
+ if (value instanceof Date)
99
+ return immutableDate(value, seen);
100
+ if (value instanceof RegExp)
101
+ return immutableRegExp(value, seen);
102
+ if (value instanceof Map) {
103
+ const copy = new ImmutableMap([]);
104
+ seen.set(value, copy);
105
+ for (const [key, child] of value) {
106
+ Map.prototype.set.call(copy, clone(key, seen), clone(child, seen));
107
+ }
108
+ return copy;
109
+ }
110
+ if (value instanceof Set) {
111
+ const copy = new ImmutableSet([]);
112
+ seen.set(value, copy);
113
+ for (const child of value)
114
+ Set.prototype.add.call(copy, clone(child, seen));
115
+ return copy;
116
+ }
117
+ if (!Array.isArray(value) && !isPlainObject(value))
118
+ return immutableObject(value, seen);
119
+ const copy = Array.isArray(value) ? [] : Object.create(Object.getPrototypeOf(value));
120
+ seen.set(value, copy);
121
+ for (const key of Reflect.ownKeys(value)) {
122
+ const descriptor = Object.getOwnPropertyDescriptor(value, key);
123
+ if (descriptor && "value" in descriptor) {
124
+ Object.defineProperty(copy, key, { ...descriptor, value: clone(descriptor.value, seen) });
125
+ }
126
+ }
127
+ return copy;
128
+ }
129
+ function freeze(value, seen = new WeakSet()) {
130
+ if (value === null || typeof value !== "object" || seen.has(value))
131
+ return value;
132
+ if (!Array.isArray(value) && !isPlainObject(value) && !(value instanceof Map) && !(value instanceof Set))
133
+ return value;
134
+ seen.add(value);
135
+ if (value instanceof Map) {
136
+ for (const [key, child] of value) {
137
+ freeze(key, seen);
138
+ freeze(child, seen);
139
+ }
140
+ }
141
+ else if (value instanceof Set) {
142
+ for (const child of value)
143
+ freeze(child, seen);
144
+ }
145
+ else {
146
+ for (const key of Reflect.ownKeys(value)) {
147
+ const descriptor = Object.getOwnPropertyDescriptor(value, key);
148
+ if (descriptor && "value" in descriptor)
149
+ freeze(descriptor.value, seen);
150
+ }
151
+ }
152
+ return Object.freeze(value);
153
+ }
154
+ export function snapshot(value) {
155
+ return freeze(clone(value, new WeakMap()));
156
+ }
@@ -0,0 +1,34 @@
1
+ import type { FolioBuildResult, FolioPage, FolioPlugin, FolioPluginContext, FolioPluginLogger } from "../../plugin.js";
2
+ import type { DocsConfig } from "../../types.js";
3
+ export type { FolioPluginHookMetadata, LifecycleHook } from "./lifecycle-error.js";
4
+ export { FolioPluginHookError } from "./lifecycle-error.js";
5
+ export interface FolioPluginLifecycleOptions {
6
+ plugins?: readonly FolioPlugin[];
7
+ config: DocsConfig;
8
+ rootDir: string;
9
+ contentDir: string;
10
+ outputDir?: string;
11
+ mode: FolioPluginContext["mode"];
12
+ logger: FolioPluginLogger;
13
+ pages?: readonly FolioPage[];
14
+ }
15
+ export declare class FolioPluginLifecycleManager {
16
+ private readonly plugins;
17
+ private readonly options;
18
+ private pages;
19
+ constructor(options: FolioPluginLifecycleOptions);
20
+ getPages(): readonly FolioPage[];
21
+ configResolved(): Promise<void>;
22
+ buildStart(): Promise<void>;
23
+ pagesGenerated(pages: readonly FolioPage[]): Promise<readonly FolioPage[]>;
24
+ pageCollected(page: FolioPage): Promise<void>;
25
+ pageTransformed(page: FolioPage): Promise<FolioPage>;
26
+ pageActions(page: FolioPage): Promise<FolioPage>;
27
+ generate(): Promise<void>;
28
+ buildEnd(result: FolioBuildResult): Promise<void>;
29
+ private context;
30
+ private runContextHook;
31
+ private validateGeneratedPages;
32
+ private wrap;
33
+ }
34
+ export declare function createFolioPluginLifecycleManager(options: FolioPluginLifecycleOptions): FolioPluginLifecycleManager;
@@ -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
- export { flattenSidebarItems } from "../navigation/sidebar-tree.js";
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;
@@ -1,24 +1,53 @@
1
- import { formatTitleFromFilename } from "./content-scanner.js";
1
+ import { formatTitleFromFilename } from "../utils/format-title.js";
2
2
  import { flattenSidebarItems } from "../navigation/sidebar-tree.js";
3
- export { flattenSidebarItems } from "../navigation/sidebar-tree.js";
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.split("/")[0];
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.url.slice(1).split("/");
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.url.slice(1).split("/");
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.url === `/${cat}`);
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.url.slice(1).split("/");
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 flattened = flattenSidebarItems(sidebar);
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,12 +1,16 @@
1
1
  export * from "./types.js";
2
+ export type { FolioBuildResult, FolioPage, FolioPlugin, FolioPluginConfig, FolioPluginContext, FolioPluginFactory, FolioPluginLogger, } from "./plugin.js";
3
+ export { createFolioPlugin, defineFolioPlugin, validateFolioPlugin, validateFolioPlugins, } from "./plugin.js";
4
+ export { createFolioPluginLifecycleManager, FolioPluginHookError, FolioPluginLifecycleManager, } from "./core/plugin/index.js";
5
+ export type { FolioPluginHookMetadata, FolioPluginLifecycleOptions, } from "./core/plugin/index.js";
6
+ export type { SearchAdapter, SearchContext } from "./search/provider.js";
2
7
  export * from "./navigation/index.js";
3
8
  export * from "./config.js";
4
9
  export * from "./core/content-scanner.js";
5
10
  export * from "./core/route-tree.js";
6
11
  export * from "./mdx/compiler.js";
7
12
  export * from "./mdx/highlighter.js";
8
- export * from "./components/mdx-components.js";
9
- export * from "./themes/index.js";
10
13
  export * from "./server/index.js";
11
- import { nikalaDocsPlugin } from "./server/plugin.js";
14
+ export * from "./search/provider.js";
15
+ import { nikalaDocsPlugin } from "./server/plugin/index.js";
12
16
  export declare const nikalaDocs: typeof nikalaDocsPlugin;
package/dist/index.js CHANGED
@@ -1,13 +1,14 @@
1
1
  // packages/docs/src/index.ts
2
2
  export * from "./types.js";
3
+ export { createFolioPlugin, defineFolioPlugin, validateFolioPlugin, validateFolioPlugins, } from "./plugin.js";
4
+ export { createFolioPluginLifecycleManager, FolioPluginHookError, FolioPluginLifecycleManager, } from "./core/plugin/index.js";
3
5
  export * from "./navigation/index.js";
4
6
  export * from "./config.js";
5
7
  export * from "./core/content-scanner.js";
6
8
  export * from "./core/route-tree.js";
7
9
  export * from "./mdx/compiler.js";
8
10
  export * from "./mdx/highlighter.js";
9
- export * from "./components/mdx-components.js";
10
- export * from "./themes/index.js";
11
11
  export * from "./server/index.js";
12
- import { nikalaDocsPlugin } from "./server/plugin.js";
12
+ export * from "./search/provider.js";
13
+ import { nikalaDocsPlugin } from "./server/plugin/index.js";
13
14
  export const nikalaDocs = nikalaDocsPlugin;
@@ -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;