doccupine 0.0.59 → 0.0.61

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 (56) hide show
  1. package/README.md +86 -4
  2. package/dist/index.d.ts +1 -0
  3. package/dist/index.js +264 -12
  4. package/dist/templates/app/layout.d.ts +34 -1
  5. package/dist/templates/app/layout.js +98 -34
  6. package/dist/templates/components/Chat.d.ts +1 -1
  7. package/dist/templates/components/Chat.js +9 -15
  8. package/dist/templates/components/DocsSideBar.d.ts +1 -1
  9. package/dist/templates/components/DocsSideBar.js +14 -12
  10. package/dist/templates/components/MDXComponents.d.ts +1 -1
  11. package/dist/templates/components/MDXComponents.js +21 -0
  12. package/dist/templates/components/SectionNavProvider.d.ts +1 -0
  13. package/dist/templates/components/SectionNavProvider.js +102 -0
  14. package/dist/templates/components/SideBar.d.ts +1 -1
  15. package/dist/templates/components/SideBar.js +7 -2
  16. package/dist/templates/components/layout/ActionBar.d.ts +1 -1
  17. package/dist/templates/components/layout/ActionBar.js +17 -75
  18. package/dist/templates/components/layout/DocsComponents.d.ts +1 -1
  19. package/dist/templates/components/layout/DocsComponents.js +50 -11
  20. package/dist/templates/components/layout/Footer.d.ts +1 -1
  21. package/dist/templates/components/layout/Footer.js +3 -3
  22. package/dist/templates/components/layout/GlobalStyles.d.ts +1 -1
  23. package/dist/templates/components/layout/GlobalStyles.js +5 -2
  24. package/dist/templates/components/layout/Header.d.ts +1 -1
  25. package/dist/templates/components/layout/Header.js +82 -45
  26. package/dist/templates/components/layout/SectionBar.d.ts +1 -0
  27. package/dist/templates/components/layout/SectionBar.js +92 -0
  28. package/dist/templates/components/layout/StaticLinks.d.ts +1 -1
  29. package/dist/templates/components/layout/StaticLinks.js +11 -30
  30. package/dist/templates/mdx/ai-assistant.mdx.d.ts +1 -1
  31. package/dist/templates/mdx/ai-assistant.mdx.js +1 -1
  32. package/dist/templates/mdx/commands.mdx.d.ts +1 -1
  33. package/dist/templates/mdx/commands.mdx.js +3 -1
  34. package/dist/templates/mdx/deployment.mdx.d.ts +1 -1
  35. package/dist/templates/mdx/deployment.mdx.js +21 -6
  36. package/dist/templates/mdx/fonts.mdx.d.ts +1 -1
  37. package/dist/templates/mdx/fonts.mdx.js +9 -2
  38. package/dist/templates/mdx/footer-links.mdx.d.ts +1 -0
  39. package/dist/templates/mdx/footer-links.mdx.js +45 -0
  40. package/dist/templates/mdx/globals.mdx.d.ts +1 -1
  41. package/dist/templates/mdx/globals.mdx.js +6 -2
  42. package/dist/templates/mdx/links.mdx.d.ts +1 -1
  43. package/dist/templates/mdx/links.mdx.js +1 -1
  44. package/dist/templates/mdx/media-and-assets.mdx.d.ts +1 -1
  45. package/dist/templates/mdx/media-and-assets.mdx.js +6 -3
  46. package/dist/templates/mdx/model-context-protocol.mdx.d.ts +1 -1
  47. package/dist/templates/mdx/model-context-protocol.mdx.js +1 -1
  48. package/dist/templates/mdx/navigation.mdx.d.ts +1 -1
  49. package/dist/templates/mdx/navigation.mdx.js +43 -7
  50. package/dist/templates/mdx/sections.mdx.d.ts +1 -0
  51. package/dist/templates/mdx/sections.mdx.js +194 -0
  52. package/dist/templates/mdx/theme.mdx.d.ts +1 -1
  53. package/dist/templates/mdx/theme.mdx.js +7 -4
  54. package/dist/templates/utils/orderNavItems.d.ts +1 -1
  55. package/dist/templates/utils/orderNavItems.js +1 -0
  56. package/package.json +2 -2
package/README.md CHANGED
@@ -8,10 +8,12 @@
8
8
 
9
9
  - **Live preview** - watches your MDX files and regenerates pages on every save
10
10
  - **Auto-generated navigation** - sidebar built from frontmatter (`category`, `order`)
