doccupine 0.0.133 → 0.0.134
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +40 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +274 -19
- package/dist/lib/config-manager.d.ts +8 -1
- package/dist/lib/config-manager.js +98 -4
- package/dist/lib/openapi-types.d.ts +120 -0
- package/dist/lib/openapi-types.js +14 -0
- package/dist/lib/openapi.d.ts +52 -0
- package/dist/lib/openapi.js +603 -0
- package/dist/lib/structures.js +28 -2
- package/dist/lib/types.d.ts +22 -0
- package/dist/lib/utils.d.ts +8 -0
- package/dist/lib/utils.js +14 -0
- package/dist/templates/app/api/playground/route.d.ts +1 -0
- package/dist/templates/app/api/playground/route.js +199 -0
- package/dist/templates/components/Docs.d.ts +1 -1
- package/dist/templates/components/Docs.js +11 -2
- package/dist/templates/components/SideBar.d.ts +1 -1
- package/dist/templates/components/SideBar.js +10 -0
- package/dist/templates/components/layout/Accordion.d.ts +1 -1
- package/dist/templates/components/layout/Accordion.js +3 -2
- package/dist/templates/components/layout/ActionBar.d.ts +1 -1
- package/dist/templates/components/layout/ActionBar.js +21 -14
- package/dist/templates/components/layout/ApiPlayground.d.ts +1 -0
- package/dist/templates/components/layout/ApiPlayground.js +1083 -0
- package/dist/templates/components/layout/ApiPlaygroundDemo.d.ts +1 -0
- package/dist/templates/components/layout/ApiPlaygroundDemo.js +81 -0
- package/dist/templates/components/layout/Code.d.ts +1 -1
- package/dist/templates/components/layout/Code.js +10 -22
- package/dist/templates/components/layout/CopyButton.d.ts +1 -0
- package/dist/templates/components/layout/CopyButton.js +68 -0
- package/dist/templates/components/layout/DocsComponents.d.ts +1 -1
- package/dist/templates/components/layout/DocsComponents.js +39 -0
- package/dist/templates/components/layout/Mermaid.d.ts +1 -1
- package/dist/templates/components/layout/Mermaid.js +21 -3
- package/dist/templates/components/layout/SharedStyles.d.ts +1 -1
- package/dist/templates/components/layout/SharedStyles.js +0 -4
- package/dist/templates/components/layout/Steps.d.ts +1 -1
- package/dist/templates/components/layout/Steps.js +0 -1
- package/dist/templates/components/layout/Update.d.ts +1 -1
- package/dist/templates/components/layout/Update.js +2 -1
- package/dist/templates/mdx/accordion.mdx.d.ts +1 -1
- package/dist/templates/mdx/accordion.mdx.js +19 -1
- package/dist/templates/mdx/ai-assistant.mdx.d.ts +1 -1
- package/dist/templates/mdx/ai-assistant.mdx.js +3 -2
- package/dist/templates/mdx/analytics.mdx.d.ts +1 -1
- package/dist/templates/mdx/analytics.mdx.js +2 -2
- package/dist/templates/mdx/api-playground.mdx.d.ts +1 -0
- package/dist/templates/mdx/api-playground.mdx.js +158 -0
- package/dist/templates/mdx/authentication.mdx.d.ts +1 -1
- package/dist/templates/mdx/authentication.mdx.js +3 -3
- package/dist/templates/mdx/buttons.mdx.d.ts +1 -1
- package/dist/templates/mdx/buttons.mdx.js +1 -1
- package/dist/templates/mdx/callouts.mdx.d.ts +1 -1
- package/dist/templates/mdx/callouts.mdx.js +1 -1
- package/dist/templates/mdx/cards.mdx.d.ts +1 -1
- package/dist/templates/mdx/cards.mdx.js +1 -1
- package/dist/templates/mdx/color-swatches.mdx.d.ts +1 -1
- package/dist/templates/mdx/color-swatches.mdx.js +1 -1
- package/dist/templates/mdx/commands.mdx.d.ts +1 -1
- package/dist/templates/mdx/commands.mdx.js +3 -2
- package/dist/templates/mdx/components.mdx.d.ts +1 -1
- package/dist/templates/mdx/components.mdx.js +12 -9
- package/dist/templates/mdx/deployment-and-hosting.mdx.d.ts +1 -1
- package/dist/templates/mdx/deployment-and-hosting.mdx.js +4 -3
- package/dist/templates/mdx/fields.mdx.d.ts +1 -1
- package/dist/templates/mdx/fields.mdx.js +1 -1
- package/dist/templates/mdx/fonts.mdx.d.ts +1 -1
- package/dist/templates/mdx/fonts.mdx.js +2 -2
- package/dist/templates/mdx/footer-links.mdx.d.ts +1 -1
- package/dist/templates/mdx/footer-links.mdx.js +1 -1
- package/dist/templates/mdx/frames.mdx.d.ts +1 -1
- package/dist/templates/mdx/frames.mdx.js +1 -1
- package/dist/templates/mdx/globals.mdx.d.ts +1 -1
- package/dist/templates/mdx/globals.mdx.js +1 -1
- package/dist/templates/mdx/icons.mdx.d.ts +1 -1
- package/dist/templates/mdx/icons.mdx.js +1 -1
- package/dist/templates/mdx/image-and-embeds.mdx.d.ts +1 -1
- package/dist/templates/mdx/image-and-embeds.mdx.js +1 -1
- package/dist/templates/mdx/media-and-assets.mdx.d.ts +1 -1
- package/dist/templates/mdx/media-and-assets.mdx.js +2 -2
- package/dist/templates/mdx/mermaid.mdx.d.ts +1 -1
- package/dist/templates/mdx/mermaid.mdx.js +1 -1
- package/dist/templates/mdx/model-context-protocol.mdx.d.ts +1 -1
- package/dist/templates/mdx/model-context-protocol.mdx.js +2 -2
- package/dist/templates/mdx/navigation.mdx.d.ts +1 -1
- package/dist/templates/mdx/navigation.mdx.js +22 -11
- package/dist/templates/mdx/platform/ai-assistant.mdx.d.ts +1 -1
- package/dist/templates/mdx/platform/ai-assistant.mdx.js +4 -3
- package/dist/templates/mdx/platform/analytics.mdx.d.ts +1 -1
- package/dist/templates/mdx/platform/analytics.mdx.js +2 -2
- package/dist/templates/mdx/platform/api-playground.mdx.d.ts +1 -0
- package/dist/templates/mdx/platform/api-playground.mdx.js +62 -0
- package/dist/templates/mdx/platform/billing.mdx.d.ts +1 -1
- package/dist/templates/mdx/platform/billing.mdx.js +1 -1
- package/dist/templates/mdx/platform/build-and-deploy.mdx.d.ts +1 -1
- package/dist/templates/mdx/platform/build-and-deploy.mdx.js +1 -1
- package/dist/templates/mdx/platform/creating-a-project.mdx.d.ts +1 -1
- package/dist/templates/mdx/platform/creating-a-project.mdx.js +12 -2
- package/dist/templates/mdx/platform/custom-domains.mdx.d.ts +1 -1
- package/dist/templates/mdx/platform/custom-domains.mdx.js +1 -1
- package/dist/templates/mdx/platform/fonts-settings.mdx.d.ts +1 -1
- package/dist/templates/mdx/platform/fonts-settings.mdx.js +1 -1
- package/dist/templates/mdx/platform/footer-links.mdx.d.ts +1 -0
- package/dist/templates/mdx/platform/footer-links.mdx.js +47 -0
- package/dist/templates/mdx/platform/import-from-github.mdx.d.ts +1 -0
- package/dist/templates/mdx/platform/import-from-github.mdx.js +91 -0
- package/dist/templates/mdx/platform/mcp.mdx.d.ts +1 -1
- package/dist/templates/mdx/platform/mcp.mdx.js +3 -3
- package/dist/templates/mdx/platform/project-settings.mdx.d.ts +1 -1
- package/dist/templates/mdx/platform/project-settings.mdx.js +1 -1
- package/dist/templates/mdx/platform/self-updating-docs.mdx.d.ts +1 -0
- package/dist/templates/mdx/platform/self-updating-docs.mdx.js +113 -0
- package/dist/templates/mdx/platform/team-members.mdx.d.ts +1 -1
- package/dist/templates/mdx/platform/team-members.mdx.js +1 -1
- package/dist/templates/mdx/sections.mdx.d.ts +1 -1
- package/dist/templates/mdx/sections.mdx.js +1 -1
- package/dist/templates/mdx/steps.mdx.d.ts +1 -1
- package/dist/templates/mdx/steps.mdx.js +1 -1
- package/dist/templates/mdx/tabs.mdx.d.ts +1 -1
- package/dist/templates/mdx/tabs.mdx.js +1 -1
- package/dist/templates/mdx/theme.mdx.d.ts +1 -1
- package/dist/templates/mdx/theme.mdx.js +1 -1
- package/dist/templates/mdx/update.mdx.d.ts +1 -1
- package/dist/templates/mdx/update.mdx.js +1 -1
- package/dist/templates/mdx/what-is-doccupine.mdx.d.ts +1 -1
- package/dist/templates/mdx/what-is-doccupine.mdx.js +1 -1
- package/dist/templates/package.js +6 -6
- package/dist/templates/services/openapi/playgroundAllowlistStub.d.ts +1 -0
- package/dist/templates/services/openapi/playgroundAllowlistStub.js +4 -0
- package/dist/templates/types/openapi.d.ts +1 -0
- package/dist/templates/types/openapi.js +93 -0
- package/dist/templates/utils/apiAnchors.d.ts +1 -0
- package/dist/templates/utils/apiAnchors.js +39 -0
- package/dist/templates/utils/apiSnippets.d.ts +1 -0
- package/dist/templates/utils/apiSnippets.js +94 -0
- package/dist/templates/utils/orderNavItems.d.ts +1 -1
- package/dist/templates/utils/orderNavItems.js +3 -0
- package/dist/templates/utils/playgroundAllowlist.d.ts +1 -0
- package/dist/templates/utils/playgroundAllowlist.js +70 -0
- package/dist/templates/utils/ssrfGuard.d.ts +1 -0
- package/dist/templates/utils/ssrfGuard.js +217 -0
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -9,6 +9,7 @@
|
|
|
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
11
|
- **Sections** - organize docs into tabbed sections via frontmatter or `sections.json`
|
|
12
|
+
- **API reference** - point at an OpenAPI spec to generate interactive endpoint pages with a live playground
|
|
12
13
|
- **Theming** - dark/light mode with customizable theme via `theme.json`
|
|
13
14
|
- **AI chat assistant** - built-in RAG-powered chat (OpenAI, Anthropic, or Google)
|
|
14
15
|
- **MCP server** - exposes `search_docs`, `get_doc`, and `list_docs` tools for AI agents
|
|
@@ -29,6 +30,7 @@ Doccupine will prompt you for:
|
|
|
29
30
|
|
|
30
31
|
1. A directory to store your MDX files (default: `docs`)
|
|
31
32
|
2. An output directory for the generated Next.js app (default: `nextjs-app`)
|
|
33
|
+
3. An optional path to an OpenAPI spec for an API reference (blank to skip)
|
|
32
34
|
|
|
33
35
|
It then scaffolds the app, installs dependencies, and starts the dev server. Open http://localhost:3000 to view your docs.
|
|
34
36
|
|
|
@@ -85,6 +87,7 @@ date: "2025-01-01" # Page date metadata
|
|
|
85
87
|
updated: "2025-02-01" # Last-modified date (JSON-LD dateModified)
|
|
86
88
|
section: "API Reference" # Section this page belongs to
|
|
87
89
|
sectionOrder: 1 # Sort order for the section in the tab bar
|
|
90
|
+
openapi: "GET /users/{id}" # Embed an API playground for this endpoint (see API Reference)
|
|
88
91
|
---
|
|
89
92
|
```
|
|
90
93
|
|
|
@@ -142,13 +145,45 @@ Each entry has:
|
|
|
142
145
|
]
|
|
143
146
|
```
|
|
144
147
|
|
|
148
|
+
## API Reference
|
|
149
|
+
|
|
150
|
+
Point Doccupine at an OpenAPI document (`.json`, `.yaml`, or `.yml`, OpenAPI 3.0/3.1) and it generates an interactive API reference: one page per operation, each with a live playground for sending requests. Set the `openapi` field in `doccupine.json`:
|
|
151
|
+
|
|
152
|
+
```json
|
|
153
|
+
{
|
|
154
|
+
"openapi": "openapi.json"
|
|
155
|
+
}
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
The endpoint pages are grouped into their own "API Reference" tab, and your hand-written docs move under a "Documentation" tab. The spec file is watched, so edits regenerate the reference live.
|
|
159
|
+
|
|
160
|
+
You can pass multiple specs as an array of paths, or name them explicitly:
|
|
161
|
+
|
|
162
|
+
```json
|
|
163
|
+
{
|
|
164
|
+
"openapi": [
|
|
165
|
+
{ "name": "Public API", "file": "openapi.json" },
|
|
166
|
+
{ "name": "Admin API", "file": "admin-openapi.yaml" }
|
|
167
|
+
]
|
|
168
|
+
}
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
To embed a single endpoint's playground inline within a hand-written page, add an `openapi` frontmatter field set to `"METHOD /path"` (or the operation's `operationId`):
|
|
172
|
+
|
|
173
|
+
```yaml
|
|
174
|
+
---
|
|
175
|
+
title: "Fetch a User"
|
|
176
|
+
openapi: "GET /users/{id}"
|
|
177
|
+
---
|
|
178
|
+
```
|
|
179
|
+
|
|
145
180
|
## Configuration Files
|
|
146
181
|
|
|
147
182
|
Place these JSON files in your project root (where you run `doccupine`). They are auto-copied to the generated app and watched for changes.
|
|
148
183
|
|
|
149
184
|
| File | Purpose |
|
|
150
185
|
| ----------------- | -------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
151
|
-
| `doccupine.json` | CLI config (watchDir
|
|
186
|
+
| `doccupine.json` | CLI config (`watchDir`, `outputDir`, `port`, `packageManager`, `openapi`). Auto-generated on first run. Paths are stored relative to the project root, so the file is safe to commit. |
|
|
152
187
|
| `config.json` | Site metadata: `name`, `description`, `icon`, `image`, `url` (public site URL for sitemap/robots) |
|
|
153
188
|
| `theme.json` | Theme overrides for [cherry-styled-components](https://github.com/cherry-design-system/styled-components) |
|
|
154
189
|
| `navigation.json` | Manual navigation structure (overrides auto-generated) |
|
|
@@ -211,8 +246,12 @@ If `LLM_PROVIDER` is not set, the chat component is hidden automatically.
|
|
|
211
246
|
LLM_CHAT_MODEL=gpt-4.1-nano # Override the default chat model
|
|
212
247
|
LLM_EMBEDDING_MODEL=text-embedding-3-small # Override the default embedding model
|
|
213
248
|
LLM_TEMPERATURE=0 # Set temperature (0-1, default: 0)
|
|
249
|
+
LLM_EMBEDDING_DIMS=512 # Dimensions for the prebuilt search index (default: 512)
|
|
250
|
+
RAG_RUNTIME_EMBED_MAX_CHUNKS=400 # Max chunks embedded on demand in production (default: 400; 0 requires a prebuilt index)
|
|
214
251
|
```
|
|
215
252
|
|
|
253
|
+
`LLM_EMBEDDING_DIMS` Matryoshka-truncates document vectors so the prebuilt search index stays small; lower values shrink the index at a slight cost to recall. `RAG_RUNTIME_EMBED_MAX_CHUNKS` caps how many chunks the chat will embed on demand in production before requiring a prebuilt index (it's unlimited under `next dev`).
|
|
254
|
+
|
|
216
255
|
Default models per provider:
|
|
217
256
|
|
|
218
257
|
| Provider | Chat model | Embedding model |
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
export { generateSlug, getFullSlug, escapeTemplateContent, } from "./lib/utils.js";
|
|
2
|
+
export { generateSlug, getFullSlug, escapeTemplateContent, toJsStringLiteral, } from "./lib/utils.js";
|
|
3
3
|
/**
|
|
4
4
|
* True when this file is what node was asked to run, rather than a module some
|
|
5
5
|
* other process imported. The test suite imports this file for its exported
|
package/dist/index.js
CHANGED
|
@@ -8,8 +8,9 @@ import matter from "gray-matter";
|
|
|
8
8
|
import chalk from "chalk";
|
|
9
9
|
import { appStructure, obsoleteFiles, startingDocsStructure, } from "./lib/structures.js";
|
|
10
10
|
import { rootLayoutTemplate, siteLayoutTemplate } from "./lib/layout.js";
|
|
11
|
-
import { ConfigManager } from "./lib/config-manager.js";
|
|
12
|
-
import {
|
|
11
|
+
import { ConfigManager, normalizeOpenApiConfig } from "./lib/config-manager.js";
|
|
12
|
+
import { OpenApiRegistry, DEFAULT_API_BASE_SLUG, buildEndpointDoc, } from "./lib/openapi.js";
|
|
13
|
+
import { findAvailablePort, generateSlug, getFullSlug, escapeTemplateContent, toJsStringLiteral, resolvePackageManager, } from "./lib/utils.js";
|
|
13
14
|
import { generateMetadataBlock, generateRuntimeOnlyMetadataBlock, generateJsonLdScript, } from "./lib/metadata.js";
|
|
14
15
|
import { nextConfigTemplate } from "./templates/next.config.js";
|
|
15
16
|
import { pnpmWorkspaceTemplate } from "./templates/pnpmWorkspace.js";
|
|
@@ -19,7 +20,7 @@ import { sitemapTemplate } from "./templates/app/sitemap.js";
|
|
|
19
20
|
import { llmsIndexTemplate } from "./templates/llms/llmsIndex.js";
|
|
20
21
|
import { llmsFullTemplate, } from "./templates/llms/llmsFull.js";
|
|
21
22
|
import { llmsPageTemplate } from "./templates/llms/llmsPage.js";
|
|
22
|
-
export { generateSlug, getFullSlug, escapeTemplateContent, } from "./lib/utils.js";
|
|
23
|
+
export { generateSlug, getFullSlug, escapeTemplateContent, toJsStringLiteral, } from "./lib/utils.js";
|
|
23
24
|
const __filename = fileURLToPath(import.meta.url);
|
|
24
25
|
const __dirname = path.dirname(__filename);
|
|
25
26
|
const packageJson = JSON.parse(fs.readFileSync(path.join(__dirname, "..", "package.json"), "utf8"));
|
|
@@ -34,6 +35,7 @@ class MDXToNextJSGenerator {
|
|
|
34
35
|
publicWatcher = null;
|
|
35
36
|
rootDirWatcher = null;
|
|
36
37
|
analyticsWatcher = null;
|
|
38
|
+
openApiWatcher = null;
|
|
37
39
|
configFiles = [
|
|
38
40
|
"theme.json",
|
|
39
41
|
"navigation.json",
|
|
@@ -49,10 +51,17 @@ class MDXToNextJSGenerator {
|
|
|
49
51
|
isReprocessing = false;
|
|
50
52
|
/** Tracks per-page .md files written under public/ so we can clean up stale ones on rename/delete */
|
|
51
53
|
generatedLlmsPagePaths = new Set();
|
|
52
|
-
|
|
54
|
+
/** OpenAPI specs (build config) that drive the generated API reference. */
|
|
55
|
+
openApiSpecs;
|
|
56
|
+
apiBaseSlug = DEFAULT_API_BASE_SLUG;
|
|
57
|
+
apiRegistry = new OpenApiRegistry();
|
|
58
|
+
/** Route slugs of the endpoint pages written last pass, for stale cleanup. */
|
|
59
|
+
generatedApiPageSlugs = new Set();
|
|
60
|
+
constructor(watchDir, outputDir, openApiSpecs = []) {
|
|
53
61
|
this.watchDir = path.resolve(watchDir);
|
|
54
62
|
this.outputDir = path.resolve(outputDir);
|
|
55
63
|
this.rootDir = process.cwd();
|
|
64
|
+
this.openApiSpecs = openApiSpecs;
|
|
56
65
|
}
|
|
57
66
|
async init() {
|
|
58
67
|
console.log(chalk.blue("🚀 Initializing MDX to Next.js generator..."));
|
|
@@ -63,6 +72,9 @@ class MDXToNextJSGenerator {
|
|
|
63
72
|
if (this.analyticsConfig) {
|
|
64
73
|
console.log(chalk.blue(`📊 Analytics enabled: ${this.analyticsConfig.provider}`));
|
|
65
74
|
}
|
|
75
|
+
// Parse OpenAPI spec(s) before generating structure so the synthetic
|
|
76
|
+
// endpoint pages flow into the very first layout/sitemap/llms pass.
|
|
77
|
+
await this.loadOpenApiRegistry();
|
|
66
78
|
await this.createNextJSStructure();
|
|
67
79
|
await this.createStartingDocs();
|
|
68
80
|
await this.copyCustomConfigFiles();
|
|
@@ -78,6 +90,10 @@ class MDXToNextJSGenerator {
|
|
|
78
90
|
if (this.sectionsConfig) {
|
|
79
91
|
console.log(chalk.blue(`📑 Found ${this.sectionsConfig.length} section(s): ${this.sectionsConfig.map((s) => s.label).join(", ")}`));
|
|
80
92
|
}
|
|
93
|
+
// Write the endpoint pages + request allowlist before the MDX pass so its
|
|
94
|
+
// aggregate refresh (nav/sitemap/llms) already sees them on disk and in the
|
|
95
|
+
// registry.
|
|
96
|
+
await this.writeApiPages();
|
|
81
97
|
await this.processAllMDXFiles();
|
|
82
98
|
console.log(chalk.green("✅ Initial setup complete!"));
|
|
83
99
|
console.log(chalk.cyan("💡 To start the Next.js dev server:"));
|
|
@@ -257,9 +273,28 @@ class MDXToNextJSGenerator {
|
|
|
257
273
|
}
|
|
258
274
|
async resolveSections() {
|
|
259
275
|
const fromFile = await this.loadSectionsConfig();
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
276
|
+
const base = fromFile ?? (await this.discoverSectionsFromFrontmatter());
|
|
277
|
+
return this.withApiReferenceSection(base);
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
280
|
+
* Promotes the generated OpenAPI endpoints into a dedicated "API Reference"
|
|
281
|
+
* section so they get their own top-level nav, separate from hand-written
|
|
282
|
+
* docs. When the site had no sections, a root "Documentation" section is added
|
|
283
|
+
* for the existing pages so both appear in the section switcher.
|
|
284
|
+
*/
|
|
285
|
+
withApiReferenceSection(sections) {
|
|
286
|
+
if (this.apiRegistry.isEmpty)
|
|
287
|
+
return sections;
|
|
288
|
+
const apiSection = {
|
|
289
|
+
label: "API Reference",
|
|
290
|
+
slug: this.apiBaseSlug,
|
|
291
|
+
};
|
|
292
|
+
if (!sections || sections.length === 0) {
|
|
293
|
+
return [{ label: "Documentation", slug: "" }, apiSection];
|
|
294
|
+
}
|
|
295
|
+
if (sections.some((s) => s.slug === this.apiBaseSlug))
|
|
296
|
+
return sections;
|
|
297
|
+
return [...sections, apiSection];
|
|
263
298
|
}
|
|
264
299
|
async reloadSections() {
|
|
265
300
|
console.log(chalk.cyan("📑 Sections configuration changed"));
|
|
@@ -273,7 +308,7 @@ class MDXToNextJSGenerator {
|
|
|
273
308
|
const fromFile = await this.loadSectionsConfig();
|
|
274
309
|
if (fromFile)
|
|
275
310
|
return;
|
|
276
|
-
const newSections = await this.discoverSectionsFromFrontmatter();
|
|
311
|
+
const newSections = this.withApiReferenceSection(await this.discoverSectionsFromFrontmatter());
|
|
277
312
|
const changed = JSON.stringify(newSections) !== JSON.stringify(this.sectionsConfig);
|
|
278
313
|
if (changed) {
|
|
279
314
|
console.log(chalk.cyan(newSections
|
|
@@ -590,6 +625,20 @@ class MDXToNextJSGenerator {
|
|
|
590
625
|
.on("error", (error) => {
|
|
591
626
|
console.error(chalk.red("❌ Analytics watcher error:"), error);
|
|
592
627
|
});
|
|
628
|
+
if (this.openApiSpecs.length > 0) {
|
|
629
|
+
const specPaths = this.openApiSpecs.map((spec) => path.resolve(this.rootDir, spec.file));
|
|
630
|
+
this.openApiWatcher = chokidar.watch(specPaths, {
|
|
631
|
+
persistent: true,
|
|
632
|
+
ignoreInitial: true,
|
|
633
|
+
});
|
|
634
|
+
this.openApiWatcher
|
|
635
|
+
.on("add", () => this.handleOpenApiChange())
|
|
636
|
+
.on("change", () => this.handleOpenApiChange())
|
|
637
|
+
.on("unlink", () => this.handleOpenApiChange())
|
|
638
|
+
.on("error", (error) => {
|
|
639
|
+
console.error(chalk.red("❌ OpenAPI watcher error:"), error);
|
|
640
|
+
});
|
|
641
|
+
}
|
|
593
642
|
const publicDir = path.join(this.rootDir, "public");
|
|
594
643
|
if (await fs.pathExists(publicDir)) {
|
|
595
644
|
this.setupPublicWatcher();
|
|
@@ -662,6 +711,14 @@ class MDXToNextJSGenerator {
|
|
|
662
711
|
// ignore
|
|
663
712
|
}
|
|
664
713
|
}
|
|
714
|
+
// A hand-written page that embeds an endpoint via `openapi:` frontmatter
|
|
715
|
+
// gets the same method badge in the sidebar as a generated endpoint page.
|
|
716
|
+
let httpMethod;
|
|
717
|
+
if (frontmatter.openapi) {
|
|
718
|
+
const op = this.apiRegistry.lookup(String(frontmatter.openapi));
|
|
719
|
+
if (op)
|
|
720
|
+
httpMethod = op.method.toUpperCase();
|
|
721
|
+
}
|
|
665
722
|
return {
|
|
666
723
|
slug: fullSlug,
|
|
667
724
|
title: frontmatter.title || "Untitled",
|
|
@@ -679,12 +736,27 @@ class MDXToNextJSGenerator {
|
|
|
679
736
|
...(frontmatter.categoryIcon
|
|
680
737
|
? { categoryIcon: String(frontmatter.categoryIcon) }
|
|
681
738
|
: {}),
|
|
739
|
+
...(httpMethod ? { httpMethod } : {}),
|
|
682
740
|
lastModified,
|
|
683
741
|
};
|
|
684
742
|
}
|
|
685
743
|
async buildAllPagesMeta() {
|
|
686
744
|
const files = await this.getAllMDXFiles();
|
|
687
|
-
|
|
745
|
+
const real = await Promise.all(files.map((file) => this.parseMDXFile(file)));
|
|
746
|
+
if (this.apiRegistry.isEmpty)
|
|
747
|
+
return real;
|
|
748
|
+
// Inject synthetic OpenAPI endpoint pages here - the single funnel every
|
|
749
|
+
// aggregate (nav, sitemap, llms) flows through - so they cannot be dropped
|
|
750
|
+
// by the .mdx-only disk scan. Hand-written pages win on any slug collision.
|
|
751
|
+
const realSlugs = new Set(real.map((page) => page.slug));
|
|
752
|
+
const synthetic = this.apiRegistry.syntheticPages().filter((page) => {
|
|
753
|
+
if (realSlugs.has(page.slug)) {
|
|
754
|
+
console.log(chalk.yellow(`⚠️ API page ${page.slug} is shadowed by a hand-written page; skipping`));
|
|
755
|
+
return false;
|
|
756
|
+
}
|
|
757
|
+
return true;
|
|
758
|
+
});
|
|
759
|
+
return [...real, ...synthetic];
|
|
688
760
|
}
|
|
689
761
|
/**
|
|
690
762
|
* Writes the generated page(s) for a single MDX file: the doc page and, for a
|
|
@@ -713,7 +785,17 @@ class MDXToNextJSGenerator {
|
|
|
713
785
|
frontmatter,
|
|
714
786
|
slug: fullSlug,
|
|
715
787
|
};
|
|
716
|
-
|
|
788
|
+
// `openapi: <METHOD> <path>` (or an operationId) in frontmatter renders
|
|
789
|
+
// that operation's playground inline with the author's prose. An unknown
|
|
790
|
+
// reference is logged and the page still renders its prose (graceful).
|
|
791
|
+
let apiOperation;
|
|
792
|
+
if (frontmatter.openapi) {
|
|
793
|
+
apiOperation = this.apiRegistry.lookup(String(frontmatter.openapi));
|
|
794
|
+
if (!apiOperation) {
|
|
795
|
+
console.error(chalk.red(`❌ openapi frontmatter "${frontmatter.openapi}" in ${filePath} not found in any spec`));
|
|
796
|
+
}
|
|
797
|
+
}
|
|
798
|
+
await this.generatePageFromMDX(mdxFile, apiOperation ? { apiOperation } : undefined);
|
|
717
799
|
}
|
|
718
800
|
if (isSectionIndex) {
|
|
719
801
|
await this.updateSectionIndex(sectionSlug, frontmatter, mdxContent, filePath);
|
|
@@ -848,8 +930,9 @@ export default function SectionIndex() {
|
|
|
848
930
|
console.log(chalk.blue(`🔀 Generated section index redirect: /${section.slug} -> /${firstPage.slug}`));
|
|
849
931
|
}
|
|
850
932
|
}
|
|
851
|
-
async generatePageFromMDX(mdxFile) {
|
|
933
|
+
async generatePageFromMDX(mdxFile, options) {
|
|
852
934
|
const fm = mdxFile.frontmatter;
|
|
935
|
+
const apiOperation = options?.apiOperation;
|
|
853
936
|
const metadataBlock = generateMetadataBlock({
|
|
854
937
|
title: fm.title,
|
|
855
938
|
titleFallback: "Generated with Doccupine",
|
|
@@ -873,12 +956,45 @@ export default function SectionIndex() {
|
|
|
873
956
|
: undefined,
|
|
874
957
|
image: fm.image,
|
|
875
958
|
});
|
|
959
|
+
// For an OpenAPI-backed page, embed the operation descriptor as a JS string
|
|
960
|
+
// literal parsed at load. Serializing to JSON then re-`JSON.parse`ing is
|
|
961
|
+
// total escaping for arbitrary JSON - unlike `escapeTemplateContent`, which
|
|
962
|
+
// only guards backticks/`${`/backslashes for the MDX prose literal.
|
|
963
|
+
const apiImport = apiOperation
|
|
964
|
+
? `\nimport { ApiPlayground } from "@/components/layout/ApiPlayground";`
|
|
965
|
+
: "";
|
|
966
|
+
// The descriptor JSON always exceeds the 80-col print width, so emit the
|
|
967
|
+
// call pre-wrapped in the exact shape Prettier produces (argument on its own
|
|
968
|
+
// line with a trailing comma, single-quoted so the JSON's own double quotes
|
|
969
|
+
// need no escaping). Keeps generated endpoint pages Prettier-stable without
|
|
970
|
+
// running a formatter at build time.
|
|
971
|
+
const apiConst = apiOperation
|
|
972
|
+
? (() => {
|
|
973
|
+
const arg = toJsStringLiteral(JSON.stringify(apiOperation));
|
|
974
|
+
const inline = `const operation = JSON.parse(${arg});`;
|
|
975
|
+
const decl = inline.length <= 80
|
|
976
|
+
? inline
|
|
977
|
+
: `const operation = JSON.parse(\n ${arg},\n);`;
|
|
978
|
+
return `\n${decl}\n`;
|
|
979
|
+
})()
|
|
980
|
+
: "";
|
|
981
|
+
// The playground renders as a child of <Docs> so it sits inside the docs
|
|
982
|
+
// content column (a sibling would escape the layout and overlap the nav).
|
|
983
|
+
// Synthetic endpoint pages pass no `sourcePath`: it only namespaces Mermaid
|
|
984
|
+
// diagrams (which endpoint docs never contain), and its long `@openapi/...`
|
|
985
|
+
// value would push the opening tag past 80 cols and make Prettier rewrap it.
|
|
986
|
+
const sourcePathLiteral = JSON.stringify(mdxFile.path);
|
|
987
|
+
const docsElement = apiOperation
|
|
988
|
+
? `<Docs content={content}>
|
|
989
|
+
<ApiPlayground operation={operation} />
|
|
990
|
+
</Docs>`
|
|
991
|
+
: `<Docs content={content} sourcePath={${sourcePathLiteral}} />`;
|
|
876
992
|
const pageContent = `import { Metadata } from "next";
|
|
877
993
|
import { Docs } from "@/components/Docs";
|
|
878
|
-
import { config } from "@/utils/config"
|
|
994
|
+
import { config } from "@/utils/config";${apiImport}
|
|
879
995
|
|
|
880
996
|
const content = \`${escapeTemplateContent(mdxFile.content)}\`;
|
|
881
|
-
|
|
997
|
+
${apiConst}
|
|
882
998
|
${metadataBlock}
|
|
883
999
|
|
|
884
1000
|
// Doc pages have no per-request data: theme resolves client-side via the
|
|
@@ -893,7 +1009,7 @@ export default function Page() {
|
|
|
893
1009
|
return (
|
|
894
1010
|
<>
|
|
895
1011
|
${jsonLd.element}
|
|
896
|
-
|
|
1012
|
+
${docsElement}
|
|
897
1013
|
</>
|
|
898
1014
|
);
|
|
899
1015
|
}
|
|
@@ -902,6 +1018,113 @@ export default function Page() {
|
|
|
902
1018
|
await fs.ensureDir(path.dirname(pagePath));
|
|
903
1019
|
await fs.writeFile(pagePath, pageContent, "utf8");
|
|
904
1020
|
}
|
|
1021
|
+
/** Parses the configured OpenAPI spec(s) into the shared registry. */
|
|
1022
|
+
async loadOpenApiRegistry() {
|
|
1023
|
+
if (this.openApiSpecs.length === 0)
|
|
1024
|
+
return;
|
|
1025
|
+
await this.apiRegistry.load(this.openApiSpecs, this.rootDir, this.apiBaseSlug);
|
|
1026
|
+
if (!this.apiRegistry.isEmpty) {
|
|
1027
|
+
console.log(chalk.blue(`📘 Loaded ${this.apiRegistry.all.length} API endpoint(s) from ${this.openApiSpecs.length} spec(s)`));
|
|
1028
|
+
}
|
|
1029
|
+
}
|
|
1030
|
+
/**
|
|
1031
|
+
* Generates one page per OpenAPI operation, (re)writes the request-execution
|
|
1032
|
+
* allowlist consumed by the playground proxy + component, and removes endpoint
|
|
1033
|
+
* pages that no longer exist in the spec. Safe to call when there are no specs
|
|
1034
|
+
* - it still emits an empty allowlist and prunes any previously generated
|
|
1035
|
+
* pages (e.g. after the `openapi` config is removed).
|
|
1036
|
+
*/
|
|
1037
|
+
async writeApiPages() {
|
|
1038
|
+
const nextSlugs = new Set();
|
|
1039
|
+
for (const op of this.apiRegistry.all) {
|
|
1040
|
+
const methodUpper = op.method.toUpperCase();
|
|
1041
|
+
const mdxFile = {
|
|
1042
|
+
path: `@openapi/${op.specName}/${op.method}${op.path}`,
|
|
1043
|
+
content: buildEndpointDoc(op),
|
|
1044
|
+
frontmatter: {
|
|
1045
|
+
title: op.summary ?? `${methodUpper} ${op.path}`,
|
|
1046
|
+
description: op.summary ?? "",
|
|
1047
|
+
},
|
|
1048
|
+
slug: op.slug,
|
|
1049
|
+
};
|
|
1050
|
+
try {
|
|
1051
|
+
await this.generatePageFromMDX(mdxFile, { apiOperation: op });
|
|
1052
|
+
nextSlugs.add(op.slug);
|
|
1053
|
+
}
|
|
1054
|
+
catch (error) {
|
|
1055
|
+
console.error(chalk.red(`❌ Error generating API page ${op.slug}:`), error);
|
|
1056
|
+
}
|
|
1057
|
+
}
|
|
1058
|
+
await this.writeApiAllowlist();
|
|
1059
|
+
await this.cleanupStaleApiPages(nextSlugs);
|
|
1060
|
+
if (nextSlugs.size > 0) {
|
|
1061
|
+
console.log(chalk.green(`🧩 Generated ${nextSlugs.size} API reference page(s)`));
|
|
1062
|
+
}
|
|
1063
|
+
}
|
|
1064
|
+
/** Writes the request-execution allowlist (overwrites the shipped stub). */
|
|
1065
|
+
async writeApiAllowlist() {
|
|
1066
|
+
const target = path.join(this.outputDir, "services", "openapi", "playground-allowlist.json");
|
|
1067
|
+
await fs.ensureDir(path.dirname(target));
|
|
1068
|
+
await fs.writeFile(target, `${JSON.stringify(this.apiRegistry.allowlist(), null, 2)}\n`, "utf8");
|
|
1069
|
+
}
|
|
1070
|
+
apiManifestPath() {
|
|
1071
|
+
return path.join(this.outputDir, ".doccupine-api-manifest.json");
|
|
1072
|
+
}
|
|
1073
|
+
async readApiManifest() {
|
|
1074
|
+
try {
|
|
1075
|
+
const manifestPath = this.apiManifestPath();
|
|
1076
|
+
if (await fs.pathExists(manifestPath)) {
|
|
1077
|
+
const raw = await fs.readFile(manifestPath, "utf8");
|
|
1078
|
+
const parsed = JSON.parse(raw);
|
|
1079
|
+
if (Array.isArray(parsed.pageSlugs)) {
|
|
1080
|
+
return new Set(parsed.pageSlugs.filter((entry) => typeof entry === "string"));
|
|
1081
|
+
}
|
|
1082
|
+
}
|
|
1083
|
+
}
|
|
1084
|
+
catch {
|
|
1085
|
+
// ignore corrupted manifest
|
|
1086
|
+
}
|
|
1087
|
+
return new Set();
|
|
1088
|
+
}
|
|
1089
|
+
async writeApiManifest(slugs) {
|
|
1090
|
+
const payload = { pageSlugs: Array.from(slugs).sort() };
|
|
1091
|
+
await fs.writeFile(this.apiManifestPath(), `${JSON.stringify(payload, null, 2)}\n`, "utf8");
|
|
1092
|
+
}
|
|
1093
|
+
/** Removes endpoint page directories that are no longer in the spec. */
|
|
1094
|
+
async cleanupStaleApiPages(nextSlugs) {
|
|
1095
|
+
const previous = new Set([
|
|
1096
|
+
...this.generatedApiPageSlugs,
|
|
1097
|
+
...(await this.readApiManifest()),
|
|
1098
|
+
]);
|
|
1099
|
+
for (const stale of previous) {
|
|
1100
|
+
if (nextSlugs.has(stale))
|
|
1101
|
+
continue;
|
|
1102
|
+
try {
|
|
1103
|
+
const dir = path.join(this.outputDir, "app", "(site)", stale);
|
|
1104
|
+
if (await fs.pathExists(dir)) {
|
|
1105
|
+
await fs.remove(dir);
|
|
1106
|
+
}
|
|
1107
|
+
}
|
|
1108
|
+
catch {
|
|
1109
|
+
// ignore
|
|
1110
|
+
}
|
|
1111
|
+
}
|
|
1112
|
+
this.generatedApiPageSlugs = nextSlugs;
|
|
1113
|
+
await this.writeApiManifest(nextSlugs);
|
|
1114
|
+
}
|
|
1115
|
+
/** Reparses the spec(s) and regenerates the API reference on a watch event. */
|
|
1116
|
+
async handleOpenApiChange() {
|
|
1117
|
+
console.log(chalk.cyan("📘 OpenAPI spec changed - regenerating API reference"));
|
|
1118
|
+
try {
|
|
1119
|
+
await this.apiRegistry.load(this.openApiSpecs, this.rootDir, this.apiBaseSlug);
|
|
1120
|
+
await this.writeApiPages();
|
|
1121
|
+
await this.refreshSiteAggregates();
|
|
1122
|
+
console.log(chalk.green("✅ API reference updated"));
|
|
1123
|
+
}
|
|
1124
|
+
catch (error) {
|
|
1125
|
+
console.error(chalk.red("❌ Error updating API reference:"), error);
|
|
1126
|
+
}
|
|
1127
|
+
}
|
|
905
1128
|
async updatePagesIndex() {
|
|
906
1129
|
const files = await this.getAllMDXFiles();
|
|
907
1130
|
let indexMDX = null;
|
|
@@ -921,6 +1144,9 @@ export default function Page() {
|
|
|
921
1144
|
updated: typeof frontmatter.updated === "string"
|
|
922
1145
|
? frontmatter.updated
|
|
923
1146
|
: undefined,
|
|
1147
|
+
openapi: typeof frontmatter.openapi === "string"
|
|
1148
|
+
? frontmatter.openapi
|
|
1149
|
+
: undefined,
|
|
924
1150
|
};
|
|
925
1151
|
break;
|
|
926
1152
|
}
|
|
@@ -946,12 +1172,32 @@ export default function Page() {
|
|
|
946
1172
|
updated: indexMDX?.updated ?? indexMDX?.date,
|
|
947
1173
|
image: indexMDX?.image,
|
|
948
1174
|
});
|
|
1175
|
+
// The homepage supports the same `openapi: <METHOD> <path>` frontmatter as
|
|
1176
|
+
// any other page: look the operation up and embed its playground inline.
|
|
1177
|
+
let apiOperation;
|
|
1178
|
+
if (indexMDX?.openapi) {
|
|
1179
|
+
apiOperation = this.apiRegistry.lookup(indexMDX.openapi);
|
|
1180
|
+
if (!apiOperation) {
|
|
1181
|
+
console.error(chalk.red(`❌ openapi frontmatter "${indexMDX.openapi}" in index.mdx not found in any spec`));
|
|
1182
|
+
}
|
|
1183
|
+
}
|
|
1184
|
+
const apiImport = apiOperation
|
|
1185
|
+
? `\nimport { ApiPlayground } from "@/components/layout/ApiPlayground";`
|
|
1186
|
+
: "";
|
|
1187
|
+
const apiConst = apiOperation
|
|
1188
|
+
? `\nconst operation = JSON.parse(${JSON.stringify(JSON.stringify(apiOperation))});\n`
|
|
1189
|
+
: "";
|
|
1190
|
+
const docsElement = apiOperation
|
|
1191
|
+
? `<Docs content={content} sourcePath="index.mdx">
|
|
1192
|
+
<ApiPlayground operation={operation} />
|
|
1193
|
+
</Docs>`
|
|
1194
|
+
: `<Docs content={content} sourcePath="index.mdx" />`;
|
|
949
1195
|
const indexContent = `import { Metadata } from "next";
|
|
950
1196
|
import { Docs } from "@/components/Docs";
|
|
951
|
-
import { config } from "@/utils/config"
|
|
1197
|
+
import { config } from "@/utils/config";${apiImport}
|
|
952
1198
|
|
|
953
1199
|
${indexMDX ? `const content = \`${escapeTemplateContent(indexMDX.content)}\`;` : `const content = null;`}
|
|
954
|
-
|
|
1200
|
+
${apiConst}
|
|
955
1201
|
${metadataBlock}
|
|
956
1202
|
|
|
957
1203
|
export const dynamic = "force-static";
|
|
@@ -963,7 +1209,7 @@ export default function Home() {
|
|
|
963
1209
|
return (
|
|
964
1210
|
<>
|
|
965
1211
|
${homeJsonLd.element}
|
|
966
|
-
|
|
1212
|
+
${docsElement}
|
|
967
1213
|
</>
|
|
968
1214
|
);
|
|
969
1215
|
}
|
|
@@ -1125,6 +1371,11 @@ export default function Page() {
|
|
|
1125
1371
|
return { url, name, description };
|
|
1126
1372
|
}
|
|
1127
1373
|
async readPageWithBody(page) {
|
|
1374
|
+
// Synthetic OpenAPI pages have no backing .mdx file; their markdown body
|
|
1375
|
+
// comes from the registry instead of disk.
|
|
1376
|
+
if (!page.path.endsWith(".mdx")) {
|
|
1377
|
+
return { ...page, body: this.apiRegistry.bodyForSlug(page.slug) ?? "" };
|
|
1378
|
+
}
|
|
1128
1379
|
const fullPath = path.join(this.watchDir, page.path);
|
|
1129
1380
|
const raw = await fs.readFile(fullPath, "utf8");
|
|
1130
1381
|
const { content: body } = matter(raw);
|
|
@@ -1223,6 +1474,10 @@ export default function Page() {
|
|
|
1223
1474
|
await this.analyticsWatcher.close();
|
|
1224
1475
|
console.log(chalk.yellow("👋 Stopped watching for analytics config changes"));
|
|
1225
1476
|
}
|
|
1477
|
+
if (this.openApiWatcher) {
|
|
1478
|
+
await this.openApiWatcher.close();
|
|
1479
|
+
console.log(chalk.yellow("👋 Stopped watching for OpenAPI spec changes"));
|
|
1480
|
+
}
|
|
1226
1481
|
if (this.publicWatcher) {
|
|
1227
1482
|
await this.publicWatcher.close();
|
|
1228
1483
|
console.log(chalk.yellow("👋 Stopped watching for public directory changes"));
|
|
@@ -1249,7 +1504,7 @@ program
|
|
|
1249
1504
|
reset: options.reset,
|
|
1250
1505
|
port: options.port,
|
|
1251
1506
|
});
|
|
1252
|
-
const generator = new MDXToNextJSGenerator(config.watchDir, config.outputDir);
|
|
1507
|
+
const generator = new MDXToNextJSGenerator(config.watchDir, config.outputDir, normalizeOpenApiConfig(config.openapi));
|
|
1253
1508
|
// Config paths are project-relative; child processes get an absolute cwd.
|
|
1254
1509
|
const outputDir = path.resolve(process.cwd(), config.outputDir);
|
|
1255
1510
|
await generator.init();
|
|
@@ -1341,7 +1596,7 @@ program
|
|
|
1341
1596
|
const config = await configManager.getConfig({
|
|
1342
1597
|
reset: options.reset,
|
|
1343
1598
|
});
|
|
1344
|
-
const generator = new MDXToNextJSGenerator(config.watchDir, config.outputDir);
|
|
1599
|
+
const generator = new MDXToNextJSGenerator(config.watchDir, config.outputDir, normalizeOpenApiConfig(config.openapi));
|
|
1345
1600
|
await generator.init();
|
|
1346
1601
|
console.log(chalk.green("🎉 Build complete!"));
|
|
1347
1602
|
});
|
|
@@ -1,4 +1,11 @@
|
|
|
1
|
-
import type { DoccupineConfig } from "./types.js";
|
|
1
|
+
import type { DoccupineConfig, NormalizedOpenApiSpec } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Collapses every accepted `openapi` config shape (a single path, a list of
|
|
4
|
+
* paths, or named-spec objects) into a uniform `NormalizedOpenApiSpec[]`. Names
|
|
5
|
+
* default to the file basename and are de-duplicated with a numeric suffix so
|
|
6
|
+
* two specs never share a route namespace.
|
|
7
|
+
*/
|
|
8
|
+
export declare function normalizeOpenApiConfig(openapi: DoccupineConfig["openapi"]): NormalizedOpenApiSpec[];
|
|
2
9
|
/**
|
|
3
10
|
* Rewrites a path as project-relative so `doccupine.json` stays portable
|
|
4
11
|
* across machines, CI and containers (an absolute `/Users/you/...` breaks for
|