@nikala-ui/folio 0.15.0 → 0.16.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 (83) hide show
  1. package/dist/cli/commands/init.js +89 -54
  2. package/dist/components/ui/code-block-commands.d.ts +4 -0
  3. package/dist/components/ui/code-block-commands.js +58 -0
  4. package/dist/components/ui/code-block.d.ts +2 -1
  5. package/dist/components/ui/code-block.jsx +4 -127
  6. package/dist/components/ui/code-highlighter.d.ts +2 -0
  7. package/dist/components/ui/code-highlighter.js +76 -0
  8. package/dist/config.js +3 -0
  9. package/dist/index.d.ts +2 -1
  10. package/dist/index.js +1 -0
  11. package/dist/mdx/builtin-languages.d.ts +34 -0
  12. package/dist/mdx/builtin-languages.js +54 -0
  13. package/dist/mdx/custom-languages.d.ts +10 -0
  14. package/dist/mdx/custom-languages.js +52 -0
  15. package/dist/mdx/highlighter.d.ts +2 -1
  16. package/dist/mdx/highlighter.js +19 -52
  17. package/dist/search/provider.d.ts +9 -0
  18. package/dist/search/provider.js +4 -2
  19. package/dist/server/plugin/constants.d.ts +2 -0
  20. package/dist/server/plugin/constants.js +2 -0
  21. package/dist/server/plugin/index.js +2 -1
  22. package/dist/server/plugin/virtual-modules.js +41 -2
  23. package/dist/templates/env.d.ts +31 -0
  24. package/dist/templates/locales/en.json +77 -0
  25. package/dist/themes/default/components/docs-landing-body.d.ts +17 -0
  26. package/dist/themes/default/components/docs-landing-body.jsx +17 -0
  27. package/dist/themes/default/components/docs-page-body.d.ts +21 -0
  28. package/dist/themes/default/components/docs-page-body.jsx +36 -0
  29. package/dist/themes/default/components/page-actions.d.ts +15 -0
  30. package/dist/themes/default/components/page-actions.jsx +60 -0
  31. package/dist/themes/default/components/page-header.d.ts +7 -0
  32. package/dist/themes/default/components/page-header.jsx +3 -0
  33. package/dist/themes/default/components/sidebar-footer.d.ts +6 -0
  34. package/dist/themes/default/components/sidebar-footer.jsx +12 -0
  35. package/dist/themes/default/components/sidebar-header.d.ts +8 -0
  36. package/dist/themes/default/components/sidebar-header.jsx +17 -0
  37. package/dist/themes/default/components/sidebar-mobile-nav.d.ts +7 -0
  38. package/dist/themes/default/components/sidebar-mobile-nav.jsx +17 -0
  39. package/dist/themes/default/components/sidebar-promo.d.ts +8 -0
  40. package/dist/themes/default/components/sidebar-promo.jsx +20 -0
  41. package/dist/themes/default/hooks/use-page-copy.d.ts +6 -0
  42. package/dist/themes/default/hooks/use-page-copy.js +26 -0
  43. package/dist/themes/default/layout.jsx +23 -147
  44. package/dist/themes/default/lib/layout-visibility.d.ts +8 -0
  45. package/dist/themes/default/lib/layout-visibility.js +24 -0
  46. package/dist/themes/default/navigation/mobile-table-of-contents.jsx +2 -2
  47. package/dist/themes/default/sidebar.jsx +17 -47
  48. package/dist/types.d.ts +9 -0
  49. package/dist/vendor/docs-src/client/page-actions.ts +69 -0
  50. package/dist/vendor/docs-src/components/ui/code-block-commands.ts +65 -0
  51. package/dist/vendor/docs-src/components/ui/code-block.tsx +5 -132
  52. package/dist/vendor/docs-src/components/ui/code-highlighter.ts +76 -0
  53. package/dist/vendor/docs-src/navigation/index.ts +2 -0
  54. package/dist/vendor/docs-src/navigation/repository-links.ts +43 -0
  55. package/dist/vendor/docs-src/navigation/sidebar-state.ts +22 -0
  56. package/dist/vendor/docs-src/navigation/sidebar-tree.ts +15 -0
  57. package/dist/vendor/docs-src/search/provider.ts +83 -0
  58. package/dist/vendor/docs-src/themes/default/components/docs-landing-body.tsx +30 -0
  59. package/dist/vendor/docs-src/themes/default/components/docs-page-body.tsx +53 -0
  60. package/dist/vendor/docs-src/themes/default/components/page-actions.tsx +105 -0
  61. package/dist/vendor/docs-src/themes/default/components/page-header.tsx +31 -0
  62. package/dist/vendor/docs-src/themes/default/components/sidebar-footer.tsx +21 -0
  63. package/dist/vendor/docs-src/themes/default/components/sidebar-header.tsx +30 -0
  64. package/dist/vendor/docs-src/themes/default/components/sidebar-mobile-nav.tsx +33 -0
  65. package/dist/vendor/docs-src/themes/default/components/sidebar-promo.tsx +37 -0
  66. package/dist/vendor/docs-src/themes/default/content/breadcrumbs.tsx +43 -0
  67. package/dist/vendor/docs-src/themes/default/content/pager.tsx +19 -0
  68. package/dist/vendor/docs-src/themes/default/content/table-of-contents.tsx +32 -0
  69. package/dist/vendor/docs-src/themes/default/hooks/use-page-copy.ts +29 -0
  70. package/dist/vendor/docs-src/themes/default/icons.ts +16 -0
  71. package/dist/vendor/docs-src/themes/default/index.ts +46 -0
  72. package/dist/vendor/docs-src/themes/default/layout.tsx +176 -0
  73. package/dist/vendor/docs-src/themes/default/lib/layout-visibility.ts +39 -0
  74. package/dist/vendor/docs-src/themes/default/navbar.tsx +130 -0
  75. package/dist/vendor/docs-src/themes/default/navigation/mobile-table-of-contents.tsx +61 -0
  76. package/dist/vendor/docs-src/themes/default/navigation/sidebar-tree.tsx +202 -0
  77. package/dist/vendor/docs-src/themes/default/overlays/search-dialog.tsx +106 -0
  78. package/dist/vendor/docs-src/themes/default/sidebar.tsx +86 -0
  79. package/dist/vendor/docs-src/themes/index.ts +4 -0
  80. package/dist/vendor/docs-src/themes/types.ts +87 -0
  81. package/dist/version.d.ts +1 -1
  82. package/dist/version.js +1 -1
  83. package/package.json +3 -3
