doccupine 0.0.142 → 0.0.144
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 +1 -1
- package/dist/cli.d.ts +6 -0
- package/dist/cli.js +130 -22
- package/dist/generator/api-reference-generator.d.ts +20 -0
- package/dist/generator/api-reference-generator.js +95 -0
- package/dist/generator/app-scaffolder.d.ts +19 -0
- package/dist/generator/app-scaffolder.js +54 -0
- package/dist/generator/generated-route-manager.d.ts +17 -0
- package/dist/generator/generated-route-manager.js +91 -0
- package/dist/generator/page-catalog.d.ts +17 -0
- package/dist/generator/page-catalog.js +68 -0
- package/dist/generator/page-renderer.d.ts +31 -0
- package/dist/generator/page-renderer.js +248 -0
- package/dist/generator/project-config-repository.d.ts +25 -0
- package/dist/generator/project-config-repository.js +133 -0
- package/dist/generator/public-asset-manager.d.ts +20 -0
- package/dist/generator/public-asset-manager.js +225 -0
- package/dist/generator/section-resolver.d.ts +13 -0
- package/dist/generator/section-resolver.js +150 -0
- package/dist/generator/secure-source-fs.d.ts +27 -0
- package/dist/generator/secure-source-fs.js +361 -0
- package/dist/generator/site-artifacts.d.ts +22 -0
- package/dist/generator/site-artifacts.js +159 -0
- package/dist/generator/watch-coordinator.d.ts +57 -0
- package/dist/generator/watch-coordinator.js +438 -0
- package/dist/lib/generated-artifacts.d.ts +3 -0
- package/dist/lib/generated-artifacts.js +27 -1
- package/dist/lib/layout.d.ts +1 -17
- package/dist/lib/layout.js +5 -4
- package/dist/lib/project-config.d.ts +3 -0
- package/dist/lib/project-config.js +110 -0
- package/dist/lib/structures.js +2 -0
- package/dist/lib/types.d.ts +14 -1
- package/dist/mdx-to-nextjs-generator.d.ts +7 -25
- package/dist/mdx-to-nextjs-generator.js +126 -1574
- package/dist/templates/app/api/rag/route.d.ts +1 -1
- package/dist/templates/app/api/rag/route.js +2 -1
- package/dist/templates/components/Chat.d.ts +1 -1
- package/dist/templates/components/Chat.js +2 -1
- package/dist/templates/components/MDXComponents.d.ts +1 -1
- package/dist/templates/components/MDXComponents.js +1 -1
- package/dist/templates/components/layout/Accordion.d.ts +1 -1
- package/dist/templates/components/layout/Accordion.js +2 -3
- package/dist/templates/components/layout/Space.d.ts +1 -0
- package/dist/templates/components/layout/Space.js +70 -0
- package/dist/templates/mdx/space.mdx.d.ts +1 -1
- package/dist/templates/mdx/space.mdx.js +27 -27
- package/dist/templates/next.config.js +2 -2
- package/dist/templates/package.js +2 -2
- package/dist/templates/proxy.js +1 -1
- package/dist/templates/services/mcp/server.d.ts +1 -1
- package/dist/templates/services/mcp/server.js +2 -12
- package/package.json +2 -2
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
import { generateJsonLdScript, generateMetadataBlock, generateRuntimeOnlyMetadataBlock, } from "../lib/metadata.js";
|
|
2
|
+
import { parseUpdateBlocks } from "../lib/rss.js";
|
|
3
|
+
import { escapeTemplateContent, toJsStringLiteral } from "../lib/utils.js";
|
|
4
|
+
import { rssRouteTemplate } from "../templates/app/rssRoute.js";
|
|
5
|
+
export function renderMdxPage(mdxFile, options) {
|
|
6
|
+
const fm = mdxFile.frontmatter;
|
|
7
|
+
const apiOperation = options?.apiOperation;
|
|
8
|
+
const isSynthetic = mdxFile.path.startsWith("@openapi/");
|
|
9
|
+
const updates = isSynthetic ? [] : parseUpdateBlocks(mdxFile.content);
|
|
10
|
+
const hasFeed = updates.length > 0;
|
|
11
|
+
const feedPath = `/${mdxFile.slug}/rss.xml`;
|
|
12
|
+
const metadataBlock = generateMetadataBlock({
|
|
13
|
+
title: fm.title,
|
|
14
|
+
titleFallback: "Generated with Doccupine",
|
|
15
|
+
name: fm.name,
|
|
16
|
+
titleOrder: "page-first",
|
|
17
|
+
description: fm.description,
|
|
18
|
+
icon: fm.icon,
|
|
19
|
+
image: fm.image,
|
|
20
|
+
canonicalPath: mdxFile.slug,
|
|
21
|
+
rssPath: hasFeed ? feedPath : undefined,
|
|
22
|
+
});
|
|
23
|
+
const jsonLd = generateJsonLdScript({
|
|
24
|
+
kind: "article",
|
|
25
|
+
canonicalPath: mdxFile.slug,
|
|
26
|
+
title: fm.title,
|
|
27
|
+
description: fm.description,
|
|
28
|
+
date: typeof fm.date === "string" ? fm.date : undefined,
|
|
29
|
+
updated: typeof fm.updated === "string"
|
|
30
|
+
? fm.updated
|
|
31
|
+
: typeof fm.date === "string"
|
|
32
|
+
? fm.date
|
|
33
|
+
: undefined,
|
|
34
|
+
image: fm.image,
|
|
35
|
+
});
|
|
36
|
+
const apiImport = apiOperation
|
|
37
|
+
? `\nimport { ApiPlayground } from "@/components/layout/ApiPlayground";`
|
|
38
|
+
: "";
|
|
39
|
+
const apiConst = apiOperation
|
|
40
|
+
? (() => {
|
|
41
|
+
const arg = toJsStringLiteral(JSON.stringify(apiOperation));
|
|
42
|
+
const inline = `const operation = JSON.parse(${arg});`;
|
|
43
|
+
const decl = inline.length <= 80
|
|
44
|
+
? inline
|
|
45
|
+
: `const operation = JSON.parse(\n ${arg},\n);`;
|
|
46
|
+
return `\n${decl}\n`;
|
|
47
|
+
})()
|
|
48
|
+
: "";
|
|
49
|
+
const sourcePathLiteral = JSON.stringify(mdxFile.path);
|
|
50
|
+
const showRssButton = hasFeed && fm.rss === true && !apiOperation;
|
|
51
|
+
const docsAttrs = [
|
|
52
|
+
`content={content}`,
|
|
53
|
+
`sourcePath={${sourcePathLiteral}}`,
|
|
54
|
+
...(showRssButton ? [`rssHref={${JSON.stringify(feedPath)}}`] : []),
|
|
55
|
+
];
|
|
56
|
+
const inlineDocs = `<Docs ${docsAttrs.join(" ")} />`;
|
|
57
|
+
const docsElement = apiOperation
|
|
58
|
+
? `<Docs content={content}>
|
|
59
|
+
<ApiPlayground operation={operation} />
|
|
60
|
+
</Docs>`
|
|
61
|
+
: inlineDocs.length + 6 <= 80
|
|
62
|
+
? inlineDocs
|
|
63
|
+
: `<Docs\n${docsAttrs.map((attr) => ` ${attr}`).join("\n")}\n />`;
|
|
64
|
+
const pageContent = `import { Metadata } from "next";
|
|
65
|
+
import { Docs } from "@/components/Docs";
|
|
66
|
+
import { config } from "@/utils/config";${apiImport}
|
|
67
|
+
|
|
68
|
+
const content = \`${escapeTemplateContent(mdxFile.content)}\`;
|
|
69
|
+
${apiConst}
|
|
70
|
+
${metadataBlock}
|
|
71
|
+
|
|
72
|
+
// Doc pages have no per-request data: theme resolves client-side via the
|
|
73
|
+
// "dark" class on <html> (set before paint by the theme-init blocking
|
|
74
|
+
// script). Static rendering lets every response come from the edge cache.
|
|
75
|
+
export const dynamic = "force-static";
|
|
76
|
+
export const revalidate = false;
|
|
77
|
+
|
|
78
|
+
export default function Page() {
|
|
79
|
+
${jsonLd.declarations}
|
|
80
|
+
|
|
81
|
+
return (
|
|
82
|
+
<>
|
|
83
|
+
${jsonLd.element}
|
|
84
|
+
${docsElement}
|
|
85
|
+
</>
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
`;
|
|
89
|
+
const rssRoute = isSynthetic
|
|
90
|
+
? { action: "preserve" }
|
|
91
|
+
: hasFeed
|
|
92
|
+
? {
|
|
93
|
+
action: "write",
|
|
94
|
+
content: rssRouteTemplate({
|
|
95
|
+
pagePath: mdxFile.slug,
|
|
96
|
+
title: typeof fm.title === "string" ? fm.title : null,
|
|
97
|
+
description: typeof fm.description === "string" ? fm.description : null,
|
|
98
|
+
items: updates.map((update) => ({
|
|
99
|
+
title: update.label,
|
|
100
|
+
anchor: update.anchor,
|
|
101
|
+
description: update.description,
|
|
102
|
+
})),
|
|
103
|
+
}),
|
|
104
|
+
}
|
|
105
|
+
: { action: "remove" };
|
|
106
|
+
return { pageContent, rssRoute };
|
|
107
|
+
}
|
|
108
|
+
export function renderHomepage(indexMDX, apiOperation) {
|
|
109
|
+
const updates = indexMDX ? parseUpdateBlocks(indexMDX.content) : [];
|
|
110
|
+
const hasFeed = updates.length > 0;
|
|
111
|
+
const feedPath = "/rss.xml";
|
|
112
|
+
const metadataBlock = indexMDX
|
|
113
|
+
? generateMetadataBlock({
|
|
114
|
+
title: indexMDX.title,
|
|
115
|
+
titleFallback: "Welcome",
|
|
116
|
+
name: indexMDX.name,
|
|
117
|
+
titleOrder: "name-first",
|
|
118
|
+
description: indexMDX.description || undefined,
|
|
119
|
+
icon: indexMDX.icon,
|
|
120
|
+
image: indexMDX.image,
|
|
121
|
+
canonicalPath: "",
|
|
122
|
+
rssPath: hasFeed ? feedPath : undefined,
|
|
123
|
+
})
|
|
124
|
+
: generateRuntimeOnlyMetadataBlock();
|
|
125
|
+
const homeJsonLd = generateJsonLdScript({
|
|
126
|
+
kind: "homepage",
|
|
127
|
+
canonicalPath: "",
|
|
128
|
+
title: indexMDX?.title,
|
|
129
|
+
description: indexMDX?.description || undefined,
|
|
130
|
+
date: indexMDX?.date,
|
|
131
|
+
updated: indexMDX?.updated ?? indexMDX?.date,
|
|
132
|
+
image: indexMDX?.image,
|
|
133
|
+
});
|
|
134
|
+
const apiImport = apiOperation
|
|
135
|
+
? `\nimport { ApiPlayground } from "@/components/layout/ApiPlayground";`
|
|
136
|
+
: "";
|
|
137
|
+
const apiConst = apiOperation
|
|
138
|
+
? `\nconst operation = JSON.parse(${JSON.stringify(JSON.stringify(apiOperation))});\n`
|
|
139
|
+
: "";
|
|
140
|
+
const showRssButton = hasFeed && indexMDX?.rss === true && !apiOperation;
|
|
141
|
+
const docsElement = apiOperation
|
|
142
|
+
? `<Docs content={content} sourcePath="index.mdx">
|
|
143
|
+
<ApiPlayground operation={operation} />
|
|
144
|
+
</Docs>`
|
|
145
|
+
: showRssButton
|
|
146
|
+
? `<Docs content={content} sourcePath="index.mdx" rssHref={"/rss.xml"} />`
|
|
147
|
+
: `<Docs content={content} sourcePath="index.mdx" />`;
|
|
148
|
+
const pageContent = `import { Metadata } from "next";
|
|
149
|
+
import { Docs } from "@/components/Docs";
|
|
150
|
+
import { config } from "@/utils/config";${apiImport}
|
|
151
|
+
|
|
152
|
+
${indexMDX ? `const content = \`${escapeTemplateContent(indexMDX.content)}\`;` : `const content = null;`}
|
|
153
|
+
${apiConst}
|
|
154
|
+
${metadataBlock}
|
|
155
|
+
|
|
156
|
+
export const dynamic = "force-static";
|
|
157
|
+
export const revalidate = false;
|
|
158
|
+
|
|
159
|
+
export default function Home() {
|
|
160
|
+
${homeJsonLd.declarations}
|
|
161
|
+
|
|
162
|
+
return (
|
|
163
|
+
<>
|
|
164
|
+
${homeJsonLd.element}
|
|
165
|
+
${docsElement}
|
|
166
|
+
</>
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
`;
|
|
170
|
+
const rssRoute = hasFeed && indexMDX
|
|
171
|
+
? {
|
|
172
|
+
action: "write",
|
|
173
|
+
content: rssRouteTemplate({
|
|
174
|
+
pagePath: "",
|
|
175
|
+
title: indexMDX.title,
|
|
176
|
+
description: indexMDX.description || null,
|
|
177
|
+
items: updates.map((update) => ({
|
|
178
|
+
title: update.label,
|
|
179
|
+
anchor: update.anchor,
|
|
180
|
+
description: update.description,
|
|
181
|
+
})),
|
|
182
|
+
}),
|
|
183
|
+
}
|
|
184
|
+
: { action: "remove" };
|
|
185
|
+
return { pageContent, rssRoute };
|
|
186
|
+
}
|
|
187
|
+
export function renderSectionPage(sectionSlug, frontmatter, mdxContent, sourcePath) {
|
|
188
|
+
const updates = parseUpdateBlocks(mdxContent);
|
|
189
|
+
const hasFeed = updates.length > 0;
|
|
190
|
+
const feedPath = `/${sectionSlug}/rss.xml`;
|
|
191
|
+
const showRssButton = hasFeed && frontmatter.rss === true;
|
|
192
|
+
const metadataBlock = generateMetadataBlock({
|
|
193
|
+
title: frontmatter.title,
|
|
194
|
+
titleFallback: "Section",
|
|
195
|
+
name: frontmatter.name,
|
|
196
|
+
titleOrder: "name-first",
|
|
197
|
+
description: frontmatter.description || undefined,
|
|
198
|
+
icon: frontmatter.icon,
|
|
199
|
+
image: frontmatter.image,
|
|
200
|
+
canonicalPath: sectionSlug,
|
|
201
|
+
rssPath: hasFeed ? feedPath : undefined,
|
|
202
|
+
});
|
|
203
|
+
const sectionJsonLd = generateJsonLdScript({
|
|
204
|
+
kind: "article",
|
|
205
|
+
canonicalPath: sectionSlug,
|
|
206
|
+
title: frontmatter.title,
|
|
207
|
+
description: frontmatter.description,
|
|
208
|
+
date: typeof frontmatter.date === "string" ? frontmatter.date : undefined,
|
|
209
|
+
updated: typeof frontmatter.updated === "string"
|
|
210
|
+
? frontmatter.updated
|
|
211
|
+
: typeof frontmatter.date === "string"
|
|
212
|
+
? frontmatter.date
|
|
213
|
+
: undefined,
|
|
214
|
+
image: frontmatter.image,
|
|
215
|
+
});
|
|
216
|
+
const docsAttrs = [
|
|
217
|
+
`content={content}`,
|
|
218
|
+
`sourcePath={${JSON.stringify(sourcePath ?? `${sectionSlug}/index.mdx`)}}`,
|
|
219
|
+
...(showRssButton ? [`rssHref={${JSON.stringify(feedPath)}}`] : []),
|
|
220
|
+
];
|
|
221
|
+
const inlineDocs = `<Docs ${docsAttrs.join(" ")} />`;
|
|
222
|
+
const docsElement = inlineDocs.length + 6 <= 80
|
|
223
|
+
? inlineDocs
|
|
224
|
+
: `<Docs\n${docsAttrs.map((attr) => ` ${attr}`).join("\n")}\n />`;
|
|
225
|
+
const pageContent = `import { Metadata } from "next";
|
|
226
|
+
import { Docs } from "@/components/Docs";
|
|
227
|
+
import { config } from "@/utils/config";
|
|
228
|
+
|
|
229
|
+
const content = \`${escapeTemplateContent(mdxContent)}\`;
|
|
230
|
+
|
|
231
|
+
${metadataBlock}
|
|
232
|
+
|
|
233
|
+
export const dynamic = "force-static";
|
|
234
|
+
export const revalidate = false;
|
|
235
|
+
|
|
236
|
+
export default function Page() {
|
|
237
|
+
${sectionJsonLd.declarations}
|
|
238
|
+
|
|
239
|
+
return (
|
|
240
|
+
<>
|
|
241
|
+
${sectionJsonLd.element}
|
|
242
|
+
${docsElement}
|
|
243
|
+
</>
|
|
244
|
+
);
|
|
245
|
+
}
|
|
246
|
+
`;
|
|
247
|
+
return { pageContent, rssRoute: { action: "preserve" } };
|
|
248
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { AnalyticsConfig, FontConfig, SectionConfig } from "../lib/types.js";
|
|
2
|
+
import { SecureSourceFs } from "./secure-source-fs.js";
|
|
3
|
+
export declare class ProjectConfigRepository {
|
|
4
|
+
private readonly rootDir;
|
|
5
|
+
private readonly outputDir;
|
|
6
|
+
private readonly sourceFs;
|
|
7
|
+
private readonly configFiles;
|
|
8
|
+
private readonly fontConfigFile;
|
|
9
|
+
private readonly analyticsConfigFile;
|
|
10
|
+
constructor(rootDir: string, outputDir: string, sourceFs: SecureSourceFs, configFiles: readonly string[], fontConfigFile: string, analyticsConfigFile: string);
|
|
11
|
+
private outputPath;
|
|
12
|
+
private copyRootSourceFile;
|
|
13
|
+
copyConfigFile(configFile: string): Promise<void>;
|
|
14
|
+
resetConfigFile(configFile: string): Promise<void>;
|
|
15
|
+
copyCustomConfigFiles(): Promise<void>;
|
|
16
|
+
copyFontConfigFile(): Promise<void>;
|
|
17
|
+
copyFontConfig(): Promise<void>;
|
|
18
|
+
removeFontConfig(): Promise<boolean>;
|
|
19
|
+
loadFontConfig(): Promise<FontConfig | null>;
|
|
20
|
+
loadAnalyticsConfig(): Promise<AnalyticsConfig | null>;
|
|
21
|
+
writeAnalyticsConfig(config: AnalyticsConfig | null): Promise<void>;
|
|
22
|
+
copyAnalyticsConfig(loadConfig: () => Promise<AnalyticsConfig | null>): Promise<void>;
|
|
23
|
+
resetAnalyticsConfig(): Promise<void>;
|
|
24
|
+
loadSectionsConfig(): Promise<SectionConfig[] | null>;
|
|
25
|
+
}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import chalk from "chalk";
|
|
2
|
+
import fs from "fs-extra";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { resolveOutputPath } from "../lib/output-safety.js";
|
|
5
|
+
import { validateAnalyticsConfig, validateFontConfig, } from "../lib/project-config.js";
|
|
6
|
+
import { writeFileAtomic } from "../lib/utils.js";
|
|
7
|
+
import { validateSectionsConfig } from "./section-resolver.js";
|
|
8
|
+
const ARRAY_CONFIG_DEFAULTS = new Set([
|
|
9
|
+
"links.json",
|
|
10
|
+
"navigation.json",
|
|
11
|
+
"sections.json",
|
|
12
|
+
]);
|
|
13
|
+
export class ProjectConfigRepository {
|
|
14
|
+
rootDir;
|
|
15
|
+
outputDir;
|
|
16
|
+
sourceFs;
|
|
17
|
+
configFiles;
|
|
18
|
+
fontConfigFile;
|
|
19
|
+
analyticsConfigFile;
|
|
20
|
+
constructor(rootDir, outputDir, sourceFs, configFiles, fontConfigFile, analyticsConfigFile) {
|
|
21
|
+
this.rootDir = rootDir;
|
|
22
|
+
this.outputDir = outputDir;
|
|
23
|
+
this.sourceFs = sourceFs;
|
|
24
|
+
this.configFiles = configFiles;
|
|
25
|
+
this.fontConfigFile = fontConfigFile;
|
|
26
|
+
this.analyticsConfigFile = analyticsConfigFile;
|
|
27
|
+
}
|
|
28
|
+
outputPath(...segments) {
|
|
29
|
+
return resolveOutputPath(this.outputDir, ...segments);
|
|
30
|
+
}
|
|
31
|
+
async copyRootSourceFile(sourcePath, destPath, label) {
|
|
32
|
+
const { data } = await this.sourceFs.readProjectSourceFile(sourcePath, label);
|
|
33
|
+
await writeFileAtomic(destPath, data);
|
|
34
|
+
}
|
|
35
|
+
async copyConfigFile(configFile) {
|
|
36
|
+
await this.copyRootSourceFile(path.join(this.rootDir, configFile), this.outputPath(configFile), "config source");
|
|
37
|
+
}
|
|
38
|
+
async resetConfigFile(configFile) {
|
|
39
|
+
await writeFileAtomic(this.outputPath(configFile), ARRAY_CONFIG_DEFAULTS.has(configFile) ? `[]\n` : `{}\n`);
|
|
40
|
+
}
|
|
41
|
+
async copyCustomConfigFiles() {
|
|
42
|
+
console.log(chalk.blue(`🔍 Checking for config files in: ${this.rootDir}`));
|
|
43
|
+
for (const configFile of this.configFiles) {
|
|
44
|
+
const sourcePath = path.join(this.rootDir, configFile);
|
|
45
|
+
console.log(chalk.gray(` Checking ${configFile}...`));
|
|
46
|
+
if (await fs.pathExists(sourcePath)) {
|
|
47
|
+
await this.copyConfigFile(configFile);
|
|
48
|
+
console.log(chalk.green(` ✓ Copied ${configFile} to Next.js app`));
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
console.log(chalk.gray(` ✗ ${configFile} not found, skipping`));
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
async copyFontConfigFile() {
|
|
56
|
+
await this.copyRootSourceFile(path.join(this.rootDir, this.fontConfigFile), this.outputPath(this.fontConfigFile), "font source");
|
|
57
|
+
}
|
|
58
|
+
async copyFontConfig() {
|
|
59
|
+
console.log(chalk.blue(`🔍 Checking for font configuration...`));
|
|
60
|
+
const sourcePath = path.join(this.rootDir, this.fontConfigFile);
|
|
61
|
+
if (await fs.pathExists(sourcePath)) {
|
|
62
|
+
await this.copyFontConfigFile();
|
|
63
|
+
console.log(chalk.green(` ✓ Copied ${this.fontConfigFile} to Next.js app`));
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
console.log(chalk.gray(` ✗ ${this.fontConfigFile} not found, skipping`));
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
async removeFontConfig() {
|
|
70
|
+
const destPath = this.outputPath(this.fontConfigFile);
|
|
71
|
+
if (!(await fs.pathExists(destPath)))
|
|
72
|
+
return false;
|
|
73
|
+
await fs.remove(destPath);
|
|
74
|
+
return true;
|
|
75
|
+
}
|
|
76
|
+
async loadFontConfig() {
|
|
77
|
+
const fontPath = path.join(this.rootDir, this.fontConfigFile);
|
|
78
|
+
try {
|
|
79
|
+
if (await fs.pathExists(fontPath)) {
|
|
80
|
+
const { data } = await this.sourceFs.readProjectSourceFile(fontPath, "font source");
|
|
81
|
+
return validateFontConfig(JSON.parse(data.toString("utf8")));
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
catch (error) {
|
|
85
|
+
console.warn(chalk.yellow(`⚠️ Error reading ${this.fontConfigFile}`), error);
|
|
86
|
+
}
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
89
|
+
async loadAnalyticsConfig() {
|
|
90
|
+
const analyticsPath = path.join(this.rootDir, this.analyticsConfigFile);
|
|
91
|
+
try {
|
|
92
|
+
if (await fs.pathExists(analyticsPath)) {
|
|
93
|
+
const { data } = await this.sourceFs.readProjectSourceFile(analyticsPath, "analytics source");
|
|
94
|
+
return validateAnalyticsConfig(JSON.parse(data.toString("utf8")));
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
catch (error) {
|
|
98
|
+
console.warn(chalk.yellow(`⚠️ Error reading ${this.analyticsConfigFile}`), error);
|
|
99
|
+
}
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
async writeAnalyticsConfig(config) {
|
|
103
|
+
await writeFileAtomic(this.outputPath(this.analyticsConfigFile), config ? `${JSON.stringify(config, null, 2)}\n` : `{}\n`);
|
|
104
|
+
}
|
|
105
|
+
async copyAnalyticsConfig(loadConfig) {
|
|
106
|
+
console.log(chalk.blue(`🔍 Checking for analytics configuration...`));
|
|
107
|
+
const sourcePath = path.join(this.rootDir, this.analyticsConfigFile);
|
|
108
|
+
if (await fs.pathExists(sourcePath)) {
|
|
109
|
+
await this.writeAnalyticsConfig(await loadConfig());
|
|
110
|
+
console.log(chalk.green(` ✓ Copied ${this.analyticsConfigFile} to Next.js app`));
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
console.log(chalk.gray(` ✗ ${this.analyticsConfigFile} not found, skipping`));
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
async resetAnalyticsConfig() {
|
|
117
|
+
await this.writeAnalyticsConfig(null);
|
|
118
|
+
}
|
|
119
|
+
async loadSectionsConfig() {
|
|
120
|
+
const sectionsPath = path.join(this.rootDir, "sections.json");
|
|
121
|
+
try {
|
|
122
|
+
if (await fs.pathExists(sectionsPath)) {
|
|
123
|
+
const content = await fs.readFile(sectionsPath, "utf8");
|
|
124
|
+
const parsed = JSON.parse(content);
|
|
125
|
+
return validateSectionsConfig(parsed);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
catch (error) {
|
|
129
|
+
console.warn(chalk.yellow("⚠️ Error reading sections.json"), error);
|
|
130
|
+
}
|
|
131
|
+
return null;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { GeneratedArtifacts } from "../lib/generated-artifacts.js";
|
|
2
|
+
import type { PageWithBody } from "../templates/llms/llmsFull.js";
|
|
3
|
+
import { SecureSourceFs } from "./secure-source-fs.js";
|
|
4
|
+
export declare class PublicAssetManager {
|
|
5
|
+
private readonly rootDir;
|
|
6
|
+
private readonly outputDir;
|
|
7
|
+
private readonly artifacts;
|
|
8
|
+
private readonly sourceFs;
|
|
9
|
+
constructor(rootDir: string, outputDir: string, artifacts: GeneratedArtifacts, sourceFs: SecureSourceFs);
|
|
10
|
+
private outputPath;
|
|
11
|
+
publicOutputFilePath(relativePath: string): string;
|
|
12
|
+
copyRegularPublicFile(sourcePath: string, destPath: string): Promise<void>;
|
|
13
|
+
copyPublicFiles(): Promise<void>;
|
|
14
|
+
handlePublicFileChange(filePath: string, restoreGeneratedArtifacts: () => Promise<void>): Promise<void>;
|
|
15
|
+
handlePublicFileDelete(filePath: string, copyCurrentSource: () => Promise<void>, restoreGeneratedArtifacts: () => Promise<void>): Promise<void>;
|
|
16
|
+
findSourcePublicAsset(relativePath: string): Promise<string | null>;
|
|
17
|
+
writePublicAggregate(relativePath: string, content: string): Promise<void>;
|
|
18
|
+
syncMcpManifest(baseUrl: string | null, siteName: string): Promise<void>;
|
|
19
|
+
syncLlmsPageFiles(pages: PageWithBody[], baseUrl: string | null): Promise<void>;
|
|
20
|
+
}
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
import chalk from "chalk";
|
|
2
|
+
import fs from "fs-extra";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { resolveOutputPath, resolveWithin } from "../lib/output-safety.js";
|
|
5
|
+
import { writeFileAtomic } from "../lib/utils.js";
|
|
6
|
+
import { llmsPageTemplate } from "../templates/llms/llmsPage.js";
|
|
7
|
+
import { siteDocsSlug } from "../templates/llms/skillMd.js";
|
|
8
|
+
const PUBLIC_AGGREGATE_PATHS = new Set([
|
|
9
|
+
"llms.txt",
|
|
10
|
+
"llms-full.txt",
|
|
11
|
+
"skill.md",
|
|
12
|
+
".well-known/mcp.json",
|
|
13
|
+
]);
|
|
14
|
+
function normalizePublicArtifactPath(relativePath) {
|
|
15
|
+
return relativePath.replace(/\\/g, "/").toLowerCase();
|
|
16
|
+
}
|
|
17
|
+
function isPublicAggregate(relativePath) {
|
|
18
|
+
return PUBLIC_AGGREGATE_PATHS.has(normalizePublicArtifactPath(relativePath));
|
|
19
|
+
}
|
|
20
|
+
function isManagedPublicArtifact(relativePath) {
|
|
21
|
+
const normalized = normalizePublicArtifactPath(relativePath);
|
|
22
|
+
return PUBLIC_AGGREGATE_PATHS.has(normalized) || normalized.endsWith(".md");
|
|
23
|
+
}
|
|
24
|
+
function publicDestinationRelativePath(relativePath) {
|
|
25
|
+
return isPublicAggregate(relativePath)
|
|
26
|
+
? normalizePublicArtifactPath(relativePath)
|
|
27
|
+
: relativePath.replace(/\\/g, "/");
|
|
28
|
+
}
|
|
29
|
+
export class PublicAssetManager {
|
|
30
|
+
rootDir;
|
|
31
|
+
outputDir;
|
|
32
|
+
artifacts;
|
|
33
|
+
sourceFs;
|
|
34
|
+
constructor(rootDir, outputDir, artifacts, sourceFs) {
|
|
35
|
+
this.rootDir = rootDir;
|
|
36
|
+
this.outputDir = outputDir;
|
|
37
|
+
this.artifacts = artifacts;
|
|
38
|
+
this.sourceFs = sourceFs;
|
|
39
|
+
}
|
|
40
|
+
outputPath(...segments) {
|
|
41
|
+
return resolveOutputPath(this.outputDir, ...segments);
|
|
42
|
+
}
|
|
43
|
+
publicOutputFilePath(relativePath) {
|
|
44
|
+
const parent = path.dirname(relativePath);
|
|
45
|
+
const outputParent = parent === "."
|
|
46
|
+
? this.outputPath("public")
|
|
47
|
+
: this.outputPath("public", parent);
|
|
48
|
+
return resolveWithin(outputParent, path.basename(relativePath));
|
|
49
|
+
}
|
|
50
|
+
async copyRegularPublicFile(sourcePath, destPath) {
|
|
51
|
+
const { data } = await this.sourceFs.readPublicSourceFile(sourcePath);
|
|
52
|
+
await writeFileAtomic(destPath, data);
|
|
53
|
+
}
|
|
54
|
+
async copyPublicFiles() {
|
|
55
|
+
const publicDir = path.join(this.rootDir, "public");
|
|
56
|
+
const previousFiles = this.artifacts.publicFiles();
|
|
57
|
+
console.log(chalk.blue(`🔍 Checking for public directory...`));
|
|
58
|
+
const files = await this.sourceFs.scanPublicFiles();
|
|
59
|
+
if (files === null) {
|
|
60
|
+
console.log(chalk.gray(` ✗ public directory not found, skipping`));
|
|
61
|
+
for (const stale of previousFiles) {
|
|
62
|
+
await fs.remove(this.publicOutputFilePath(stale));
|
|
63
|
+
}
|
|
64
|
+
this.artifacts.replacePublicFiles([]);
|
|
65
|
+
await this.artifacts.save();
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
const nextFiles = new Set();
|
|
69
|
+
const nextByFoldedPath = new Map();
|
|
70
|
+
for (const relativePath of files) {
|
|
71
|
+
const destRelativePath = publicDestinationRelativePath(relativePath);
|
|
72
|
+
nextFiles.add(destRelativePath);
|
|
73
|
+
nextByFoldedPath.set(destRelativePath.toLowerCase(), destRelativePath);
|
|
74
|
+
}
|
|
75
|
+
const removedBeforeCopy = new Set();
|
|
76
|
+
for (const stale of previousFiles) {
|
|
77
|
+
const replacement = nextByFoldedPath.get(stale.toLowerCase());
|
|
78
|
+
if (replacement &&
|
|
79
|
+
replacement !== stale &&
|
|
80
|
+
(await fs.pathExists(this.publicOutputFilePath(replacement)))) {
|
|
81
|
+
await fs.remove(this.publicOutputFilePath(stale));
|
|
82
|
+
removedBeforeCopy.add(stale);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
for (const relativePath of files) {
|
|
86
|
+
const destRelativePath = publicDestinationRelativePath(relativePath);
|
|
87
|
+
await this.copyRegularPublicFile(path.join(publicDir, relativePath), this.publicOutputFilePath(destRelativePath));
|
|
88
|
+
}
|
|
89
|
+
for (const stale of previousFiles) {
|
|
90
|
+
if (!nextFiles.has(stale) && !removedBeforeCopy.has(stale)) {
|
|
91
|
+
await fs.remove(this.publicOutputFilePath(stale));
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
this.artifacts.replacePublicFiles(nextFiles);
|
|
95
|
+
await this.artifacts.save();
|
|
96
|
+
console.log(chalk.green(` ✓ Copied public directory to Next.js app`));
|
|
97
|
+
}
|
|
98
|
+
async handlePublicFileChange(filePath, restoreGeneratedArtifacts) {
|
|
99
|
+
const publicDir = path.join(this.rootDir, "public");
|
|
100
|
+
const relativePath = path.relative(publicDir, filePath);
|
|
101
|
+
const destRelativePath = publicDestinationRelativePath(relativePath);
|
|
102
|
+
const destPath = this.publicOutputFilePath(destRelativePath);
|
|
103
|
+
try {
|
|
104
|
+
await this.copyRegularPublicFile(filePath, destPath);
|
|
105
|
+
const publicFiles = this.artifacts.publicFiles();
|
|
106
|
+
publicFiles.add(destRelativePath);
|
|
107
|
+
this.artifacts.replacePublicFiles(publicFiles);
|
|
108
|
+
await this.artifacts.save();
|
|
109
|
+
console.log(chalk.green(`📋 Updated public/${relativePath} in Next.js app`));
|
|
110
|
+
if (isManagedPublicArtifact(relativePath)) {
|
|
111
|
+
await restoreGeneratedArtifacts();
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
catch (error) {
|
|
115
|
+
console.error(chalk.red(`❌ Error copying public/${relativePath}:`), error);
|
|
116
|
+
throw error;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
async handlePublicFileDelete(filePath, copyCurrentSource, restoreGeneratedArtifacts) {
|
|
120
|
+
const publicDir = path.join(this.rootDir, "public");
|
|
121
|
+
const relativePath = path.relative(publicDir, filePath);
|
|
122
|
+
const destRelativePath = publicDestinationRelativePath(relativePath);
|
|
123
|
+
const destPath = this.outputPath("public", destRelativePath);
|
|
124
|
+
try {
|
|
125
|
+
// A rapid replace can queue an unlink after the replacement already
|
|
126
|
+
// exists. Copy the current source instead of deleting its fresh mirror.
|
|
127
|
+
if (await fs.pathExists(filePath)) {
|
|
128
|
+
await copyCurrentSource();
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
if (await fs.pathExists(destPath)) {
|
|
132
|
+
await fs.remove(destPath);
|
|
133
|
+
console.log(chalk.yellow(`🗑️ Removed public/${relativePath} from Next.js app`));
|
|
134
|
+
}
|
|
135
|
+
const publicFiles = this.artifacts.publicFiles();
|
|
136
|
+
publicFiles.delete(destRelativePath);
|
|
137
|
+
this.artifacts.replacePublicFiles(publicFiles);
|
|
138
|
+
await this.artifacts.save();
|
|
139
|
+
if (isManagedPublicArtifact(relativePath)) {
|
|
140
|
+
await restoreGeneratedArtifacts();
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
catch (error) {
|
|
144
|
+
console.error(chalk.red(`❌ Error removing public/${relativePath}:`), error);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
async findSourcePublicAsset(relativePath) {
|
|
148
|
+
return this.sourceFs.findPublicAsset(relativePath);
|
|
149
|
+
}
|
|
150
|
+
async writePublicAggregate(relativePath, content) {
|
|
151
|
+
const sourcePath = await this.findSourcePublicAsset(relativePath);
|
|
152
|
+
const targetPath = this.publicOutputFilePath(relativePath);
|
|
153
|
+
if (sourcePath) {
|
|
154
|
+
console.warn(chalk.yellow(`⚠️ Skipping generated public/${relativePath}; a project public asset owns that path`));
|
|
155
|
+
await this.copyRegularPublicFile(sourcePath, targetPath);
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
await fs.ensureDir(path.dirname(targetPath));
|
|
159
|
+
await writeFileAtomic(targetPath, content);
|
|
160
|
+
}
|
|
161
|
+
async syncMcpManifest(baseUrl, siteName) {
|
|
162
|
+
const relativePath = ".well-known/mcp.json";
|
|
163
|
+
const outputPath = this.publicOutputFilePath(relativePath);
|
|
164
|
+
const sourcePath = await this.findSourcePublicAsset(relativePath);
|
|
165
|
+
if (sourcePath) {
|
|
166
|
+
console.warn(chalk.yellow(`⚠️ Skipping generated public/${relativePath}; a project public asset owns that path`));
|
|
167
|
+
await this.copyRegularPublicFile(sourcePath, outputPath);
|
|
168
|
+
}
|
|
169
|
+
else if (baseUrl) {
|
|
170
|
+
const content = JSON.stringify({
|
|
171
|
+
mcpServers: {
|
|
172
|
+
[siteDocsSlug(siteName)]: {
|
|
173
|
+
url: `${baseUrl}/api/mcp`,
|
|
174
|
+
transport: "streamable-http",
|
|
175
|
+
},
|
|
176
|
+
},
|
|
177
|
+
}, null, 2) + "\n";
|
|
178
|
+
await fs.ensureDir(path.dirname(outputPath));
|
|
179
|
+
await writeFileAtomic(outputPath, content);
|
|
180
|
+
}
|
|
181
|
+
else if (await fs.pathExists(outputPath)) {
|
|
182
|
+
await fs.remove(outputPath);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
async syncLlmsPageFiles(pages, baseUrl) {
|
|
186
|
+
const publicDir = this.outputPath("public");
|
|
187
|
+
const nextRelativePaths = new Set();
|
|
188
|
+
await Promise.all(pages.map(async (page) => {
|
|
189
|
+
const relativePath = page.slug === "" ? "index.md" : `${page.slug}.md`;
|
|
190
|
+
if (isPublicAggregate(relativePath))
|
|
191
|
+
return;
|
|
192
|
+
const sourcePath = await this.findSourcePublicAsset(relativePath);
|
|
193
|
+
if (sourcePath) {
|
|
194
|
+
console.warn(chalk.yellow(`⚠️ Skipping generated public/${relativePath}; a project public asset owns that path`));
|
|
195
|
+
const targetPath = this.publicOutputFilePath(relativePath);
|
|
196
|
+
await this.copyRegularPublicFile(sourcePath, targetPath);
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
const targetPath = this.publicOutputFilePath(relativePath);
|
|
200
|
+
await fs.ensureDir(path.dirname(targetPath));
|
|
201
|
+
await writeFileAtomic(targetPath, llmsPageTemplate(page, baseUrl));
|
|
202
|
+
nextRelativePaths.add(relativePath);
|
|
203
|
+
}));
|
|
204
|
+
const previousRelativePaths = this.artifacts.llmsPageFiles();
|
|
205
|
+
for (const stale of previousRelativePaths) {
|
|
206
|
+
if (!nextRelativePaths.has(stale)) {
|
|
207
|
+
try {
|
|
208
|
+
if (isPublicAggregate(stale))
|
|
209
|
+
continue;
|
|
210
|
+
if (await this.findSourcePublicAsset(stale))
|
|
211
|
+
continue;
|
|
212
|
+
const stalePath = resolveOutputPath(publicDir, stale);
|
|
213
|
+
if (await fs.pathExists(stalePath)) {
|
|
214
|
+
await fs.remove(stalePath);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
catch {
|
|
218
|
+
// ignore
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
this.artifacts.replaceLlmsPageFiles(nextRelativePaths);
|
|
223
|
+
await this.artifacts.save();
|
|
224
|
+
}
|
|
225
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { SectionConfig } from "../lib/types.js";
|
|
2
|
+
export interface SectionDocument {
|
|
3
|
+
filePath: string;
|
|
4
|
+
frontmatter: Record<string, any>;
|
|
5
|
+
}
|
|
6
|
+
export interface SectionRoute {
|
|
7
|
+
sectionSlug: string;
|
|
8
|
+
pageSlug: string;
|
|
9
|
+
}
|
|
10
|
+
export declare function validateSectionsConfig(parsed: unknown): SectionConfig[] | null;
|
|
11
|
+
export declare function discoverSections(documents: SectionDocument[]): SectionConfig[] | null;
|
|
12
|
+
export declare function addApiReferenceSection(sections: SectionConfig[] | null, hasApiReference: boolean, apiBaseSlug: string): SectionConfig[] | null;
|
|
13
|
+
export declare function determineSectionRoute(filePath: string, frontmatter: Record<string, any>, sections: SectionConfig[] | null): SectionRoute;
|