11
+ - **Sections** - organize docs into tabbed sections via frontmatter or `sections.json`
11
12
  - **Theming** - dark/light mode with customizable theme via `theme.json`
12
13
  - **AI chat assistant** - built-in RAG-powered chat (OpenAI, Anthropic, or Google)
13
14
  - **MCP server** - exposes `search_docs`, `get_doc`, and `list_docs` tools for AI agents
14
15
  - **Custom fonts** - Google Fonts or local fonts via `fonts.json`
16
+ - **Static assets** - `public/` directory is watched and synced to the generated app
15
17
  - **Zero config to start** - `npx doccupine` scaffolds everything and starts the server
16
18
 
17
19
  ## Quick Start
@@ -34,8 +36,8 @@ It then scaffolds the app, installs dependencies, and starts the dev server. Ope
34
36
  ```bash
35
37
  doccupine watch [options] # Default. Watch MDX files and start dev server
36
38
  doccupine build [options] # One-time build without starting the server
37
- doccupine config --show # Show current configuration
38
- doccupine config --reset # Re-prompt for configuration
39
+ doccupine config -show # Show current configuration
40
+ doccupine config -reset # Re-prompt for configuration
39
41
  ```
40
42
 
41
43
  ### Options
@@ -59,11 +61,66 @@ categoryOrder: 0 # Sort order for the category group
59
61
  order: 1 # Sort order within the category
60
62
  icon: "https://..." # Page favicon URL
61
63
  image: "https://..." # OpenGraph image URL
64
+ date: "2025-01-01" # Page date metadata
65
+ section: "API Reference" # Section this page belongs to
66
+ sectionOrder: 1 # Sort order for the section in the tab bar
62
67
  ---
63
68
  ```
64
69
 
65
70
  Navigation is auto-generated from `category`, `categoryOrder`, and `order`. Pages without a category appear ungrouped.
66
71
 
72
+ Use `sectionLabel` on your root `index.mdx` to rename the default "Docs" tab:
73
+
74
+ ```yaml
75
+ ---
76
+ title: "Welcome"
77
+ sectionLabel: "Guides"
78
+ ---
79
+ ```
80
+
81
+ ## Sections
82
+
83
+ Sections let you split your docs into separate tabbed groups (e.g. "Docs", "API Reference", "SDKs"). There are two ways to configure them:
84
+
85
+ ### Via frontmatter
86
+
87
+ Add a `section` field to your MDX files. The section slug is derived from the label automatically. Use `sectionOrder` to control tab order (lower numbers appear first):
88
+
89
+ ```yaml
90
+ ---
91
+ title: "Authentication"
92
+ section: "API Reference"
93
+ sectionOrder: 1
94
+ ---
95
+ ```
96
+
97
+ Pages without a `section` field stay at the root URL under the default "Docs" tab.
98
+
99
+ ### Via `sections.json`
100
+
101
+ For full control, create a `sections.json` file in your project root:
102
+
103
+ ```json
104
+ [
105
+ { "label": "Docs", "slug": "" },
106
+ { "label": "API Reference", "slug": "api" },
107
+ { "label": "SDKs", "slug": "sdks" }
108
+ ]
109
+ ```
110
+
111
+ Each entry has:
112
+
113
+ - `label` - display name shown in the section bar
114
+ - `slug` - URL prefix for this section (use `""` for the root section)
115
+ - `directory` (optional) - subdirectory containing this section's MDX files, only needed when the directory name differs from the slug
116
+
117
+ ```json
118
+ [
119
+ { "label": "Guides", "slug": "", "directory": "guides" },
120
+ { "label": "API Reference", "slug": "api", "directory": "api-reference" }
121
+ ]
122
+ ```
123
+
67
124
  ## Configuration Files
68
125
 
69
126
  Place these JSON files in your project root (where you run `doccupine`). They are auto-copied to the generated app and watched for changes.
@@ -76,6 +133,11 @@ Place these JSON files in your project root (where you run `doccupine`). They ar
76
133
  | `navigation.json` | Manual navigation structure (overrides auto-generated) |
77
134
  | `links.json` | Static header/footer links |
78
135
  | `fonts.json` | Font configuration (Google Fonts or local) |
136
+ | `sections.json` | Section definitions for tabbed doc groups (see [Sections](#sections)) |
137
+
138
+ ## Public Directory
139
+
140
+ Place static assets (images, favicons, `robots.txt`, etc.) in a `public/` directory at your project root. Doccupine copies it to the generated Next.js app on startup and watches for changes, so added, modified, or deleted files are synced automatically.
79
141
 
80
142
  ## AI Chat Setup
81
143
 
@@ -83,13 +145,33 @@ The generated app includes an AI chat assistant. To enable it, create a `.env.lo
83
145
 