@@ -15,17 +15,8 @@ async function listSourceFiles(dir) {
15
15
  }
16
16
  return result;
17
17
  }
18
- async function stripSourceMapComments(dir) {
19
- if (!(await fs.pathExists(dir)))
20
- return;
21
- for (const sourceFile of await listSourceFiles(dir)) {
22
- if (!/\.(?:d\.ts|[cm]?[jt]sx?)$/.test(sourceFile))
23
- continue;
24
- const content = await fs.readFile(sourceFile, "utf-8");
25
- const cleaned = content.replace(/\s*\/\/[#@]\s*sourceMappingURL=.*$/gm, "");
26
- if (cleaned !== content)
27
- await fs.writeFile(sourceFile, cleaned, "utf-8");
28
- }
18
+ function isAuthoredSourceFile(file) {
19
+ return /\.(?:ts|tsx)$/.test(file) && !file.endsWith(".d.ts");
29
20
  }
30
21
  async function resolveRegistryDir() {
31
22
  const commandDir = path.dirname(fileURLToPath(import.meta.url));
@@ -79,7 +70,7 @@ async function copyRegistrySource(root) {
79
70
  ].filter((file) => fs.existsSync(file));
80
71
  const projectFiles = [
81
72
  ...(await listSourceFiles(path.join(root, "docs"))),
82
- ...(await listSourceFiles(path.join(root, "src/themes/default"))),
73
+ ...(await listSourceFiles(path.join(root, "src/themes/default"))).filter(isAuthoredSourceFile),
83
74
  ...docsComponentSources,
84
75
  ];
85
76
  const projectSource = (await Promise.all(projectFiles.map((file) => fs.readFile(file, "utf-8")))).join("\n");
@@ -152,7 +143,8 @@ async function copyRegistrySource(root) {
152
143
  : file.path.startsWith("ui/")
153
144
  ? path.join(componentsDir, relative)
154
145
  : path.join(hooksDir, relative);
155
- await fs.outputFile(destination, file.content, "utf-8");
146
+ if (!(await fs.pathExists(destination)))
147
+ await fs.outputFile(destination, file.content, "utf-8");
156
148
  if (file.path.startsWith("hooks/"))
157
149
  hookFiles.push(relative.replace(/\.(tsx?|jsx?)$/, ""));
158
150
  else if (file.path.startsWith("ui/"))
@@ -167,10 +159,12 @@ async function copyRegistrySource(root) {
167
159
  ];
168
160
  const internalComponentsSource = internalComponentCandidates.find((directory) => fs.existsSync(directory));
169
161
  if (internalComponentsSource) {
170
- for (const name of ["theme-toggle"]) {
162
+ for (const name of ["theme-toggle", "sidebar", "section-heading", "command"]) {
171
163
  const source = path.join(internalComponentsSource, `${name}.tsx`);
172
164
  if (await fs.pathExists(source)) {
173
- await fs.copy(source, path.join(componentsDir, `${name}.tsx`));
165
+ const destination = path.join(componentsDir, `${name}.tsx`);
166
+ if (!(await fs.pathExists(destination)))
167
+ await fs.copy(source, destination);
174
168
  componentFiles.push(name);
175
169
  }
176
170
  }
@@ -180,91 +174,105 @@ async function copyRegistrySource(root) {
180
174
  path.resolve(commandDir, "../../vendor/docs-src/lib/cn.ts"),
181
175
  ];
182
176
  const cnSource = cnCandidates.find((candidate) => fs.existsSync(candidate));
183
- if (cnSource)
184
- await fs.outputFile(path.join(root, "src/lib/cn.ts"), await fs.readFile(cnSource, "utf-8"), "utf-8");
177
+ const cnDestination = path.join(root, "src/lib/cn.ts");
178
+ if (cnSource && !(await fs.pathExists(cnDestination))) {
179
+ await fs.outputFile(cnDestination, await fs.readFile(cnSource, "utf-8"), "utf-8");
180
+ }
185
181
  return { componentFiles, hookFiles, dependencies: [...dependencies].sort() };
186
182
  }
187
183
  async function copyPluginSources(root) {
188
184
  const commandDir = path.dirname(fileURLToPath(import.meta.url));
189
185
  const sourceCandidates = [
190
186
  path.resolve(commandDir, "../../../src/plugins"),
191
- path.resolve(commandDir, "../../plugins"),
187
+ path.resolve(commandDir, "../../vendor/docs-src/plugins"),
192
188
  ];
193
189
  const source = sourceCandidates.find((candidate) => fs.existsSync(candidate));
194
190
  if (!source)
195
191
  throw new Error("Folio plugin sources are missing from the package");
196
- for (const sourceFile of await listSourceFiles(source)) {
192
+ for (const sourceFile of (await listSourceFiles(source)).filter(isAuthoredSourceFile)) {
197
193
  const relative = path.relative(source, sourceFile);
198
194
  const destination = path.join(root, "src/plugins", relative);
199
195
  const content = (await fs.readFile(sourceFile, "utf-8"))
200
196
  .replace(/from "(?:\.\.\/)+plugin\.js"/g, 'from "@nikala-ui/folio"')
201
- .replace(/from "(?:\.\.\/)+types\.js"/g, 'from "@nikala-ui/folio"');
202
- await fs.outputFile(destination, content, "utf-8");
197
+ .replace(/from "(?:\.\.\/)+types\.js"/g, 'from "@nikala-ui/folio"')
198
+ .replace(/(from "(?:\.\/|\.\.\/)[^"]+)\.jsx"/g, "$1.tsx\"")
199
+ .replace(/(from "(?:\.\/|\.\.\/)[^"]+)\.js"/g, "$1.ts\"");
200
+ if (!(await fs.pathExists(destination)))
201
+ await fs.outputFile(destination, content, "utf-8");
203
202
  }
204
203
  }
205
204
  async function copyCustomTheme(root) {
206
205
  const commandDir = path.dirname(fileURLToPath(import.meta.url));
207
- const sourceCandidates = [path.resolve(commandDir, "../../../src/themes/default"), path.resolve(commandDir, "../../themes/default")];
206
+ const sourceCandidates = [
207
+ path.resolve(commandDir, "../../../src/themes/default"),
208
+ path.resolve(commandDir, "../../vendor/docs-src/themes/default"),
209
+ ];
208
210
  const source = sourceCandidates.find((candidate) => fs.existsSync(candidate));
209
211
  const target = path.join(root, "src/themes/default");
210
212
  if (!source) {
211
- await fs.outputFile(path.join(target, "index.ts"), `export { defaultTheme as default } from "@nikala-ui/folio";
213
+ const destination = path.join(target, "index.ts");
214
+ if (!(await fs.pathExists(destination)))
215
+ await fs.outputFile(destination, `export { defaultTheme as default } from "@nikala-ui/folio";
212
216
  `, "utf-8");
213
217
  return;
214
218
  }
215
219
  for (const sourceFile of await listSourceFiles(source)) {
216
220
  const basename = path.basename(sourceFile);
217
- const isSourceFile = /\.(ts|tsx)$/.test(sourceFile);
218
- const isPublishedThemeFile = /\.jsx?$/.test(sourceFile);
219
- if ((!isSourceFile && !isPublishedThemeFile) || basename !== basename.toLowerCase())
221
+ if (!isAuthoredSourceFile(sourceFile) || basename !== basename.toLowerCase())
220
222
  continue;
221
223
  const relative = path.relative(source, sourceFile)
222
224
  .replace(/\.jsx?$/, ".tsx")
223
225
  .replace(/index\.tsx$/, "index.ts");
224
226
  const destination = path.join(target, relative);
227
+ const runtimePrefix = relative.includes(path.sep) ? "../" : "./";
225
228
  let content = await fs.readFile(sourceFile, "utf-8");
226
229
  content = content
227
230
  .replace(/\s*\/\/[#@]\s*sourceMappingURL=.*$/gm, "")
228
231
  .replace(/from "@nikala-ui\/core\/ui\//g, 'from "@/components/ui/')
229
232
  .replace(/from "@nikala-ui\/core"/g, 'from "@/components/ui"')
230
233
  .replace(/from "@nikala-ui\/hooks"/g, 'from "@/hooks"')
231
- .replace(/from "\.\.\/\.\.\/client\/page-actions\.js"/g, 'from "./runtime/page-actions.js"')
232
- .replace(/from "\.\.\/\.\.\/navigation\/repository-links\.js"/g, 'from "./runtime/repository-links.js"')
233
- .replace(/from "\.\.\/\.\.\/search\/provider\.js"/g, 'from "./runtime/search-provider.js"')
234
- .replace(/from "\.\.\/\.\.\/\.\.\/search\/provider\.js"/g, 'from "../runtime/search-provider.js"')
234
+ .replace(/from "(?:\.\.\/)+client\/page-actions\.js"/g, `from "${runtimePrefix}runtime/page-actions.js"`)
235
+ .replace(/from "(?:\.\.\/)+navigation\/repository-links\.js"/g, `from "${runtimePrefix}runtime/repository-links.js"`)
236
+ .replace(/from "(?:\.\.\/)+search\/provider\.js"/g, `from "${runtimePrefix}runtime/search-provider.js"`)
235
237
  .replace(/from "\.\.\/\.\.\/\.\.\/navigation\/sidebar-state\.js"/g, 'from "./sidebar-state.js"')
236
238
  .replace(/from "(?:\.\.\/)+types\.js"/g, 'from "@nikala-ui/folio"')
239
+ .replace(/import\("(?:\.\.\/)+types\.js"\)/g, 'import("@nikala-ui/folio")')
237
240
  .replace(/from "(\.\.\/|\.\/)[^"]+\.(?:jsx|tsx|js)"/g, (match) => match.replace(/\.(?:jsx|tsx|js)"$/, '"'))
238
241
  .replace(/export \* from "(\.\.\/|\.\/)[^"]+\.(?:jsx|tsx|js)"/g, (match) => match.replace(/\.(?:jsx|tsx|js)"$/, '"'));
239
- await fs.outputFile(destination, content, "utf-8");
242
+ if (!(await fs.pathExists(destination)))
243
+ await fs.outputFile(destination, content, "utf-8");
240
244
  }
241
245
  const runtimeSources = [
242
- ["../../client/page-actions.js", "runtime/page-actions.js"],
243
- ["../../navigation/repository-links.js", "runtime/repository-links.js"],
244
- ["../../search/provider.js", "runtime/search-provider.js"],
246
+ [["../../../src/client/page-actions.ts", "../../vendor/docs-src/client/page-actions.ts"], "runtime/page-actions.ts"],
247
+ [["../../../src/navigation/repository-links.ts", "../../vendor/docs-src/navigation/repository-links.ts"], "runtime/repository-links.ts"],
248
+ [["../../../src/search/provider.ts", "../../vendor/docs-src/search/provider.ts"], "runtime/search-provider.ts"],
245
249
  ];
246
- for (const [relativeSource, relativeDestination] of runtimeSources) {
247
- const sourceFile = path.resolve(commandDir, relativeSource);
248
- if (!fs.existsSync(sourceFile))
250
+ for (const [relativeSources, relativeDestination] of runtimeSources) {
251
+ const sourceFile = relativeSources
252
+ .map((relativeSource) => path.resolve(commandDir, relativeSource))
253
+ .find((candidate) => fs.existsSync(candidate));
254
+ if (!sourceFile)
249
255
  continue;
250
256
  const destination = path.join(target, relativeDestination);
251
- const content = (await fs.readFile(sourceFile, "utf-8"))
252
- .replace(/from "\.\.\/types\.js"/g, 'from "@nikala-ui/folio"');
253
- await fs.outputFile(destination, content, "utf-8");
257
+ if (!(await fs.pathExists(destination))) {
258
+ const content = (await fs.readFile(sourceFile, "utf-8"))
259
+ .replace(/\s*\/\/[#@]\s*sourceMappingURL=.*$/gm, "")
260
+ .replace(/from "\.\.\/types\.js"/g, 'from "@nikala-ui/folio"');
261
+ await fs.outputFile(destination, content, "utf-8");
262
+ }
254
263
  }
255
264
  const navigationCandidates = [
256
265
  path.resolve(commandDir, "../../../src/navigation/sidebar-state.ts"),
257
- path.resolve(commandDir, "../../navigation/sidebar-state.js"),
266
+ path.resolve(commandDir, "../../vendor/docs-src/navigation/sidebar-state.ts"),
258
267
  ];
259
268
  const navigationSource = navigationCandidates.find((candidate) => fs.existsSync(candidate));
260
269
  if (navigationSource) {
261
270
  const navigationContent = (await fs.readFile(navigationSource, "utf-8"))
262
271
  .replace(/from "\.\.\/types\.js"/g, 'from "@nikala-ui/folio"');
263
- await fs.outputFile(path.join(target, "navigation/sidebar-state.ts"), navigationContent, "utf-8");
272
+ const navigationTarget = path.join(target, "navigation/sidebar-state.ts");
273
+ if (!(await fs.pathExists(navigationTarget)))
274
+ await fs.outputFile(navigationTarget, navigationContent, "utf-8");
264
275
  }
265
- // Remove stale references left by older generated themes so upgrading and
266
- // re-running init is enough to silence Vite's missing-map warnings.
267
- await stripSourceMapComments(target);
268
276
  }
269
277
  async function copyDefaultAssets(root) {
270
278
  const commandDir = path.dirname(fileURLToPath(import.meta.url));
@@ -280,11 +288,15 @@ async function copyDefaultAssets(root) {
280
288
  await fs.copy(source, destination);
281
289
  }
282
290
  async function writeProjectFiles(root, registryDependencies) {
291
+ const commandDir = path.dirname(fileURLToPath(import.meta.url));
283
292
  const docsConfigPath = path.join(root, "docs.config.ts");
284
293
  if (!(await fs.pathExists(docsConfigPath)))
285
- await fs.outputFile(docsConfigPath, `export default {
294
+ await fs.outputFile(docsConfigPath, `import { createI18nPlugin } from "./src/plugins/i18n/index.ts";
295
+
296
+ export default {
297
+ plugins: [createI18nPlugin({ defaultLocale: "en", locales: ["en"] })],
286
298
  title: "My Project Docs",
287
- description: "Documentation built with Folio and SolidJS",
299
+ description: "Documentation built with Folio",
288
300
  favicon: "/favicon.ico",
289
301
  contentDir: "docs",
290
302
  css: "src/index.css",
@@ -294,6 +306,17 @@ async function writeProjectFiles(root, registryDependencies) {
294
306
  search: { enabled: true },
295
307
  };
296
308
  `, "utf-8");
309
+ const localeTemplate = path.resolve(commandDir, "../../templates/locales/en.json");
310
+ const localePath = path.join(root, "locales/en.json");
311
+ if (!(await fs.pathExists(localePath)) && await fs.pathExists(localeTemplate)) {
312
+ await fs.ensureDir(path.dirname(localePath));
313
+ await fs.copyFile(localeTemplate, localePath);
314
+ }
315
+ const environmentTemplate = path.resolve(commandDir, "../../templates/env.d.ts");
316
+ const environmentPath = path.join(root, "src/env.d.ts");
317
+ if (!(await fs.pathExists(environmentPath)) && await fs.pathExists(environmentTemplate)) {
318
+ await fs.copyFile(environmentTemplate, environmentPath);
319
+ }
297
320
  const nikalaConfigPath = path.join(root, "nikala.config.json");
298
321
  if (!(await fs.pathExists(nikalaConfigPath))) {
299
322
  await fs.writeJson(nikalaConfigPath, {
@@ -309,7 +332,6 @@ async function writeProjectFiles(root, registryDependencies) {
309
332
  }, { spaces: 2 });
310
333
  }
311
334
  const packagePath = path.join(root, "package.json");
312
- const commandDir = path.dirname(fileURLToPath(import.meta.url));
313
335
  const docsPackageRoot = path.resolve(commandDir, "../../..");
314
336
  const workspaceRoot = path.resolve(docsPackageRoot, "../..");
315
337
  const workspaceManifestPath = path.join(workspaceRoot, "package.json");
@@ -322,7 +344,12 @@ async function writeProjectFiles(root, registryDependencies) {
322
344
  private: true,
323
345
  type: "module",
324
346
  scripts: { dev: "bunx @nikala-ui/folio dev", build: "bunx @nikala-ui/folio build", preview: "bunx @nikala-ui/folio preview" },
325
- dependencies: {},
347
+ dependencies: { "fs-extra": "^11.3.6" },
348
+ devDependencies: {
349
+ "@types/fs-extra": "latest",
350
+ "@types/node": "latest",
351
+ typescript: "latest",
352
+ },
326
353
  };
327
354
  const existingPackageJson = await fs.pathExists(packagePath) ? await fs.readJson(packagePath) : {};
328
355
  const packageJson = {
@@ -350,11 +377,18 @@ async function writeProjectFiles(root, registryDependencies) {
350
377
  .filter((dependency) => !packageJson.dependencies?.[dependency])
351
378
  .map((dependency) => [dependency, "latest"])),
352
379
  };
380
+ packageJson.devDependencies = {
381
+ ...defaultPackageJson.devDependencies,
382
+ ...existingPackageJson.devDependencies,
383
+ };
353
384
  await fs.writeJson(packagePath, packageJson, { spaces: 2 });
354
- await fs.outputFile(path.join(root, "tsconfig.json"), JSON.stringify({
355
- compilerOptions: { target: "ES2022", module: "ESNext", moduleResolution: "Bundler", jsx: "preserve", jsxImportSource: "solid-js", strict: true, skipLibCheck: true, paths: { "@/*": ["./src/*"], "@/components/*": ["./src/components/*"], "@/components/ui/*": ["./src/components/ui/*"], "@/hooks/*": ["./src/hooks/*"], "@/plugins/*": ["./src/plugins/*"] } },
356
- include: ["src/**/*", "docs.config.ts"],
357
- }, null, 2) + "\n", "utf-8");
385
+ const tsconfigPath = path.join(root, "tsconfig.json");
386
+ if (!(await fs.pathExists(tsconfigPath))) {
387
+ await fs.outputFile(tsconfigPath, JSON.stringify({
388
+ compilerOptions: { target: "ES2022", module: "ESNext", moduleResolution: "Bundler", jsx: "preserve", jsxImportSource: "solid-js", allowImportingTsExtensions: true, strict: true, skipLibCheck: true, paths: { "@/*": ["./src/*"], "@/components/*": ["./src/components/*"], "@/components/ui/*": ["./src/components/ui/*"], "@/hooks/*": ["./src/hooks/*"], "@/plugins/*": ["./src/plugins/*"] } },
389
+ include: ["src/**/*", "docs.config.ts"],
390
+ }, null, 2) + "\n", "utf-8");
391
+ }
358
392
  }
359
393
  export async function runInitCommand(targetDir = ".") {
360
394
  const root = path.resolve(process.cwd(), targetDir);
@@ -363,7 +397,8 @@ export async function runInitCommand(targetDir = ".") {
363
397
  console.log(pc.dim(` Initializing copy-paste documentation project in ${pc.bold(root)}...`));
364
398
  console.log();
365
399
  await fs.ensureDir(root);
366
- runNikalaInit(root);
400
+ if (!(await fs.pathExists(path.join(root, "src/lib/cn.ts"))))
401
+ runNikalaInit(root);
367
402
  await writeProjectFiles(root, []);
368
403
  const indexPath = path.join(root, "docs/index.mdx");
369
404
  if (!(await fs.pathExists(indexPath)))
@@ -0,0 +1,4 @@
1
+ export type CodeBlockPackageManager = "bunx" | "npx" | "pnpm" | "yarn";
2
+ export declare function highlightCliCommand(code: string): string;
3
+ export declare function transformCommandForPm(cmd: string, pm: CodeBlockPackageManager): string;
4
+ export declare function escapeHtml(str: string): string;
@@ -0,0 +1,58 @@
1
+ // Pure helpers that format and highlight CLI commands for CodeBlock's package manager runner.
2
+ export function highlightCliCommand(code) {
3
+ const trimmed = code.trim();
4
+ const parts = trimmed.split(/\s+/);
5
+ if (parts.length === 0)
6
+ return code;
7
+ const isRunner = ["bunx", "npx", "pnpm", "yarn", "bun", "npm", "cargo", "deno"].includes(parts[0]);
8
+ if (!isRunner)
9
+ return `<span class="text-foreground">${escapeHtml(trimmed)}</span>`;
10
+ return parts
11
+ .map((part, index) => {
12
+ if (index === 0) {
13
+ return `<span class="text-amber-500 dark:text-amber-400 font-bold">${escapeHtml(part)}</span>`;
14
+ }
15
+ if (part.startsWith("-")) {
16
+ return `<span class="text-purple-400 dark:text-purple-300">${escapeHtml(part)}</span>`;
17
+ }
18
+ if (part.startsWith("@")) {
19
+ return `<span class="text-emerald-400 dark:text-emerald-300 font-medium">${escapeHtml(part)}</span>`;
20
+ }
21
+ if (index === 1 && ["add", "install", "init", "create", "run"].includes(part)) {
22
+ return `<span class="text-sky-500 dark:text-sky-400 font-medium">${escapeHtml(part)}</span>`;
23
+ }
24
+ return `<span class="text-foreground/90">${escapeHtml(part)}</span>`;
25
+ })
26
+ .join(" ");
27
+ }
28
+ export function transformCommandForPm(cmd, pm) {
29
+ const trimmed = cmd.trim();
30
+ const runners = ["bunx", "npx", "pnpm dlx", "pnpm", "yarn dlx", "yarn", "bun", "npm"];
31
+ let base = trimmed;
32
+ for (const r of runners) {
33
+ if (trimmed.startsWith(r)) {
34
+ base = trimmed.slice(r.length).trim();
35
+ break;
36
+ }
37
+ }
38
+ switch (pm) {
39
+ case "bunx":
40
+ return `bunx ${base}`;
41
+ case "npx":
42
+ return `npx ${base}`;
43
+ case "pnpm":
44
+ return `pnpm dlx ${base}`;
45
+ case "yarn":
46
+ return `yarn dlx ${base}`;
47
+ default:
48
+ return `${pm} ${base}`;
49
+ }
50
+ }
51
+ export function escapeHtml(str) {
52
+ return str
53
+ .replace(/&/g, "&amp;")
54
+ .replace(/</g, "&lt;")
55
+ .replace(/>/g, "&gt;")
56
+ .replace(/"/g, "&quot;")
57
+ .replace(/'/g, "&#039;");
58
+ }
@@ -1,5 +1,6 @@
1
1
  import { type JSX, type ParentComponent } from "solid-js";
2
- export type CodeBlockPackageManager = "bunx" | "npx" | "pnpm" | "yarn";
2
+ import { type CodeBlockPackageManager } from "./code-block-commands";
3
+ export type { CodeBlockPackageManager };
3
4
  export interface CodeBlockProps extends JSX.HTMLAttributes<HTMLDivElement> {
4
5
  /** The raw source code text to display and copy */
5
6
  code?: string;
@@ -5,115 +5,10 @@ import { Badge } from "@/components/ui/badge";
5
5
  import { Tabs, TabsList, TabsTrigger } from "@/components/ui/tabs";
6
6
  import { Tooltip, TooltipTrigger, TooltipContent } from "@/components/ui/tooltip";
7
7
  import { createClipboard } from "@/hooks/create-clipboard";
8
+ import { highlightOnClient } from "./code-highlighter";
9
+ import { escapeHtml, highlightCliCommand, transformCommandForPm } from "./code-block-commands";
8
10
  import { useSiteLocale } from "../../plugins/i18n/runtime.jsx";
9
11
  import { cn } from "@/lib/cn";
10
- let shikiHighlighterPromise = null;
11
- async function getShikiHighlighter() {
12
- if (typeof window === "undefined")
13
- return null;
14
- if (!shikiHighlighterPromise) {
15
- try {
16
- const [{ createBundledHighlighter }, { createJavaScriptRegexEngine }] = await Promise.all([
17
- import("shiki/core"),
18
- import("shiki/engine/javascript"),
19
- ]);
20
- const createHighlighter = createBundledHighlighter({
21
- langs: {
22
- typescript: () => import("@shikijs/langs/typescript"),
23
- javascript: () => import("@shikijs/langs/javascript"),
24
- tsx: () => import("@shikijs/langs/tsx"),
25
- jsx: () => import("@shikijs/langs/jsx"),
26
- bash: () => import("@shikijs/langs/shellscript"),
27
- json: () => import("@shikijs/langs/json"),
28
- css: () => import("@shikijs/langs/css"),
29
- html: () => import("@shikijs/langs/html"),
30
- rust: () => import("@shikijs/langs/rust"),
31
- },
32
- themes: {
33
- "github-dark": () => import("@shikijs/themes/github-dark"),
34
- "github-light": () => import("@shikijs/themes/github-light"),
35
- },
36
- engine: () => createJavaScriptRegexEngine(),
37
- });
38
- shikiHighlighterPromise = createHighlighter({
39
- themes: ["github-dark", "github-light"],
40
- langs: [
41
- "typescript",
42
- "javascript",
43
- "tsx",
44
- "jsx",
45
- "bash",
46
- "json",
47
- "css",
48
- "html",
49
- "rust",
50
- ],
51
- });
52
- }
53
- catch (e) {
54
- console.warn("Failed to load Shiki highlighter", e);
55
- return null;
56
- }
57
- }
58
- return shikiHighlighterPromise;
59
- }
60
- function highlightCliCommand(code) {
61
- const trimmed = code.trim();
62
- const parts = trimmed.split(/\s+/);
63
- if (parts.length === 0)
64
- return code;
65
- const isRunner = ["bunx", "npx", "pnpm", "yarn", "bun", "npm", "cargo", "deno"].includes(parts[0]);
66
- if (!isRunner)
67
- return `<span class="text-foreground">${escapeHtml(trimmed)}</span>`;
68
- return parts
69
- .map((part, index) => {
70
- if (index === 0) {
71
- return `<span class="text-amber-500 dark:text-amber-400 font-bold">${escapeHtml(part)}</span>`;
72
- }
73
- if (part.startsWith("-")) {
74
- return `<span class="text-purple-400 dark:text-purple-300">${escapeHtml(part)}</span>`;
75
- }
76
- if (part.startsWith("@")) {
77
- return `<span class="text-emerald-400 dark:text-emerald-300 font-medium">${escapeHtml(part)}</span>`;
78
- }
79
- if (index === 1 && ["add", "install", "init", "create", "run"].includes(part)) {
80
- return `<span class="text-sky-500 dark:text-sky-400 font-medium">${escapeHtml(part)}</span>`;
81
- }
82
- return `<span class="text-foreground/90">${escapeHtml(part)}</span>`;
83
- })
84
- .join(" ");
85
- }
86
- function transformCommandForPm(cmd, pm) {
87
- const trimmed = cmd.trim();
88
- const runners = ["bunx", "npx", "pnpm dlx", "pnpm", "yarn dlx", "yarn", "bun", "npm"];
89
- let base = trimmed;
90
- for (const r of runners) {
91
- if (trimmed.startsWith(r)) {
92
- base = trimmed.slice(r.length).trim();
93
- break;
94
- }
95
- }
96
- switch (pm) {
97
- case "bunx":
98
- return `bunx ${base}`;
99
- case "npx":
100
- return `npx ${base}`;
101
- case "pnpm":
102
- return `pnpm dlx ${base}`;
103
- case "yarn":
104
- return `yarn dlx ${base}`;
105
- default:
106
- return `${pm} ${base}`;
107
- }
108
- }
109
- function escapeHtml(str) {
110
- return str
111
- .replace(/&/g, "&amp;")
112
- .replace(/</g, "&lt;")
113
- .replace(/>/g, "&gt;")
114
- .replace(/"/g, "&quot;")
115
- .replace(/'/g, "&#039;");
116
- }
117
12
  export const CodeBlock = (props) => {
118
13
  const siteLocale = useSiteLocale();
119
14
  const [local, rest] = splitProps(props, [
@@ -158,26 +53,8 @@ export const CodeBlock = (props) => {
158
53
  setHighlightedHtml("");
159
54
  return;
160
55
  }
161
- // Attempt Shiki highlighting
162
- getShikiHighlighter().then((highlighter) => {
163
- if (highlighter) {
164
- try {
165
- const html = highlighter.codeToHtml(codeStr, {
166
- lang: lang === "js" ? "javascript" : lang === "ts" ? "typescript" : lang,
167
- themes: {
168
- light: "github-light",
169
- dark: "github-dark",
170
- },
171
- });
172
- setHighlightedHtml(html);
173
- return;
174
- }
175
- catch (err) {
176
- console.warn(`Shiki failed for language: ${lang}`, err);
177
- }
178
- }
179
- // Fallback
180
- setHighlightedHtml(`<pre class="text-foreground/90"><code>${escapeHtml(codeStr)}</code></pre>`);
56
+ highlightOnClient(codeStr, lang).then((html) => {
57
+ setHighlightedHtml(html ?? `<pre class="text-foreground/90"><code>${escapeHtml(codeStr)}</code></pre>`);
181
58
  });
182
59
  });
183
60
  const handleCopy = async () => {
@@ -0,0 +1,2 @@
1
+ /** Highlight code in the browser, or return null when the caller should fall back to plain text. */
2
+ export declare function highlightOnClient(code: string, rawLang: string): Promise<string | null>;
@@ -0,0 +1,76 @@
1
+ // Browser-side Shiki highlighting for CodeBlock usages without server-rendered children.
2
+ let highlighterPromise = null;
3
+ async function getHighlighter() {
4
+ if (typeof window === "undefined")
5
+ return null;
6
+ if (!highlighterPromise) {
7
+ try {
8
+ const [{ createBundledHighlighter }, { createJavaScriptRegexEngine }] = await Promise.all([
9
+ import("shiki/core"),
10
+ import("shiki/engine/javascript"),
11
+ ]);
12
+ const createHighlighter = createBundledHighlighter({
13
+ langs: {
14
+ typescript: () => import("@shikijs/langs/typescript"),
15
+ javascript: () => import("@shikijs/langs/javascript"),
16
+ tsx: () => import("@shikijs/langs/tsx"),
17
+ jsx: () => import("@shikijs/langs/jsx"),
18
+ bash: () => import("@shikijs/langs/shellscript"),
19
+ json: () => import("@shikijs/langs/json"),
20
+ css: () => import("@shikijs/langs/css"),
21
+ html: () => import("@shikijs/langs/html"),
22
+ rust: () => import("@shikijs/langs/rust"),
23
+ },
24
+ themes: {
25
+ "github-dark": () => import("@shikijs/themes/github-dark"),
26
+ "github-light": () => import("@shikijs/themes/github-light"),
27
+ },
28
+ engine: () => createJavaScriptRegexEngine(),
29
+ });
30
+ highlighterPromise = createHighlighter({
31
+ themes: ["github-dark", "github-light"],
32
+ langs: ["typescript", "javascript", "tsx", "jsx", "bash", "json", "css", "html", "rust"],
33
+ });
34
+ }
35
+ catch (e) {
36
+ console.warn("Failed to load Shiki highlighter", e);
37
+ return null;
38
+ }
39
+ }
40
+ return highlighterPromise;
41
+ }
42
+ /**
43
+ * Resolve a language from the site's `shiki.customLangs`. The grammar module is
44
+ * a separate lazy chunk, fetched only for languages the highlighter lacks.
45
+ */
46
+ async function loadCustomLanguage(highlighter, lang) {
47
+ try {
48
+ const { default: languages } = await import("virtual:folio-highlight-languages");
49
+ const match = languages.find((entry) => [entry.name, ...(entry.aliases ?? [])].some((id) => id.toLowerCase() === lang));
50
+ if (!match)
51
+ return null;
52
+ await highlighter.loadLanguage(match);
53
+ return match.name;
54
+ }
55
+ catch (err) {
56
+ console.warn(`Failed to load custom Shiki language: ${lang}`, err);
57
+ return null;
58
+ }
59
+ }
60
+ /** Highlight code in the browser, or return null when the caller should fall back to plain text. */
61
+ export async function highlightOnClient(code, rawLang) {
62
+ const highlighter = await getHighlighter();
63
+ if (!highlighter)
64
+ return null;
65
+ const requested = rawLang === "js" ? "javascript" : rawLang === "ts" ? "typescript" : rawLang;
66
+ const lang = highlighter.getLoadedLanguages().includes(requested)
67
+ ? requested
68
+ : (await loadCustomLanguage(highlighter, requested.toLowerCase())) ?? requested;
69
+ try {
70
+ return highlighter.codeToHtml(code, { lang, themes: { light: "github-light", dark: "github-dark" } });
71
+ }
72
+ catch (err) {
73
+ console.warn(`Shiki failed for language: ${rawLang}`, err);
74
+ return null;
75
+ }
76
+ }
package/dist/config.js CHANGED
@@ -2,6 +2,7 @@
2
2
  import path from "node:path";
3
3
  import { pathToFileURL } from "node:url";
4
4
  import fs from "fs-extra";
5
+ import { validateCustomLanguages } from "./mdx/custom-languages.js";
5
6
  import { validateFolioPlugins } from "./plugin.js";
6
7
  import { loadSiteTranslations } from "./plugins/i18n/site-locale.js";
7
8
  export const DEFAULT_DOCS_CONFIG = {
@@ -51,6 +52,7 @@ export const DEFAULT_DOCS_CONFIG = {
51
52
  };
52
53
  export function defineDocsConfig(config) {
53
54
  validateFolioPlugins(config.plugins);
55
+ validateCustomLanguages(config.shiki?.customLangs);
54
56
  if (config.uiLocale !== undefined) {
55
57
  throw new Error("[folio] Configure site translations through createI18nPlugin() in DocsConfig.plugins");
56
58
  }
@@ -140,6 +142,7 @@ export async function resolveDocsConfig(cwd = process.cwd()) {
140
142
  ...pluginConfig.uiLocale,
141
143
  },
142
144
  };
145
+ validateCustomLanguages(mergedConfig.shiki?.customLangs);
143
146
  mergedConfig.uiLocale = await loadSiteTranslations(cwd, mergedConfig.uiLocale);
144
147
  return mergedConfig;
145
148
  }
package/dist/index.d.ts CHANGED
@@ -1,9 +1,10 @@
1
1
  export * from "./types.js";
2
+ export * from "./themes/types.js";
2
3
  export type { FolioBuildResult, FolioPage, FolioPlugin, FolioPluginConfig, FolioPluginContext, FolioPluginFactory, FolioPluginLogger, } from "./plugin.js";
3
4
  export { createFolioPlugin, defineFolioPlugin, validateFolioPlugin, validateFolioPlugins, } from "./plugin.js";
4
5
  export { createFolioPluginLifecycleManager, FolioPluginHookError, FolioPluginLifecycleManager, } from "./core/plugin/index.js";
5
6
  export type { FolioPluginHookMetadata, FolioPluginLifecycleOptions, } from "./core/plugin/index.js";
6
- export type { SearchAdapter, SearchContext } from "./search/provider.js";
7
+ export type { SearchAdapter, SearchAdapterRuntime, SearchContext } from "./search/provider.js";
7
8
  export * from "./navigation/index.js";
8
9
  export * from "./config.js";
9
10
  export * from "./core/content-scanner.js";
package/dist/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  // packages/docs/src/index.ts
2
2
  export * from "./types.js";
3
+ export * from "./themes/types.js";
3
4
  export { createFolioPlugin, defineFolioPlugin, validateFolioPlugin, validateFolioPlugins, } from "./plugin.js";
4
5
  export { createFolioPluginLifecycleManager, FolioPluginHookError, FolioPluginLifecycleManager, } from "./core/plugin/index.js";
5
6
  export * from "./navigation/index.js";