-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathdocs.config.ts
More file actions
161 lines (142 loc) · 5.62 KB
/
docs.config.ts
File metadata and controls
161 lines (142 loc) · 5.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import fs from "fs-extra";
import path from "path";
import {
Version,
MdxData,
NonVersionedDocumentRootConfig,
VersionedDocumentRootConfig,
LinkValidator,
MdxFileFilter,
VersionsProvider,
NonVersionedVariableProcessor
} from "@webiny/docs-generator";
import { DeveloperDocsMdxFile } from "./docs/developer-docs/DeveloperDocsMdxFile";
import { HandbookMdxFile } from "./docs/handbook/HandbookMdxFile";
import { UserGuideMdxFile } from "./docs/user-guides/UserGuideMdxFile";
import { UserGuidesVersionProvider } from "./docs/user-guides/UserGuidesVersionProvider";
import { ReleaseNotesMdxFile } from "./docs/release-notes/ReleaseNotesMdxFile";
// @ts-expect-error
import { redirects } from "./vercel.json";
const preview = process.env.VERCEL_ENV === "preview" || process.env.NODE_ENV === "development";
/**
* Sometimes, we want to use links unknown to the LinkValidator, but which are located under the same domain.
* Use this array to whitelist such links.
* We also whitelist all links that are defined in `vercel.json` redirects.
*/
const linkWhitelist: string[] = [...redirects.map(r => r.source), "/forms/product-demo", "/slack"];
/**
* If you want to only build specific versions in the Vercel preview deployment,
* whitelist them by adding version numbers to this array. Example: "5.35.x"
*/
const whitelistedVersions: string[] = [];
/**
* Only build versions at or above this version (e.g., "5.40.x").
* Set via MIN_VERSION environment variable or modify here.
* Set to empty string to build all versions.
*/
const minVersionToBuild = process.env.MIN_VERSION || "";
const filterByEnvironment = (version: Version) => {
// In `preview`, if there are specific versions whitelisted for deployment, those are the only ones we'll output.
if (preview && whitelistedVersions.length > 0) {
return whitelistedVersions.includes(version.getValue());
}
// If minVersionToBuild is set, only build versions >= minVersion or `latest`.
if (minVersionToBuild) {
if (minVersionToBuild === "latest") {
return version.isLatest();
}
const versionNum = parseFloat(version.getValue().replace(/[^\d.]/g, ""));
const minVersionNum = parseFloat(minVersionToBuild.replace(/[^\d.]/g, ""));
return versionNum >= minVersionNum;
}
// Build everything.
return true;
};
const filterFilePathByVersion = (filePath: string): boolean => {
// Extract version from file path (e.g., /docs/developer-docs/5.40.x/... or /docs/user-guides/5.40.x/...)
const versionMatch = filePath.match(/\/(\d+\.\d+\.x)\//);
if (!versionMatch) {
// If no version in path, include the file (e.g., non-versioned docs)
return true;
}
const versionString = versionMatch[1];
// Use the same filtering logic as filterByEnvironment
if (preview && whitelistedVersions.length > 0) {
return whitelistedVersions.includes(versionString);
}
if (minVersionToBuild) {
if (minVersionToBuild === "latest") {
// For file paths, we can't determine if it's "latest" without more context
// So we'll include all versioned files when minVersionToBuild is "latest"
return true;
}
const versionNum = parseFloat(versionString.replace(/[^\d.]/g, ""));
const minVersionNum = parseFloat(minVersionToBuild.replace(/[^\d.]/g, ""));
return versionNum >= minVersionNum;
}
return true;
};
const existsInDocs = (link: string) => {
return fs.pathExists(path.join(__dirname, `src/pages/${link}.js`));
};
export default {
projectRootDir: __dirname,
cleanOutputDir: [path.resolve("src/pages/docs"), path.resolve("public/raw/docs")],
sitemapOutputPath: path.resolve("public/algolia/sitemap.xml"),
linkValidator: new LinkValidator(
linkWhitelist,
link => {
return existsInDocs(link);
},
filterFilePathByVersion
),
documentRoots: [
/* Developer Docs */
new VersionedDocumentRootConfig({
rootDir: path.resolve("docs/developer-docs"),
linkPrefix: "/docs",
outputDir: path.resolve("src/pages"),
pageLayout: "@/layouts/DocumentationLayout",
mdxFileFactory: (data: MdxData, version: Version) => new DeveloperDocsMdxFile(data, version),
mdxFileOutputFilter: new MdxFileFilter<DeveloperDocsMdxFile>(mdxFile => {
return filterByEnvironment(mdxFile.getVersion());
})
}),
/* User Guides */
new VersionedDocumentRootConfig({
rootDir: path.resolve("docs/user-guides"),
linkPrefix: "/docs",
outputDir: path.resolve("src/pages"),
pageLayout: "@/layouts/DocumentationLayout",
mdxFileFactory: (data: MdxData, version: Version) => new UserGuideMdxFile(data, version),
mdxFileOutputFilter: new MdxFileFilter<UserGuideMdxFile>(mdxFile => {
return filterByEnvironment(mdxFile.getVersion());
}),
versionsProvider: new UserGuidesVersionProvider(
path.resolve("docs/developer-docs"),
path.resolve("docs/user-guides")
)
}),
/* Release Notes */
new NonVersionedDocumentRootConfig({
rootDir: path.resolve("docs/release-notes"),
linkPrefix: "/docs",
outputDir: path.resolve("src/pages"),
pageLayout: "@/layouts/ReleaseNotesLayout",
mdxFileFactory: (data: MdxData) => new ReleaseNotesMdxFile(data),
mdxFileProcessors: [
new NonVersionedVariableProcessor(
new VersionsProvider(path.resolve("docs/developer-docs")).getVersions()
)
]
}),
/* Handbook */
new NonVersionedDocumentRootConfig({
rootDir: path.resolve("docs/handbook"),
linkPrefix: "/docs/handbook",
outputDir: path.resolve("src/pages"),
pageLayout: "@/layouts/HandbookLayout",
mdxFileFactory: (data: MdxData) => new HandbookMdxFile(data)
})
]
};