84
146
  ```env
85
147
  LLM_PROVIDER=openai # openai | anthropic | google
148
+
149
+ # API Keys (set the one matching your provider)
86
150
  OPENAI_API_KEY=sk-...
87
- # Or: ANTHROPIC_API_KEY=sk-ant-...
88
- # Or: GOOGLE_API_KEY=...
151
+ ANTHROPIC_API_KEY=sk-ant-...
152
+ GOOGLE_API_KEY=...
89
153
  ```
90
154
 
91
155
  If `LLM_PROVIDER` is not set, the chat component is hidden automatically.
92
156
 
157
+ > **Note:** Anthropic does not provide an embeddings API. When using `anthropic` as your provider, you must also set `OPENAI_API_KEY` for embeddings to work.
158
+
159
+ ### Optional overrides
160
+
161
+ ```env
162
+ LLM_CHAT_MODEL=gpt-4.1-nano # Override the default chat model
163
+ LLM_EMBEDDING_MODEL=text-embedding-3-small # Override the default embedding model
164
+ LLM_TEMPERATURE=0 # Set temperature (0-1, default: 0)
165
+ ```
166
+
167
+ Default models per provider:
168
+
169
+ | Provider | Chat model | Embedding model |
170
+ | --------- | ---------------------------- | ------------------------ |
171
+ | OpenAI | `gpt-4.1-nano` | `text-embedding-3-small` |
172
+ | Anthropic | `claude-sonnet-4-5-20250929` | OpenAI fallback |
173
+ | Google | `gemini-2.5-flash-lite` | `text-embedding-004` |
174
+
93
175
  ## MCP Server
94
176
 
95
177
  The generated app exposes an MCP endpoint at `/api/mcp` with three tools:
package/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  #!/usr/bin/env node
2
2
  export declare function generateSlug(filePath: string): string;
3
+ export declare function getFullSlug(pageSlug: string, sectionSlug: string): string;
3
4
  export declare function escapeTemplateContent(content: string): string;
package/dist/index.js CHANGED
@@ -27,7 +27,9 @@ import { clickOutsideTemplate } from "./templates/components/ClickOutside.js";
27
27
  import { docsTemplate } from "./templates/components/Docs.js";
28
28
  import { docsSideBarTemplate } from "./templates/components/DocsSideBar.js";
29
29
  import { mdxComponentsTemplate } from "./templates/components/MDXComponents.js";
30
+ import { sectionNavProviderTemplate } from "./templates/components/SectionNavProvider.js";
30
31
  import { sideBarTemplate } from "./templates/components/SideBar.js";
32
+ import { sectionBarTemplate } from "./templates/components/layout/SectionBar.js";
31
33
  import { accordionTemplate } from "./templates/components/layout/Accordion.js";
32
34
  import { actionBarTemplate } from "./templates/components/layout/ActionBar.js";
33
35
  import { buttonTemplate } from "./templates/components/layout/Button.js";
@@ -82,11 +84,12 @@ import { headersAndTextMdxTemplate } from "./templates/mdx/headers-and-text.mdx.
82
84
  import { iconsMdxTemplate } from "./templates/mdx/icons.mdx.js";
83
85
  import { imageAndEmbedsMdxTemplate } from "./templates/mdx/image-and-embeds.mdx.js";
84
86
  import { indexMdxTemplate } from "./templates/mdx/index.mdx.js";
85
- import { linksMdxTemplate } from "./templates/mdx/links.mdx.js";
87
+ import { footerLinksMdxTemplate } from "./templates/mdx/footer-links.mdx.js";
86
88
  import { listAndTablesMdxTemplate } from "./templates/mdx/list-and-tables.mdx.js";
87
89
  import { mediaAndAssetsMdxTemplate } from "./templates/mdx/media-and-assets.mdx.js";
88
90
  import { mcpMdxTemplate } from "./templates/mdx/model-context-protocol.mdx.js";
89
91
  import { navigationMdxTemplate } from "./templates/mdx/navigation.mdx.js";
92
+ import { sectionsMdxTemplate } from "./templates/mdx/sections.mdx.js";
90
93
  import { stepsMdxTemplate } from "./templates/mdx/steps.mdx.js";
