doccupine 0.0.148 → 0.0.150
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/dist/generator/app-scaffolder.js +1 -0
- package/dist/generator/icon-asset-manager.d.ts +13 -0
- package/dist/generator/icon-asset-manager.js +87 -0
- package/dist/generator/page-renderer.js +14 -3
- package/dist/generator/project-config-repository.d.ts +3 -1
- package/dist/generator/project-config-repository.js +9 -1
- package/dist/generator/public-asset-manager.d.ts +1 -0
- package/dist/generator/public-asset-manager.js +11 -0
- package/dist/generator/watch-coordinator.d.ts +2 -0
- package/dist/generator/watch-coordinator.js +31 -2
- package/dist/lib/generated-artifacts.d.ts +3 -0
- package/dist/lib/generated-artifacts.js +23 -0
- package/dist/lib/layout.js +32 -4
- package/dist/lib/metadata.js +8 -8
- package/dist/lib/output-safety.js +1 -0
- package/dist/lib/structures.js +2 -0
- package/dist/mdx-to-nextjs-generator.d.ts +2 -0
- package/dist/mdx-to-nextjs-generator.js +10 -1
- package/dist/templates/app/theme.d.ts +1 -1
- package/dist/templates/app/theme.js +1 -2
- package/dist/templates/components/layout/CherryThemeProvider.d.ts +1 -1
- package/dist/templates/components/layout/CherryThemeProvider.js +33 -30
- package/dist/templates/components/layout/DocsComponents.d.ts +1 -1
- package/dist/templates/components/layout/DocsComponents.js +6 -5
- package/dist/templates/mdx/commands.mdx.d.ts +1 -1
- package/dist/templates/mdx/commands.mdx.js +17 -1
- package/dist/templates/mdx/globals.mdx.d.ts +1 -1
- package/dist/templates/mdx/globals.mdx.js +27 -4
- package/dist/templates/mdx/media-and-assets.mdx.d.ts +1 -1
- package/dist/templates/mdx/media-and-assets.mdx.js +3 -7
- package/dist/templates/mdx/theme.mdx.d.ts +1 -1
- package/dist/templates/mdx/theme.mdx.js +2 -0
- package/dist/templates/package.js +4 -4
- package/dist/templates/utils/icons.d.ts +1 -0
- package/dist/templates/utils/icons.js +44 -0
- package/package.json +2 -2
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { GeneratedArtifacts } from "../lib/generated-artifacts.js";
|
|
2
|
+
import { SecureSourceFs } from "./secure-source-fs.js";
|
|
3
|
+
export declare const ICON_SOURCE_FILES: readonly ["icon.png", "icon-dark.png", "apple-icon.png"];
|
|
4
|
+
export declare class IconAssetManager {
|
|
5
|
+
private readonly rootDir;
|
|
6
|
+
private readonly outputDir;
|
|
7
|
+
private readonly artifacts;
|
|
8
|
+
private readonly sourceFs;
|
|
9
|
+
private readonly readIconSourceFile;
|
|
10
|
+
constructor(rootDir: string, outputDir: string, artifacts: GeneratedArtifacts, sourceFs: SecureSourceFs, readIconSourceFile: (fileName: string) => Promise<Buffer | null>);
|
|
11
|
+
private outputPath;
|
|
12
|
+
syncIconFiles(): Promise<void>;
|
|
13
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import chalk from "chalk";
|
|
2
|
+
import fs from "fs-extra";
|
|
3
|
+
import { createHash } from "node:crypto";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { resolveOutputPath } from "../lib/output-safety.js";
|
|
6
|
+
import { writeFileAtomic } from "../lib/utils.js";
|
|
7
|
+
// Next.js-style icon file conventions, read from the project root. The files
|
|
8
|
+
// are copied to the generated public/ directory and referenced through
|
|
9
|
+
// icons.json instead of Next's app/ file convention, because file-based icons
|
|
10
|
+
// override config-based metadata entirely and have no dark-variant form.
|
|
11
|
+
export const ICON_SOURCE_FILES = [
|
|
12
|
+
"icon.png",
|
|
13
|
+
"icon-dark.png",
|
|
14
|
+
"apple-icon.png",
|
|
15
|
+
];
|
|
16
|
+
const ICON_MANIFEST_KEYS = {
|
|
17
|
+
"icon.png": "icon",
|
|
18
|
+
"icon-dark.png": "iconDark",
|
|
19
|
+
"apple-icon.png": "appleIcon",
|
|
20
|
+
};
|
|
21
|
+
const ICONS_JSON_FILE = "icons.json";
|
|
22
|
+
export class IconAssetManager {
|
|
23
|
+
rootDir;
|
|
24
|
+
outputDir;
|
|
25
|
+
artifacts;
|
|
26
|
+
sourceFs;
|
|
27
|
+
readIconSourceFile;
|
|
28
|
+
constructor(rootDir, outputDir, artifacts, sourceFs, readIconSourceFile) {
|
|
29
|
+
this.rootDir = rootDir;
|
|
30
|
+
this.outputDir = outputDir;
|
|
31
|
+
this.artifacts = artifacts;
|
|
32
|
+
this.sourceFs = sourceFs;
|
|
33
|
+
this.readIconSourceFile = readIconSourceFile;
|
|
34
|
+
}
|
|
35
|
+
outputPath(...segments) {
|
|
36
|
+
return resolveOutputPath(this.outputDir, ...segments);
|
|
37
|
+
}
|
|
38
|
+
async syncIconFiles() {
|
|
39
|
+
console.log(chalk.blue(`🔍 Checking for icon files...`));
|
|
40
|
+
const sources = new Map();
|
|
41
|
+
for (const fileName of ICON_SOURCE_FILES) {
|
|
42
|
+
const data = await this.readIconSourceFile(fileName);
|
|
43
|
+
if (data !== null)
|
|
44
|
+
sources.set(fileName, data);
|
|
45
|
+
}
|
|
46
|
+
// A root icon file and a user public/ file would publish the same URL.
|
|
47
|
+
// Refuse before writing anything so a failed sync leaves output untouched.
|
|
48
|
+
for (const fileName of sources.keys()) {
|
|
49
|
+
const publicSource = await this.sourceFs.findPublicAsset(fileName);
|
|
50
|
+
if (publicSource !== null) {
|
|
51
|
+
throw new Error(`Conflicting icon sources: ${path.join(this.rootDir, fileName)} ` +
|
|
52
|
+
`and ${publicSource} both publish public/${fileName}. ` +
|
|
53
|
+
`Remove one of them.`);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
if (sources.has("icon-dark.png") && !sources.has("icon.png")) {
|
|
57
|
+
console.warn(chalk.yellow(`⚠️ icon-dark.png found without icon.png - the dark variant only ` +
|
|
58
|
+
`applies alongside a light icon, skipping it`));
|
|
59
|
+
sources.delete("icon-dark.png");
|
|
60
|
+
}
|
|
61
|
+
const manifest = {};
|
|
62
|
+
const nextFiles = new Set();
|
|
63
|
+
for (const fileName of ICON_SOURCE_FILES) {
|
|
64
|
+
const data = sources.get(fileName);
|
|
65
|
+
if (!data)
|
|
66
|
+
continue;
|
|
67
|
+
const hash = createHash("sha256").update(data).digest("hex").slice(0, 8);
|
|
68
|
+
await writeFileAtomic(this.outputPath("public", fileName), data);
|
|
69
|
+
manifest[ICON_MANIFEST_KEYS[fileName]] = `/${fileName}?v=${hash}`;
|
|
70
|
+
nextFiles.add(fileName);
|
|
71
|
+
}
|
|
72
|
+
await writeFileAtomic(this.outputPath(ICONS_JSON_FILE), nextFiles.size > 0 ? `${JSON.stringify(manifest, null, 2)}\n` : `{}\n`);
|
|
73
|
+
for (const stale of this.artifacts.iconFiles()) {
|
|
74
|
+
if (!nextFiles.has(stale)) {
|
|
75
|
+
await fs.remove(this.outputPath("public", stale));
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
this.artifacts.replaceIconFiles(nextFiles);
|
|
79
|
+
await this.artifacts.save();
|
|
80
|
+
if (nextFiles.size > 0) {
|
|
81
|
+
console.log(chalk.green(` ✓ Copied ${[...nextFiles].join(", ")} to Next.js app`));
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
console.log(chalk.gray(` ✗ no icon files found, skipping`));
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
@@ -68,9 +68,10 @@ export function renderMdxPage(mdxFile, options) {
|
|
|
68
68
|
: inlineDocs.length + 6 <= 80
|
|
69
69
|
? inlineDocs
|
|
70
70
|
: `<Docs\n${docsAttrs.map((attr) => ` ${attr}`).join("\n")}\n />`;
|
|
71
|
+
const iconsImport = iconsImportLine(metadataBlock, jsonLd.declarations);
|
|
71
72
|
const pageContent = `import { Metadata } from "next";
|
|
72
73
|
import { Docs } from "@/components/Docs";
|
|
73
|
-
import { config } from "@/utils/config";${apiImport}
|
|
74
|
+
import { config } from "@/utils/config";${iconsImport}${apiImport}
|
|
74
75
|
|
|
75
76
|
const content = \`${escapeTemplateContent(mdxFile.content)}\`;
|
|
76
77
|
${apiConst}
|
|
@@ -112,6 +113,14 @@ export default function Page() {
|
|
|
112
113
|
: { action: "remove" };
|
|
113
114
|
return { pageContent, rssRoute };
|
|
114
115
|
}
|
|
116
|
+
// Pages with a frontmatter icon and image reference neither export; scanning
|
|
117
|
+
// only the generated metadata/JSON-LD code keeps their imports unused-free.
|
|
118
|
+
function iconsImportLine(...codeBlocks) {
|
|
119
|
+
const names = ["primaryIconUrl", "siteIcons"].filter((name) => codeBlocks.some((block) => block.includes(name)));
|
|
120
|
+
return names.length > 0
|
|
121
|
+
? `\nimport { ${names.join(", ")} } from "@/utils/icons";`
|
|
122
|
+
: "";
|
|
123
|
+
}
|
|
115
124
|
export function renderHomepage(indexMDX, apiOperation) {
|
|
116
125
|
const updates = indexMDX ? parseUpdateBlocks(indexMDX.content) : [];
|
|
117
126
|
const hasFeed = updates.length > 0;
|
|
@@ -150,9 +159,10 @@ export function renderHomepage(indexMDX, apiOperation) {
|
|
|
150
159
|
: showRssButton
|
|
151
160
|
? `<Docs content={content} sourcePath="index.mdx" rssHref={"/rss.xml"} />`
|
|
152
161
|
: `<Docs content={content} sourcePath="index.mdx" />`;
|
|
162
|
+
const iconsImport = iconsImportLine(metadataBlock, homeJsonLd.declarations);
|
|
153
163
|
const pageContent = `import { Metadata } from "next";
|
|
154
164
|
import { Docs } from "@/components/Docs";
|
|
155
|
-
import { config } from "@/utils/config";${apiImport}
|
|
165
|
+
import { config } from "@/utils/config";${iconsImport}${apiImport}
|
|
156
166
|
|
|
157
167
|
${indexMDX ? `const content = \`${escapeTemplateContent(indexMDX.content)}\`;` : `const content = null;`}
|
|
158
168
|
${apiConst}
|
|
@@ -227,9 +237,10 @@ export function renderSectionPage(sectionSlug, frontmatter, mdxContent, sourcePa
|
|
|
227
237
|
const docsElement = inlineDocs.length + 6 <= 80
|
|
228
238
|
? inlineDocs
|
|
229
239
|
: `<Docs\n${docsAttrs.map((attr) => ` ${attr}`).join("\n")}\n />`;
|
|
240
|
+
const iconsImport = iconsImportLine(metadataBlock, sectionJsonLd.declarations);
|
|
230
241
|
const pageContent = `import { Metadata } from "next";
|
|
231
242
|
import { Docs } from "@/components/Docs";
|
|
232
|
-
import { config } from "@/utils/config"
|
|
243
|
+
import { config } from "@/utils/config";${iconsImport}
|
|
233
244
|
|
|
234
245
|
const content = \`${escapeTemplateContent(mdxContent)}\`;
|
|
235
246
|
|
|
@@ -7,14 +7,16 @@ export declare class ProjectConfigRepository {
|
|
|
7
7
|
private readonly configFiles;
|
|
8
8
|
private readonly fontConfigFile;
|
|
9
9
|
private readonly analyticsConfigFile;
|
|
10
|
+
private readonly iconFiles;
|
|
10
11
|
private sourceSnapshot;
|
|
11
|
-
constructor(rootDir: string, outputDir: string, sourceFs: SecureSourceFs, configFiles: readonly string[], fontConfigFile: string, analyticsConfigFile: string);
|
|
12
|
+
constructor(rootDir: string, outputDir: string, sourceFs: SecureSourceFs, configFiles: readonly string[], fontConfigFile: string, analyticsConfigFile: string, iconFiles?: readonly string[]);
|
|
12
13
|
private outputPath;
|
|
13
14
|
private hasRootSourceFile;
|
|
14
15
|
private readRootSourceFile;
|
|
15
16
|
private copyRootSourceFile;
|
|
16
17
|
private readOptionalRootSourceFile;
|
|
17
18
|
readConfigFile(): Promise<string | null>;
|
|
19
|
+
readOptionalRootBinaryFile(fileName: string, label: string): Promise<Buffer | null>;
|
|
18
20
|
preflightSourceFiles(): Promise<void>;
|
|
19
21
|
sourceSnapshotStates(): Record<string, string>;
|
|
20
22
|
clearSourceSnapshot(): void;
|
|
@@ -18,14 +18,16 @@ export class ProjectConfigRepository {
|
|
|
18
18
|
configFiles;
|
|
19
19
|
fontConfigFile;
|
|
20
20
|
analyticsConfigFile;
|
|
21
|
+
iconFiles;
|
|
21
22
|
sourceSnapshot = null;
|
|
22
|
-
constructor(rootDir, outputDir, sourceFs, configFiles, fontConfigFile, analyticsConfigFile) {
|
|
23
|
+
constructor(rootDir, outputDir, sourceFs, configFiles, fontConfigFile, analyticsConfigFile, iconFiles = []) {
|
|
23
24
|
this.rootDir = rootDir;
|
|
24
25
|
this.outputDir = outputDir;
|
|
25
26
|
this.sourceFs = sourceFs;
|
|
26
27
|
this.configFiles = configFiles;
|
|
27
28
|
this.fontConfigFile = fontConfigFile;
|
|
28
29
|
this.analyticsConfigFile = analyticsConfigFile;
|
|
30
|
+
this.iconFiles = iconFiles;
|
|
29
31
|
}
|
|
30
32
|
outputPath(...segments) {
|
|
31
33
|
return resolveOutputPath(this.outputDir, ...segments);
|
|
@@ -61,11 +63,17 @@ export class ProjectConfigRepository {
|
|
|
61
63
|
async readConfigFile() {
|
|
62
64
|
return this.readOptionalRootSourceFile("config.json", "config source");
|
|
63
65
|
}
|
|
66
|
+
async readOptionalRootBinaryFile(fileName, label) {
|
|
67
|
+
if (!(await this.hasRootSourceFile(fileName)))
|
|
68
|
+
return null;
|
|
69
|
+
return this.readRootSourceFile(path.join(this.rootDir, fileName), label);
|
|
70
|
+
}
|
|
64
71
|
async preflightSourceFiles() {
|
|
65
72
|
const sources = [
|
|
66
73
|
...this.configFiles.map((fileName) => [fileName, "config source"]),
|
|
67
74
|
[this.fontConfigFile, "font source"],
|
|
68
75
|
[this.analyticsConfigFile, "analytics source"],
|
|
76
|
+
...this.iconFiles.map((fileName) => [fileName, "icon source"]),
|
|
69
77
|
];
|
|
70
78
|
const snapshot = new Map();
|
|
71
79
|
for (const [fileName, label] of sources) {
|
|
@@ -10,6 +10,7 @@ export declare class PublicAssetManager {
|
|
|
10
10
|
private outputPath;
|
|
11
11
|
publicOutputFilePath(relativePath: string): string;
|
|
12
12
|
copyRegularPublicFile(sourcePath: string, destPath: string): Promise<void>;
|
|
13
|
+
private assertNotTrackedIconFile;
|
|
13
14
|
copyPublicFiles(): Promise<void>;
|
|
14
15
|
handlePublicFileChange(filePath: string, restoreGeneratedArtifacts: () => Promise<void>): Promise<void>;
|
|
15
16
|
handlePublicFileDelete(filePath: string, copyCurrentSource: () => Promise<void>, restoreGeneratedArtifacts: () => Promise<void>): Promise<void>;
|
|
@@ -51,6 +51,15 @@ export class PublicAssetManager {
|
|
|
51
51
|
const { data } = await this.sourceFs.readPublicSourceFile(sourcePath);
|
|
52
52
|
await writeFileAtomic(destPath, data);
|
|
53
53
|
}
|
|
54
|
+
// A tracked root icon file already publishes this URL. Refuse the copy so
|
|
55
|
+
// the two sources cannot silently overwrite each other.
|
|
56
|
+
assertNotTrackedIconFile(destRelativePath, relativePath) {
|
|
57
|
+
if (this.artifacts.iconFiles().has(destRelativePath)) {
|
|
58
|
+
throw new Error(`Conflicting icon sources: ${path.join(this.rootDir, destRelativePath)} ` +
|
|
59
|
+
`and ${path.join(this.rootDir, "public", relativePath)} both ` +
|
|
60
|
+
`publish public/${destRelativePath}. Remove one of them.`);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
54
63
|
async copyPublicFiles() {
|
|
55
64
|
const publicDir = path.join(this.rootDir, "public");
|
|
56
65
|
const previousFiles = this.artifacts.publicFiles();
|
|
@@ -69,6 +78,7 @@ export class PublicAssetManager {
|
|
|
69
78
|
const nextByFoldedPath = new Map();
|
|
70
79
|
for (const relativePath of files) {
|
|
71
80
|
const destRelativePath = publicDestinationRelativePath(relativePath);
|
|
81
|
+
this.assertNotTrackedIconFile(destRelativePath, relativePath);
|
|
72
82
|
nextFiles.add(destRelativePath);
|
|
73
83
|
nextByFoldedPath.set(destRelativePath.toLowerCase(), destRelativePath);
|
|
74
84
|
}
|
|
@@ -101,6 +111,7 @@ export class PublicAssetManager {
|
|
|
101
111
|
const destRelativePath = publicDestinationRelativePath(relativePath);
|
|
102
112
|
const destPath = this.publicOutputFilePath(destRelativePath);
|
|
103
113
|
try {
|
|
114
|
+
this.assertNotTrackedIconFile(destRelativePath, relativePath);
|
|
104
115
|
await this.copyRegularPublicFile(filePath, destPath);
|
|
105
116
|
const publicFiles = this.artifacts.publicFiles();
|
|
106
117
|
publicFiles.add(destRelativePath);
|
|
@@ -15,6 +15,7 @@ interface WatchCoordinatorCallbacks {
|
|
|
15
15
|
copyPublicFiles(): Promise<void>;
|
|
16
16
|
handlePublicFileChange(filePath: string): Promise<void>;
|
|
17
17
|
handlePublicFileDelete(filePath: string): Promise<void>;
|
|
18
|
+
syncIconFiles(): Promise<void>;
|
|
18
19
|
processAllMDXFiles(): Promise<void>;
|
|
19
20
|
}
|
|
20
21
|
interface WatchCoordinatorOptions {
|
|
@@ -24,6 +25,7 @@ interface WatchCoordinatorOptions {
|
|
|
24
25
|
fontConfigFile: string;
|
|
25
26
|
analyticsConfigFile: string;
|
|
26
27
|
doccupineConfigFile: string;
|
|
28
|
+
iconFiles: readonly string[];
|
|
27
29
|
sourceFs: SecureSourceFs;
|
|
28
30
|
getOpenApiSpecs(): readonly NormalizedOpenApiSpec[];
|
|
29
31
|
getOpenApiSourceFiles(): readonly string[];
|
|
@@ -92,11 +92,15 @@ export class WatchCoordinator {
|
|
|
92
92
|
}
|
|
93
93
|
}
|
|
94
94
|
async captureSourceSnapshot() {
|
|
95
|
-
const { analyticsConfigFile, configFiles, doccupineConfigFile, fontConfigFile, getOpenApiSpecs, getOpenApiSourceFiles, rootDir, sourceFs, watchDir, } = this.options;
|
|
95
|
+
const { analyticsConfigFile, configFiles, doccupineConfigFile, fontConfigFile, getOpenApiSpecs, getOpenApiSourceFiles, iconFiles, rootDir, sourceFs, watchDir, } = this.options;
|
|
96
96
|
const configs = Object.fromEntries(await Promise.all(configFiles.map(async (fileName) => [
|
|
97
97
|
fileName,
|
|
98
98
|
await sourceFs.pathState(path.join(rootDir, fileName), true),
|
|
99
99
|
])));
|
|
100
|
+
const icons = Object.fromEntries(await Promise.all(iconFiles.map(async (fileName) => [
|
|
101
|
+
fileName,
|
|
102
|
+
await sourceFs.pathState(path.join(rootDir, fileName), true),
|
|
103
|
+
])));
|
|
100
104
|
const openApiSourceFiles = getOpenApiSourceFiles();
|
|
101
105
|
const openapi = (await Promise.all((openApiSourceFiles.length > 0
|
|
102
106
|
? openApiSourceFiles
|
|
@@ -111,6 +115,7 @@ export class WatchCoordinator {
|
|
|
111
115
|
doccupine: await sourceFs.pathState(path.join(rootDir, doccupineConfigFile), true),
|
|
112
116
|
public: await sourceFs.treeState(path.join(rootDir, "public"), () => true),
|
|
113
117
|
openapi,
|
|
118
|
+
icons,
|
|
114
119
|
};
|
|
115
120
|
}
|
|
116
121
|
async establishSourceSnapshot(projectSourceStates = {}, openApiSourceState, doccupineSourceState) {
|
|
@@ -120,6 +125,11 @@ export class WatchCoordinator {
|
|
|
120
125
|
snapshot.configs[fileName] = projectSourceStates[fileName];
|
|
121
126
|
}
|
|
122
127
|
}
|
|
128
|
+
for (const fileName of this.options.iconFiles) {
|
|
129
|
+
if (projectSourceStates[fileName] !== undefined) {
|
|
130
|
+
snapshot.icons[fileName] = projectSourceStates[fileName];
|
|
131
|
+
}
|
|
132
|
+
}
|
|
123
133
|
snapshot.font =
|
|
124
134
|
projectSourceStates[this.options.fontConfigFile] ?? snapshot.font;
|
|
125
135
|
snapshot.analytics =
|
|
@@ -130,7 +140,7 @@ export class WatchCoordinator {
|
|
|
130
140
|
this.sourceSnapshot = snapshot;
|
|
131
141
|
}
|
|
132
142
|
async reconcileWatchedSources(previous, current) {
|
|
133
|
-
const { analyticsConfigFile, callbacks, configFiles, doccupineConfigFile, fontConfigFile, getOpenApiSpecs, rootDir, } = this.options;
|
|
143
|
+
const { analyticsConfigFile, callbacks, configFiles, doccupineConfigFile, fontConfigFile, getOpenApiSpecs, iconFiles, rootDir, } = this.options;
|
|
134
144
|
const doccupineConfigPath = path.join(rootDir, doccupineConfigFile);
|
|
135
145
|
const doccupineChanged = previous === null || previous.doccupine !== current.doccupine;
|
|
136
146
|
if (doccupineChanged && (await fs.pathExists(doccupineConfigPath))) {
|
|
@@ -181,6 +191,10 @@ export class WatchCoordinator {
|
|
|
181
191
|
}
|
|
182
192
|
await callbacks.copyPublicFiles();
|
|
183
193
|
}
|
|
194
|
+
if (previous === null ||
|
|
195
|
+
iconFiles.some((fileName) => previous.icons[fileName] !== current.icons[fileName])) {
|
|
196
|
+
await callbacks.syncIconFiles();
|
|
197
|
+
}
|
|
184
198
|
if (getOpenApiSpecs().length > 0 &&
|
|
185
199
|
(previous === null || previous.openapi !== current.openapi)) {
|
|
186
200
|
await callbacks.handleOpenApiChange();
|
|
@@ -366,6 +380,11 @@ export class WatchCoordinator {
|
|
|
366
380
|
}
|
|
367
381
|
});
|
|
368
382
|
};
|
|
383
|
+
const isRootIconFile = (filePath) => this.options.iconFiles.includes(path.basename(filePath)) &&
|
|
384
|
+
path.dirname(filePath) === rootDir;
|
|
385
|
+
const queueIconSync = () => {
|
|
386
|
+
this.enqueueMutation("Error syncing icon files", () => callbacks.syncIconFiles());
|
|
387
|
+
};
|
|
369
388
|
this.rootDirWatcher
|
|
370
389
|
.on("addDir", (dirPath) => {
|
|
371
390
|
if (path.basename(dirPath) === "public" &&
|
|
@@ -376,6 +395,16 @@ export class WatchCoordinator {
|
|
|
376
395
|
.on("add", (filePath) => {
|
|
377
396
|
if (isPathInside(publicDir, filePath))
|
|
378
397
|
queuePublicWatcherStart();
|
|
398
|
+
if (isRootIconFile(filePath))
|
|
399
|
+
queueIconSync();
|
|
400
|
+
})
|
|
401
|
+
.on("change", (filePath) => {
|
|
402
|
+
if (isRootIconFile(filePath))
|
|
403
|
+
queueIconSync();
|
|
404
|
+
})
|
|
405
|
+
.on("unlink", (filePath) => {
|
|
406
|
+
if (isRootIconFile(filePath))
|
|
407
|
+
queueIconSync();
|
|
379
408
|
})
|
|
380
409
|
.on("unlinkDir", (dirPath) => {
|
|
381
410
|
if (path.basename(dirPath) !== "public" ||
|
|
@@ -9,6 +9,7 @@ export declare class GeneratedArtifacts {
|
|
|
9
9
|
private routes;
|
|
10
10
|
private llmsFiles;
|
|
11
11
|
private publicFilePaths;
|
|
12
|
+
private iconFilePaths;
|
|
12
13
|
constructor(outputDir: string);
|
|
13
14
|
private manifestPath;
|
|
14
15
|
private routeKey;
|
|
@@ -30,6 +31,8 @@ export declare class GeneratedArtifacts {
|
|
|
30
31
|
replaceLlmsPageFiles(files: Iterable<string>): void;
|
|
31
32
|
publicFiles(): Set<string>;
|
|
32
33
|
replacePublicFiles(files: Iterable<string>): void;
|
|
34
|
+
iconFiles(): Set<string>;
|
|
35
|
+
replaceIconFiles(files: Iterable<string>): void;
|
|
33
36
|
private writeManifest;
|
|
34
37
|
save(): Promise<void>;
|
|
35
38
|
}
|
|
@@ -47,6 +47,7 @@ export class GeneratedArtifacts {
|
|
|
47
47
|
routes = new Map();
|
|
48
48
|
llmsFiles = new Set();
|
|
49
49
|
publicFilePaths = new Set();
|
|
50
|
+
iconFilePaths = new Set();
|
|
50
51
|
constructor(outputDir) {
|
|
51
52
|
this.outputDir = outputDir;
|
|
52
53
|
}
|
|
@@ -60,6 +61,7 @@ export class GeneratedArtifacts {
|
|
|
60
61
|
this.routes.clear();
|
|
61
62
|
this.llmsFiles.clear();
|
|
62
63
|
this.publicFilePaths.clear();
|
|
64
|
+
this.iconFilePaths.clear();
|
|
63
65
|
const manifestContent = await readOutputFileIfPresent(this.outputDir, MANIFEST_FILE);
|
|
64
66
|
if (manifestContent !== null) {
|
|
65
67
|
try {
|
|
@@ -85,6 +87,13 @@ export class GeneratedArtifacts {
|
|
|
85
87
|
this.publicFilePaths.add(file);
|
|
86
88
|
}
|
|
87
89
|
}
|
|
90
|
+
if (Array.isArray(parsed.iconFiles)) {
|
|
91
|
+
for (const value of parsed.iconFiles) {
|
|
92
|
+
const file = normalizePublicFile(value);
|
|
93
|
+
if (file)
|
|
94
|
+
this.iconFilePaths.add(file);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
88
97
|
}
|
|
89
98
|
catch {
|
|
90
99
|
// A corrupt manifest owns nothing. Generated output is rebuilt safely.
|
|
@@ -177,12 +186,26 @@ export class GeneratedArtifacts {
|
|
|
177
186
|
}
|
|
178
187
|
this.publicFilePaths = next;
|
|
179
188
|
}
|
|
189
|
+
iconFiles() {
|
|
190
|
+
return new Set(this.iconFilePaths);
|
|
191
|
+
}
|
|
192
|
+
replaceIconFiles(files) {
|
|
193
|
+
const next = new Set();
|
|
194
|
+
for (const value of files) {
|
|
195
|
+
const file = normalizePublicFile(value);
|
|
196
|
+
if (!file)
|
|
197
|
+
throw new Error("Refusing to record an unsafe icon path");
|
|
198
|
+
next.add(file);
|
|
199
|
+
}
|
|
200
|
+
this.iconFilePaths = next;
|
|
201
|
+
}
|
|
180
202
|
async writeManifest(routes) {
|
|
181
203
|
const manifest = {
|
|
182
204
|
schemaVersion: 2,
|
|
183
205
|
routes: [...routes.values()].sort((a, b) => `${a.kind}:${a.source}`.localeCompare(`${b.kind}:${b.source}`)),
|
|
184
206
|
llmsPageFiles: [...this.llmsFiles].sort(),
|
|
185
207
|
publicFiles: [...this.publicFilePaths].sort(),
|
|
208
|
+
iconFiles: [...this.iconFilePaths].sort(),
|
|
186
209
|
};
|
|
187
210
|
await writeFileAtomic(this.manifestPath(MANIFEST_FILE), `${JSON.stringify(manifest, null, 2)}\n`);
|
|
188
211
|
}
|
package/dist/lib/layout.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { DEFAULT_DESCRIPTION,
|
|
1
|
+
import { DEFAULT_DESCRIPTION, DEFAULT_OG_IMAGE, DEFAULT_SITE_NAME, } from "./constants.js";
|
|
2
2
|
function formatObjectArray(items) {
|
|
3
3
|
const MAX_WIDTH = 80;
|
|
4
4
|
const formatted = items.map((item) => {
|
|
@@ -89,7 +89,22 @@ function fontDeclLine(fontConfig) {
|
|
|
89
89
|
// The mode comes from the "theme" cookie, falling back to the OS preference
|
|
90
90
|
// and seeding the cookie so Cherry's ClientThemeProvider reconciles against
|
|
91
91
|
// the same answer on mount instead of flipping the class back.
|
|
92
|
-
|
|
92
|
+
//
|
|
93
|
+
// The script also retargets the theme-color meta from the resolved mode. The
|
|
94
|
+
// meta itself is server-rendered in the layout below, because Safari only
|
|
95
|
+
// tints reliably from a tag present in the parsed HTML - a meta created by
|
|
96
|
+
// script is honored inconsistently or not at all - while it does track
|
|
97
|
+
// content updates to an existing tag. It cannot be emitted as
|
|
98
|
+
// OS-scheme-scoped SSR metas (Next's viewport export): the site's mode is
|
|
99
|
+
// cookie-based, so a dark site on a light-OS device would show the wrong
|
|
100
|
+
// browser chrome until hydration - a visible flash on every load. Pages are
|
|
101
|
+
// static, so the server cannot know the mode either: the SSR value is the
|
|
102
|
+
// light primary and this script corrects it before first paint. The \${...}
|
|
103
|
+
// palette interpolations below are escaped so they resolve in the generated
|
|
104
|
+
// layout, at app build time, from the imported theme palettes. The token
|
|
105
|
+
// must match the provider's $themeColor (primary), so the pre-paint chrome
|
|
106
|
+
// color equals what Cherry syncs after hydration.
|
|
107
|
+
const THEME_INIT_SCRIPT = `(function(){try{var c=document.cookie.split(";").map(function(s){return s.trim();}).find(function(s){return s.indexOf("theme=")===0;});var v=c?c.split("=")[1]:null;var d=v?v==="dark":(window.matchMedia&&window.matchMedia("(prefers-color-scheme:dark)").matches);if(!v){document.cookie="theme="+(d?"dark":"light")+";path=/;max-age=31536000;SameSite=Lax";}var r=document.documentElement;r.dataset.theme=d?"dark":"light";if(d){r.classList.add("dark");}var m=document.querySelector('meta[name="theme-color"]');if(!m){m=document.createElement("meta");m.name="theme-color";document.head.appendChild(m);}m.content=d?"\${colorsDark.primary}":"\${colorsLight.primary}";}catch(e){}})();`;
|
|
93
108
|
/**
|
|
94
109
|
* Root layout ("app/layout.tsx"). Minimal shell: html/body, fonts, the theme
|
|
95
110
|
* provider stack, and (optionally) PostHog. It renders `children` directly so
|
|
@@ -103,9 +118,10 @@ export const rootLayoutTemplate = (fontConfig, analyticsEnabled = false) => {
|
|
|
103
118
|
return `import type { Metadata } from "next";
|
|
104
119
|
${fontImportLine(fontConfig)}
|
|
105
120
|
import { StyledComponentsRegistry } from "cherry-styled-components";
|
|
106
|
-
import { theme, themeDark } from "@/app/theme";
|
|
121
|
+
import { colorsDark, colorsLight, theme, themeDark } from "@/app/theme";
|
|
107
122
|
import { CherryThemeProvider } from "@/components/layout/CherryThemeProvider";
|
|
108
123
|
import { config } from "@/utils/config";
|
|
124
|
+
import { siteIcons } from "@/utils/icons";
|
|
109
125
|
${analyticsEnabled ? `import { PostHogProvider } from "@/components/PostHogProvider";\n` : ""}
|
|
110
126
|
${fontDeclLine(fontConfig)}
|
|
111
127
|
|
|
@@ -125,7 +141,7 @@ export const metadata: Metadata = {
|
|
|
125
141
|
description:
|
|
126
142
|
config.description ||
|
|
127
143
|
"${DEFAULT_DESCRIPTION}",
|
|
128
|
-
icons:
|
|
144
|
+
icons: siteIcons,
|
|
129
145
|
openGraph: {
|
|
130
146
|
title: config.name || "${DEFAULT_SITE_NAME}",
|
|
131
147
|
description:
|
|
@@ -143,6 +159,18 @@ export default function RootLayout({
|
|
|
143
159
|
return (
|
|
144
160
|
<html lang="en" suppressHydrationWarning>
|
|
145
161
|
<head>
|
|
162
|
+
{/* Server-rendered so Safari has a real tag to tint from: WebKit
|
|
163
|
+
honors a theme-color meta created by script inconsistently, but
|
|
164
|
+
reliably tracks content updates to an existing one. Pages are
|
|
165
|
+
static, so the value is the light primary; the blocking script
|
|
166
|
+
below corrects it before first paint and Cherry keeps it in sync
|
|
167
|
+
after hydration. suppressHydrationWarning: the script edits
|
|
168
|
+
content before React hydrates. */}
|
|
169
|
+
<meta
|
|
170
|
+
name="theme-color"
|
|
171
|
+
content={colorsLight.primary}
|
|
172
|
+
suppressHydrationWarning
|
|
173
|
+
/>
|
|
146
174
|
{/* Resolves dark mode before the first paint by stamping data-theme
|
|
147
175
|
on <html>, which flips the CSS variables in GlobalStyles.
|
|
148
176
|
Inlined as a plain <script> (not next/script) so it ships in the
|
package/dist/lib/metadata.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { DEFAULT_META_DESCRIPTION, DEFAULT_OG_IMAGE, DEFAULT_SITE_NAME, } from "./constants.js";
|
|
2
2
|
/**
|
|
3
3
|
* Returns a code snippet that:
|
|
4
4
|
* - declares a `jsonLd` object at runtime using the site's config,
|
|
@@ -18,13 +18,11 @@ export function generateJsonLdScript(opts) {
|
|
|
18
18
|
: opts.date
|
|
19
19
|
? JSON.stringify(opts.date)
|
|
20
20
|
: "undefined";
|
|
21
|
-
|
|
22
|
-
//
|
|
23
|
-
// after a string literal makes TypeScript fail the build with
|
|
24
|
-
// "This kind of expression is always truthy".
|
|
21
|
+
// When the page declares an image, it always wins; otherwise the site-wide
|
|
22
|
+
// primary icon (root icon files > config.icon > default) supplies the logo.
|
|
25
23
|
const faviconLine = opts.image
|
|
26
24
|
? `const faviconUrl = ${JSON.stringify(opts.image)};`
|
|
27
|
-
: `const faviconUrl =
|
|
25
|
+
: `const faviconUrl = primaryIconUrl;`;
|
|
28
26
|
// Indent 10 + "description: ".length(13) + ",".length(1) = 24. Prettier
|
|
29
27
|
// wraps the value to the next line (indent 12) once total exceeds 80.
|
|
30
28
|
const descriptionLine = descLiteral.length > 56
|
|
@@ -139,7 +137,9 @@ function buildAlternatesBlock(canonicalPath, rssPath) {
|
|
|
139
137
|
export function generateMetadataBlock(opts) {
|
|
140
138
|
const title = buildTitleExpression(opts);
|
|
141
139
|
const desc = buildFieldExpression(opts.description, "description", DEFAULT_META_DESCRIPTION);
|
|
142
|
-
|
|
140
|
+
// Frontmatter icon wins as a literal; otherwise the site-wide icon set
|
|
141
|
+
// built by utils/icons.ts (root icon files > config.icon > default) applies.
|
|
142
|
+
const icon = opts.icon ? JSON.stringify(String(opts.icon)) : "siteIcons";
|
|
143
143
|
const image = buildFieldExpression(opts.image, "image", DEFAULT_OG_IMAGE);
|
|
144
144
|
const canonical = buildAlternatesBlock(opts.canonicalPath, opts.rssPath);
|
|
145
145
|
const inlineTitleDeclaration = `const pageTitle = ${title};`;
|
|
@@ -166,7 +166,7 @@ ${imageLine}
|
|
|
166
166
|
export function generateRuntimeOnlyMetadataBlock() {
|
|
167
167
|
const title = `config.name || ${JSON.stringify(DEFAULT_SITE_NAME)}`;
|
|
168
168
|
const desc = `config.description || ${JSON.stringify(DEFAULT_META_DESCRIPTION)}`;
|
|
169
|
-
const icon =
|
|
169
|
+
const icon = "siteIcons";
|
|
170
170
|
const image = `config.image || ${JSON.stringify(DEFAULT_OG_IMAGE)}`;
|
|
171
171
|
const descriptionLine = formatProperty(2, "description", desc);
|
|
172
172
|
const iconLine = formatProperty(2, "icons", icon);
|
package/dist/lib/structures.js
CHANGED
|
@@ -90,6 +90,7 @@ import { orderNavItemsTemplate } from "../templates/utils/orderNavItems.js";
|
|
|
90
90
|
import { rateLimitTemplate } from "../templates/utils/rateLimit.js";
|
|
91
91
|
import { brandingTemplate } from "../templates/utils/branding.js";
|
|
92
92
|
import { configTemplate } from "../templates/utils/config.js";
|
|
93
|
+
import { iconsTemplate } from "../templates/utils/icons.js";
|
|
93
94
|
import { playgroundAllowlistTemplate } from "../templates/utils/playgroundAllowlist.js";
|
|
94
95
|
import { ssrfGuardTemplate } from "../templates/utils/ssrfGuard.js";
|
|
95
96
|
import { apiSnippetsTemplate } from "../templates/utils/apiSnippets.js";
|
|
@@ -205,6 +206,7 @@ export const appStructure = {
|
|
|
205
206
|
"utils/rateLimit.ts": rateLimitTemplate,
|
|
206
207
|
"utils/rehypeCodeMeta.ts": rehypeCodeMetaTemplate,
|
|
207
208
|
"utils/config.ts": configTemplate,
|
|
209
|
+
"utils/icons.ts": iconsTemplate,
|
|
208
210
|
"utils/playgroundAllowlist.ts": playgroundAllowlistTemplate,
|
|
209
211
|
"utils/ssrfGuard.ts": ssrfGuardTemplate,
|
|
210
212
|
"utils/apiSnippets.ts": apiSnippetsTemplate,
|
|
@@ -28,6 +28,7 @@ export declare class MDXToNextJSGenerator {
|
|
|
28
28
|
private sectionIndexGenerator;
|
|
29
29
|
private projectConfigRepository;
|
|
30
30
|
private publicAssetManager;
|
|
31
|
+
private iconAssetManager;
|
|
31
32
|
private watchCoordinator;
|
|
32
33
|
constructor(watchDir: string, outputDir: string, openApiSpecs?: NormalizedOpenApiSpec[], rootDir?: string);
|
|
33
34
|
private outputPath;
|
|
@@ -63,6 +64,7 @@ export declare class MDXToNextJSGenerator {
|
|
|
63
64
|
handleAnalyticsConfigChange(): Promise<void>;
|
|
64
65
|
handleAnalyticsConfigDelete(): Promise<void>;
|
|
65
66
|
copyPublicFiles(): Promise<void>;
|
|
67
|
+
syncIconFiles(): Promise<void>;
|
|
66
68
|
handlePublicFileChange(filePath: string): Promise<void>;
|
|
67
69
|
handlePublicFileDelete(filePath: string): Promise<void>;
|
|
68
70
|
startWatching(): Promise<void>;
|
|
@@ -18,6 +18,7 @@ import { GeneratedRouteManager } from "./generator/generated-route-manager.js";
|
|
|
18
18
|
import { MdxReconciliationCoordinator } from "./generator/mdx-reconciliation-coordinator.js";
|
|
19
19
|
import { ProjectConfigRepository } from "./generator/project-config-repository.js";
|
|
20
20
|
import { PublicAssetManager } from "./generator/public-asset-manager.js";
|
|
21
|
+
import { ICON_SOURCE_FILES, IconAssetManager, } from "./generator/icon-asset-manager.js";
|
|
21
22
|
import { WatchCoordinator } from "./generator/watch-coordinator.js";
|
|
22
23
|
import { SectionIndexGenerator } from "./generator/section-index-generator.js";
|
|
23
24
|
import { OpenApiRefreshCoordinator, } from "./generator/openapi-refresh-coordinator.js";
|
|
@@ -56,6 +57,7 @@ export class MDXToNextJSGenerator {
|
|
|
56
57
|
sectionIndexGenerator;
|
|
57
58
|
projectConfigRepository;
|
|
58
59
|
publicAssetManager;
|
|
60
|
+
iconAssetManager;
|
|
59
61
|
watchCoordinator;
|
|
60
62
|
constructor(watchDir, outputDir, openApiSpecs = [], rootDir = process.cwd()) {
|
|
61
63
|
this.watchDir = path.resolve(watchDir);
|
|
@@ -97,8 +99,9 @@ export class MDXToNextJSGenerator {
|
|
|
97
99
|
maybeUpdateSections: () => this.maybeUpdateSections(),
|
|
98
100
|
removeOwnedRoute: (slug) => this.removeOwnedRoute(slug),
|
|
99
101
|
});
|
|
100
|
-
this.projectConfigRepository = new ProjectConfigRepository(this.rootDir, this.outputDir, this.sourceFs, this.configFiles, this.fontConfigFile, this.analyticsConfigFile);
|
|
102
|
+
this.projectConfigRepository = new ProjectConfigRepository(this.rootDir, this.outputDir, this.sourceFs, this.configFiles, this.fontConfigFile, this.analyticsConfigFile, ICON_SOURCE_FILES);
|
|
101
103
|
this.publicAssetManager = new PublicAssetManager(this.rootDir, this.outputDir, this.artifacts, this.sourceFs);
|
|
104
|
+
this.iconAssetManager = new IconAssetManager(this.rootDir, this.outputDir, this.artifacts, this.sourceFs, (fileName) => this.projectConfigRepository.readOptionalRootBinaryFile(fileName, "icon source"));
|
|
102
105
|
this.watchCoordinator = new WatchCoordinator({
|
|
103
106
|
watchDir: this.watchDir,
|
|
104
107
|
rootDir: this.rootDir,
|
|
@@ -106,6 +109,7 @@ export class MDXToNextJSGenerator {
|
|
|
106
109
|
fontConfigFile: this.fontConfigFile,
|
|
107
110
|
analyticsConfigFile: this.analyticsConfigFile,
|
|
108
111
|
doccupineConfigFile: this.doccupineConfigFile,
|
|
112
|
+
iconFiles: ICON_SOURCE_FILES,
|
|
109
113
|
sourceFs: this.sourceFs,
|
|
110
114
|
getOpenApiSpecs: () => this.openApiSpecs,
|
|
111
115
|
getOpenApiSourceFiles: () => this.apiRegistry.sourceFiles,
|
|
@@ -124,6 +128,7 @@ export class MDXToNextJSGenerator {
|
|
|
124
128
|
copyPublicFiles: () => this.copyPublicFiles(),
|
|
125
129
|
handlePublicFileChange: (filePath) => this.handlePublicFileChange(filePath),
|
|
126
130
|
handlePublicFileDelete: (filePath) => this.handlePublicFileDelete(filePath),
|
|
131
|
+
syncIconFiles: () => this.syncIconFiles(),
|
|
127
132
|
processAllMDXFiles: () => this.reconcileMdxSources(),
|
|
128
133
|
},
|
|
129
134
|
});
|
|
@@ -201,6 +206,7 @@ export class MDXToNextJSGenerator {
|
|
|
201
206
|
await this.copyFontConfig();
|
|
202
207
|
await this.copyAnalyticsConfig();
|
|
203
208
|
await this.copyPublicFiles();
|
|
209
|
+
await this.syncIconFiles();
|
|
204
210
|
if (this.sectionsConfig) {
|
|
205
211
|
console.log(chalk.blue(`📑 Found ${this.sectionsConfig.length} section(s): ${this.sectionsConfig.map((s) => s.label).join(", ")}`));
|
|
206
212
|
}
|
|
@@ -408,6 +414,9 @@ export class MDXToNextJSGenerator {
|
|
|
408
414
|
async copyPublicFiles() {
|
|
409
415
|
return this.publicAssetManager.copyPublicFiles();
|
|
410
416
|
}
|
|
417
|
+
async syncIconFiles() {
|
|
418
|
+
return this.iconAssetManager.syncIconFiles();
|
|
419
|
+
}
|
|
411
420
|
async handlePublicFileChange(filePath) {
|
|
412
421
|
return this.publicAssetManager.handlePublicFileChange(filePath, () => this.updateLlmsFiles());
|
|
413
422
|
}
|