91
94
  import { tabsMdxTemplate } from "./templates/mdx/tabs.mdx.js";
92
95
  import { themeMdxTemplate } from "./templates/mdx/theme.mdx.js";
@@ -107,11 +110,23 @@ export function generateSlug(filePath) {
107
110
  if (filePath === "index.mdx" || filePath === "./index.mdx") {
108
111
  return "";
109
112
  }
110
- return filePath
113
+ const normalized = filePath
111
114
  .replace(/\.mdx$/, "")
112
115
  .replace(/\\/g, "/")
113
116
  .replace(/[^a-zA-Z0-9\/\-_]/g, "-")
114
117
  .toLowerCase();
118
+ // Strip trailing /index for subdirectory index files
119
+ if (normalized.endsWith("/index")) {
120
+ return normalized.slice(0, -"/index".length);
121
+ }
122
+ return normalized;
123
+ }
124
+ export function getFullSlug(pageSlug, sectionSlug) {
125
+ if (!sectionSlug)
126
+ return pageSlug;
127
+ if (pageSlug === "")
128
+ return sectionSlug;
129
+ return `${sectionSlug}/${pageSlug}`;
115
130
  }
116
131
  export function escapeTemplateContent(content) {
117
132
  return content
@@ -207,8 +222,12 @@ class MDXToNextJSGenerator {
207
222
  "navigation.json",
208
223
  "config.json",
209
224
  "links.json",
225
+ "sections.json",
210
226
  ];
211
227
  fontConfigFile = "fonts.json";
228
+ sectionsConfig = null;
229
+ /** Guards against recursive reprocessing when maybeUpdateSections() triggers processAllMDXFiles() */
230
+ isReprocessing = false;
212
231
  constructor(watchDir, outputDir) {
213
232
  this.watchDir = path.resolve(watchDir);
214
233
  this.outputDir = path.resolve(outputDir);
@@ -218,12 +237,17 @@ class MDXToNextJSGenerator {
218
237
  console.log(chalk.blue("🚀 Initializing MDX to Next.js generator..."));
219
238
  await fs.ensureDir(this.watchDir);
220
239
  await fs.ensureDir(this.outputDir);
240
+ this.sectionsConfig = await this.resolveSections();
241
+ if (this.sectionsConfig) {
242
+ console.log(chalk.blue(`📑 Found ${this.sectionsConfig.length} section(s): ${this.sectionsConfig.map((s) => s.label).join(", ")}`));
243
+ }
221
244
  await this.createNextJSStructure();
222
245
  await this.createStartingDocs();
223
246
  await this.copyCustomConfigFiles();
224
247
  await this.copyFontConfig();
225
248
  await this.copyPublicFiles();
226
249
  await this.processAllMDXFiles();
250
+ await this.generateSectionIndexPages();
227
251
  console.log(chalk.green("✅ Initial setup complete!"));
228
252
  console.log(chalk.cyan("💡 To start the Next.js dev server:"));
229
253
  console.log(chalk.white(` cd ${path.relative(process.cwd(), this.outputDir)}`));
@@ -239,6 +263,7 @@ class MDXToNextJSGenerator {
239
263
  "eslint.config.mjs": eslintConfigTemplate,
240
264
  "links.json": `[]`,
241
265
  "navigation.json": `[]`,
266
+ "sections.json": `[]`,
242
267
  "next.config.ts": nextConfigTemplate,
243
268
  "package.json": packageJsonTemplate,
244
269
  "proxy.ts": proxyTemplate,
@@ -268,6 +293,7 @@ class MDXToNextJSGenerator {
268
293
  "components/Docs.tsx": docsTemplate,
269
294
  "components/DocsSideBar.tsx": docsSideBarTemplate,
270
295
  "components/MDXComponents.tsx": mdxComponentsTemplate,
296
+ "components/SectionNavProvider.tsx": sectionNavProviderTemplate,
271
297
  "components/SideBar.tsx": sideBarTemplate,
272
298
  "components/layout/Accordion.tsx": accordionTemplate,
273
299
  "components/layout/ActionBar.tsx": actionBarTemplate,
@@ -281,6 +307,7 @@ class MDXToNextJSGenerator {
281
307
  "components/layout/DemoTheme.tsx": demoThemeTemplate,
282
308
  "components/layout/DocsComponents.tsx": docsComponentsTemplate,
283
309
  "components/layout/DocsNavigation.tsx": docsNavigationTemplate,
310
+ "components/layout/SectionBar.tsx": sectionBarTemplate,
284
311
  "components/layout/Field.tsx": fieldTemplate,
285
312
  "components/layout/Footer.tsx": footerTemplate,
286
313
  "components/layout/GlobalStyles.ts": globalStylesTemplate,
@@ -319,11 +346,12 @@ class MDXToNextJSGenerator {
319
346
  "icons.mdx": iconsMdxTemplate,
320
347
  "image-and-embeds.mdx": imageAndEmbedsMdxTemplate,
321
348
  "index.mdx": indexMdxTemplate,
322
- "links.mdx": linksMdxTemplate,
349
+ "footer-links.mdx": footerLinksMdxTemplate,
323
350
  "lists-and-tables.mdx": listAndTablesMdxTemplate,
324
351
  "media-and-assets.mdx": mediaAndAssetsMdxTemplate,
325
352
  "model-context-protocol.mdx": mcpMdxTemplate,
326
353
  "navigation.mdx": navigationMdxTemplate,
354
+ "sections.mdx": sectionsMdxTemplate,
327
355
  "steps.mdx": stepsMdxTemplate,
328
356
  "tabs.mdx": tabsMdxTemplate,
329
357
  "theme.mdx": themeMdxTemplate,
@@ -378,6 +406,152 @@ class MDXToNextJSGenerator {
378
406
  }
379
407
  return null;
380
408
  }
409
+ async loadSectionsConfig() {
410
+ const sectionsPath = path.join(this.rootDir, "sections.json");
411
+ try {
412
+ if (await fs.pathExists(sectionsPath)) {
413
+ const content = await fs.readFile(sectionsPath, "utf8");
414
+ const parsed = JSON.parse(content);
415
+ if (Array.isArray(parsed) && parsed.length > 0) {
416
+ return parsed;
417
+ }
418
+ }
419
+ }
420
+ catch (error) {
421
+ console.warn(chalk.yellow("⚠️ Error reading sections.json"), error);
422
+ }
423
+ return null;
424
+ }
425
+ async discoverSectionsFromFrontmatter() {
426
+ const files = await this.getAllMDXFiles();
427
+ const sectionMap = new Map();
428
+ let hasUnsectionedPages = false;
429
+ let defaultSectionLabel = "Docs";
430
+ for (const file of files) {
431
+ const fullPath = path.join(this.watchDir, file);
432
+ const content = await fs.readFile(fullPath, "utf8");
433
+ const { data: frontmatter } = matter(content);
434
+ if (frontmatter.section) {
435
+ const label = frontmatter.section;
436
+ const order = frontmatter.sectionOrder || 0;
437
+ const existing = sectionMap.get(label);
438
+ if (!existing || order < existing.order) {
439
+ sectionMap.set(label, { label, order });
440
+ }
441
+ }
442
+ else {
443
+ hasUnsectionedPages = true;
444
+ }
445
+ if ((file === "index.mdx" || file === "./index.mdx") &&
446
+ frontmatter.sectionLabel) {
447
+ defaultSectionLabel = frontmatter.sectionLabel;
448
+ }
449
+ }
450
+ if (sectionMap.size === 0)
451
+ return null;
452
+ const sorted = [...sectionMap.values()].sort((a, b) => a.order - b.order);
453
+ const sections = [];
454
+ // Implicit root entry for pages without a section field
455
+ if (hasUnsectionedPages) {
456
+ sections.push({ label: defaultSectionLabel, slug: "" });
457
+ }
458
+ for (const s of sorted) {
459
+ sections.push({
460
+ label: s.label,
461
+ slug: s.label.toLowerCase().replace(/\s+/g, "-"),
462
+ });
463
+ }
464
+ return sections;
465
+ }
466
+ async resolveSections() {
467
+ const fromFile = await this.loadSectionsConfig();
468
+ if (fromFile)
469
+ return fromFile;
470
+ return this.discoverSectionsFromFrontmatter();
471
+ }
472
+ async reloadSections() {
473
+ console.log(chalk.cyan("📑 Sections configuration changed"));
474
+ this.sectionsConfig = await this.resolveSections();
475
+ await this.processAllMDXFiles();
476
+ await this.generateSectionIndexPages();
477
+ }
478
+ async maybeUpdateSections() {
479
+ if (this.isReprocessing)
480
+ return;
481
+ // Skip if sections.json exists (explicit config takes priority)
482
+ const fromFile = await this.loadSectionsConfig();
483
+ if (fromFile)
484
+ return;
485
+ const newSections = await this.discoverSectionsFromFrontmatter();
486
+ const changed = JSON.stringify(newSections) !== JSON.stringify(this.sectionsConfig);
487
+ if (changed) {
488
+ console.log(chalk.cyan(newSections
489
+ ? `📑 Sections updated from frontmatter: ${newSections.map((s) => s.label).join(", ")}`
490
+ : "📑 Sections cleared (no section frontmatter found)"));
491
+ this.sectionsConfig = newSections;
492
+ this.isReprocessing = true;
493
+ try {
494
+ await this.processAllMDXFiles();
495
+ await this.generateSectionIndexPages();
496
+ }
497
+ finally {
498
+ this.isReprocessing = false;
499
+ }
500
+ }
501
+ }
502
+ determineSectionForFile(filePath, frontmatter) {
503
+ if (!this.sectionsConfig || this.sectionsConfig.length === 0) {
504
+ return { sectionSlug: "", pageSlug: generateSlug(filePath) };
505
+ }
506
+ const normalizedPath = filePath.replace(/\\/g, "/");
507
+ const firstDir = normalizedPath.includes("/")
508
+ ? normalizedPath.split("/")[0]
509
+ : "";
510
+ // Explicit directory matching (entries with a directory field)
511
+ for (const section of this.sectionsConfig) {
512
+ if (!section.directory)
513
+ continue;
514
+ const dirPrefix = section.directory + "/";
515
+ if (normalizedPath.startsWith(dirPrefix)) {
516
+ return {
517
+ sectionSlug: section.slug,
518
+ pageSlug: generateSlug(normalizedPath.slice(dirPrefix.length)),
519
+ };
520
+ }
521
+ }
522
+ // Directory matches section slug (auto-detect)
523
+ if (firstDir) {
524
+ const match = this.sectionsConfig.find((s) => s.slug === firstDir);
525
+ if (match) {
526
+ const pathForSlug = normalizedPath.slice(firstDir.length + 1);
527
+ return {
528
+ sectionSlug: match.slug,
529
+ pageSlug: generateSlug(pathForSlug),
530
+ };
531
+ }
532
+ }
533
+ // Frontmatter section field
534
+ if (frontmatter.section) {
535
+ const label = frontmatter.section;
536
+ const match = this.sectionsConfig.find((s) => s.label === label);
537
+ if (match) {
538
+ // Strip the directory if it matches the section slug
539
+ let pathForSlug = filePath;
540
+ if (firstDir && firstDir === match.slug) {
541
+ pathForSlug = normalizedPath.slice(firstDir.length + 1);
542
+ }
543
+ return {
544
+ sectionSlug: match.slug,
545
+ pageSlug: generateSlug(pathForSlug),
546
+ };
547
+ }
548
+ }
549
+ // No section match - page stays at root
550
+ return {
551
+ sectionSlug: "",
552
+ pageSlug: generateSlug(filePath),
553
+ };
554
+ }
381
555
  async handleConfigFileChange(filePath) {
382
556
  const fileName = path.basename(filePath);
383
557
  if (this.configFiles.includes(fileName)) {
@@ -386,6 +560,9 @@ class MDXToNextJSGenerator {
386
560
  try {
387
561
  await fs.copy(sourcePath, destPath);
388
562
  console.log(chalk.green(`📋 Updated ${fileName} in Next.js app`));
563
+ if (fileName === "sections.json") {
564
+ await this.reloadSections();
565
+ }
389
566
  }
390
567
  catch (error) {
391
568
  console.error(chalk.red(`❌ Error copying ${fileName}:`), error);
@@ -401,6 +578,9 @@ class MDXToNextJSGenerator {
401
578
  await fs.remove(destPath);
402
579
  console.log(chalk.yellow(`🗑️ Removed ${fileName} from Next.js app`));
403
580
  }
581
+ if (fileName === "sections.json") {
582
+ await this.reloadSections();
583
+ }
404
584
  }
405
585
  catch (error) {
406
586
  console.error(chalk.red(`❌ Error removing ${fileName}:`), error);
@@ -579,8 +759,10 @@ class MDXToNextJSGenerator {
579
759
  const fullPath = path.join(this.watchDir, file);
580
760
  const content = await fs.readFile(fullPath, "utf8");
581
761
  const { data: frontmatter } = matter(content);
762
+ const { sectionSlug, pageSlug } = this.determineSectionForFile(file, frontmatter);
763
+ const fullSlug = getFullSlug(pageSlug, sectionSlug);
582
764
  return {
583
- slug: this.generateSlug(file),
765
+ slug: fullSlug,
584
766
  title: frontmatter.title || "Untitled",
585
767
  description: frontmatter.description || "",
586
768
  date: frontmatter.date || null,
@@ -588,6 +770,7 @@ class MDXToNextJSGenerator {
588
770
  path: file,
589
771
  categoryOrder: frontmatter.categoryOrder || 0,
590
772
  order: frontmatter.order || 0,
773
+ section: sectionSlug,
591
774
  };
592
775
  }
593
776
  async buildAllPagesMeta() {
@@ -600,7 +783,11 @@ class MDXToNextJSGenerator {
600
783
  try {
601
784
  const content = await fs.readFile(fullPath, "utf8");
602
785
  const { data: frontmatter, content: mdxContent } = matter(content);
603
- if (filePath === "index.mdx" || filePath === "./index.mdx") {
786
+ const { sectionSlug, pageSlug } = this.determineSectionForFile(filePath, frontmatter);
787
+ const fullSlug = getFullSlug(pageSlug, sectionSlug);
788
+ const isIndex = filePath === "index.mdx" || filePath === "./index.mdx";
789
+ const isSectionIndex = this.sectionsConfig && pageSlug === "" && sectionSlug !== "";
790
+ if (isIndex) {
604
791
  console.log(chalk.blue("🏠 Updating homepage with index.mdx content"));
605
792
  }
606
793
  else {
@@ -608,13 +795,18 @@ class MDXToNextJSGenerator {
608
795
  path: filePath,
609
796
  content: mdxContent,
610
797
  frontmatter,
611
- slug: this.generateSlug(filePath),
798
+ slug: fullSlug,
612
799
  };
613
800
  await this.generatePageFromMDX(mdxFile);
614
801
  }
802
+ if (isSectionIndex) {
803
+ await this.updateSectionIndex(sectionSlug, frontmatter, mdxContent);
804
+ }
615
805
  await this.updatePagesIndex();
616
806
  await this.updateRootLayout();
807
+ await this.generateSectionIndexPages();
617
808
  console.log(chalk.green(`✅ Generated page for: ${filePath}`));
809
+ await this.maybeUpdateSections();
618
810
  }
619
811
  catch (error) {
620
812
  console.error(chalk.red(`❌ Error processing ${filePath}:`), error);
@@ -627,13 +819,16 @@ class MDXToNextJSGenerator {
627
819
  console.log(chalk.blue("🏠 Updating homepage - index.mdx deleted"));
628
820
  }
629
821
  else {
630
- const slug = this.generateSlug(filePath);
631
- const pagePath = path.join(this.outputDir, "app", slug);
822
+ // We don't have frontmatter for deleted files, so use directory-based matching
823
+ const { sectionSlug, pageSlug } = this.determineSectionForFile(filePath, {});
824
+ const fullSlug = getFullSlug(pageSlug, sectionSlug);
825
+ const pagePath = path.join(this.outputDir, "app", fullSlug);
632
826
  await fs.remove(pagePath);
633
827
  }
634
828
  await this.updatePagesIndex();
635
829
  await this.updateRootLayout();
636
830
  console.log(chalk.green(`✅ Removed page for: ${filePath}`));
831
+ await this.maybeUpdateSections();
637
832
  }
638
833
  catch (error) {
639
834
  console.error(chalk.red(`❌ Error removing page for ${filePath}:`), error);
@@ -663,13 +858,44 @@ class MDXToNextJSGenerator {
663
858
  await scanDir(this.watchDir);
664
859
  return files;
665
860
  }
666
- generateSlug(filePath) {
667
- return generateSlug(filePath);
668
- }
669
861
  async generateRootLayout() {
670
862
  const pages = await this.buildAllPagesMeta();
671
863
  const fontConfig = await this.loadFontConfig();
672
- return layoutTemplate(pages, fontConfig);
864
+ return layoutTemplate(pages, fontConfig, this.sectionsConfig);
865
+ }
866
+ async generateSectionIndexPages() {
867
+ if (!this.sectionsConfig || this.sectionsConfig.length === 0)
868
+ return;
869
+ const pages = await this.buildAllPagesMeta();
870
+ for (const section of this.sectionsConfig) {
871
+ if (section.slug === "")
872
+ continue;
873
+ // Check if a page already exists at the section root
874
+ const hasIndex = pages.some((p) => p.slug === section.slug);
875
+ if (hasIndex)
876
+ continue;
877
+ // Find the first page in this section
878
+ const sectionPages = pages
879
+ .filter((p) => p.section === section.slug)
880
+ .sort((a, b) => {
881
+ if (a.categoryOrder !== b.categoryOrder)
882
+ return a.categoryOrder - b.categoryOrder;
883
+ return a.order - b.order;
884
+ });
885
+ if (sectionPages.length === 0)
886
+ continue;
887
+ const firstPage = sectionPages[0];
888
+ const redirectContent = `import { redirect } from "next/navigation";
889
+
890
+ export default function SectionIndex() {
891
+ redirect("/${firstPage.slug}");
892
+ }
893
+ `;
894
+ const pagePath = path.join(this.outputDir, "app", section.slug, "page.tsx");
895
+ await fs.ensureDir(path.dirname(pagePath));
896
+ await fs.writeFile(pagePath, redirectContent, "utf8");
897
+ console.log(chalk.blue(`🔀 Generated section index redirect: /${section.slug} -> /${firstPage.slug}`));
898
+ }
673
899
  }
674
900
  async generatePageFromMDX(mdxFile) {
675
901
  const pageContent = `import { Metadata } from "next";
@@ -749,6 +975,32 @@ export default function Home() {
749
975
  `;
750
976
  await fs.writeFile(path.join(this.outputDir, "app", "page.tsx"), indexContent, "utf8");
751
977
  }
978
+ async updateSectionIndex(sectionSlug, frontmatter, mdxContent) {
979
+ const indexContent = `import { Metadata } from "next";
980
+ import { Docs } from "@/components/Docs";
981
+ import { config } from "@/utils/config";
982
+
983
+ const content = \`${escapeTemplateContent(mdxContent)}\`;
984
+
985
+ export const metadata: Metadata = {
986
+ title: \`\${config.name ? config.name + " -" : "Doccupine -"} ${frontmatter.title || "Section"}\`,
987
+ description: \`${frontmatter.description ? frontmatter.description : '${config.description ? config.description : "Generated with Doccupine"}'}\`,
988
+ icons: \`${frontmatter.icon ? frontmatter.icon : '\${config.icon || "https://doccupine.com/favicon.ico"}'}\`,
989
+ openGraph: {
990
+ title: \`\${config.name ? config.name + " -" : "Doccupine -"} ${frontmatter.title || "Section"}\`,
991
+ description: \`${frontmatter.description ? frontmatter.description : '${config.description ? config.description : "Generated with Doccupine"}'}\`,
992
+ images: \`${frontmatter.image ? frontmatter.image : '\${config.preview || "https://doccupine.com/preview.png"}'}\`,
993
+ },
994
+ };
995
+
996
+ export default function Page() {
997
+ return <Docs content={content} />;
998
+ }
999
+ `;
1000
+ const pagePath = path.join(this.outputDir, "app", sectionSlug, "page.tsx");
1001
+ await fs.ensureDir(path.dirname(pagePath));
1002
+ await fs.writeFile(pagePath, indexContent, "utf8");
1003
+ }
752
1004
  async updateRootLayout() {
753
1005
  const layoutContent = await this.generateRootLayout();
754
1006
  await fs.writeFile(path.join(this.outputDir, "app", "layout.tsx"), layoutContent, "utf8");
@@ -1 +1,34 @@
1
- export declare const layoutTemplate: (pages: any[], fontConfig: any) => string;
1
+ interface SectionConfig {
2
+ label: string;
3
+ slug: string;
4
+ directory?: string;
5
+ }
6
+ interface PageData {
7
+ slug: string;
8
+ title: string;
9
+ description: string;
10
+ date: string | null;
11
+ category: string;
12
+ path: string;
13
+ categoryOrder: number;
14
+ order: number;
15
+ section: string;
16
+ }
17
+ interface GoogleFontConfig {
18
+ fontName?: string;
19
+ subsets?: string[];
20
+ weight?: string | string[];
21
+ }
22
+ interface LocalFontSrc {
23
+ path: string;
24
+ weight: string;
25
+ style: string;
26
+ }
27
+ interface FontConfig {
28
+ googleFont?: GoogleFontConfig;
29
+ localFonts?: string | {
30
+ src?: LocalFontSrc[];
31
+ };
32
+ }
33
+ export declare const layoutTemplate: (pages: PageData[], fontConfig: FontConfig | null, sectionsConfig?: SectionConfig[] | null) => string;
34
+ export {};