-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync-versioned-docs.mjs
More file actions
289 lines (247 loc) · 7.37 KB
/
sync-versioned-docs.mjs
File metadata and controls
289 lines (247 loc) · 7.37 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import { execFile } from "node:child_process";
import { mkdir, rm, writeFile } from "node:fs/promises";
import path from "node:path";
import { promisify } from "node:util";
import { parse as parseYaml } from "yaml";
import { repoRoot, websiteRoot } from "../src/lib/docsTree.mjs";
const execFileAsync = promisify(execFile);
const docsVersionsRoot = path.join(repoRoot, "docs-versions");
const versionedDocsDir = path.join(docsVersionsRoot, "versioned_docs");
const versionedSidebarsDir = path.join(docsVersionsRoot, "versioned_sidebars");
const versionsJsonPath = path.join(docsVersionsRoot, "versions.json");
const githubBlobBase =
"https://github.com/BootstrapLaboratory/rush-delivery/blob";
const publishedVersions = [
"v0.6.7",
"v0.6.6",
"v0.6.5",
"v0.6.4",
"v0.6.3",
"v0.6.2",
"v0.6.1",
"v0.6.0",
"v0.5.0",
"v0.4.1",
"v0.4.0",
"v0.3.4",
"v0.3.3",
"v0.3.2",
"v0.3.1",
];
function stripLeadingHeading(markdown) {
return markdown.replace(/^# .+\r?\n+/, "");
}
async function gitShow(ref, filePath) {
try {
const { stdout } = await execFileAsync(
"git",
["show", `${ref}:${filePath}`],
{
cwd: repoRoot,
maxBuffer: 10 * 1024 * 1024,
},
);
return stdout;
} catch (error) {
throw new Error(`Unable to read ${filePath} from ${ref}.`, {
cause: error,
});
}
}
function normalizeTagSource(source) {
const normalized = path.posix.normalize(
path.posix.join("website-docusaurus", source),
);
if (normalized.startsWith("../")) {
throw new Error(`Docs tree source escapes repository root: ${source}`);
}
return normalized;
}
function flattenDocsTree(tree, items = allDocItems(tree)) {
const pages = [];
for (const item of items) {
if (item.source !== undefined) {
pages.push({
description: item.description ?? "",
id: item.id,
source: normalizeTagSource(item.source),
title: item.title,
});
}
if (Array.isArray(item.children)) {
pages.push(...flattenDocsTree(tree, item.children));
}
}
return pages;
}
function allDocItems(tree) {
return [
...tree.items,
...(tree.quickStartItems ?? []),
...(tree.tutorialItems ?? []),
];
}
function validateDocsTree(tree, version) {
if (
typeof tree !== "object" ||
tree === null ||
!Array.isArray(tree.items) ||
(tree.quickStartItems !== undefined &&
!Array.isArray(tree.quickStartItems)) ||
(tree.tutorialItems !== undefined && !Array.isArray(tree.tutorialItems))
) {
throw new Error(
`${version}: website-docusaurus/docs-tree.yaml must define items and optional quickStartItems/tutorialItems arrays.`,
);
}
}
function frontmatterString(page) {
const lines = [
"---",
`title: ${JSON.stringify(page.title)}`,
`sidebar_label: ${JSON.stringify(page.title)}`,
];
if (!page.id.includes("/")) {
lines.splice(1, 0, `id: ${JSON.stringify(page.id)}`);
}
if (page.description.length > 0) {
lines.push(`description: ${JSON.stringify(page.description)}`);
}
lines.push("---", "", "");
return lines.join("\n");
}
function routeForId(id) {
return id === "index" ? "/docs/" : `/docs/${id}/`;
}
function relativeDocRoute(fromId, toId) {
const relative = path.posix.relative(routeForId(fromId), routeForId(toId));
return relative.length > 0 ? relative : ".";
}
function rewriteMarkdownLinks(markdown, page, sourceToId, version) {
const sourceDir = path.posix.dirname(page.source);
return markdown.replace(
/\]\((?!https?:\/\/|mailto:|#|\/)([^)\s]+)(#[^)]+)?\)/g,
(_match, rawLink, rawHash = "") => {
const targetPath = path.posix
.normalize(path.posix.join(sourceDir, rawLink))
.replaceAll(path.sep, "/");
const targetId = sourceToId.get(targetPath);
if (targetId !== undefined) {
return `](${relativeDocRoute(page.id, targetId)}${rawHash})`;
}
return `](${githubBlobBase}/${version}/${targetPath}${rawHash})`;
},
);
}
function treeItemToSidebar(item) {
if (Array.isArray(item.children)) {
return {
type: "category",
label: item.title,
items: item.children.map(treeItemToSidebar),
};
}
return {
type: "doc",
id: item.id,
label: item.title,
};
}
function buildSidebars(tree) {
const tutorialItems =
tree.tutorialItems === undefined || tree.tutorialItems.length === 0
? [
{
id: "tutorial",
title: "Tutorial",
},
]
: tree.tutorialItems;
return {
docsSidebar: tree.items.map(treeItemToSidebar),
quickStartSidebar: (tree.quickStartItems ?? []).map(treeItemToSidebar),
tutorialSidebar: tutorialItems.map(treeItemToSidebar),
};
}
async function loadTree(version) {
const tree = parseYaml(
await gitShow(version, "website-docusaurus/docs-tree.yaml"),
);
validateDocsTree(tree, version);
return tree;
}
async function writeVersionDocs(version) {
const tree = await loadTree(version);
const outputDir = path.join(versionedDocsDir, `version-${version}`);
const pages = flattenDocsTree(tree);
const sourceToId = new Map(pages.map((page) => [page.source, page.id]));
await rm(outputDir, { force: true, recursive: true });
await mkdir(outputDir, { recursive: true });
await writeFile(
path.join(outputDir, "index.md"),
[
"---",
'id: "index"',
'title: "Docs"',
'sidebar_label: "Docs"',
`description: ${JSON.stringify(`Documentation for Rush Delivery ${version}.`)}`,
"---",
"",
`You are viewing archived documentation for Rush Delivery ${version}.`,
"",
"Choose a page from the sidebar, or start with the [Quick Start](quick-start/github-actions).",
"",
].join("\n"),
);
if (tree.tutorialItems === undefined || tree.tutorialItems.length === 0) {
await writeFile(
path.join(outputDir, "tutorial.md"),
[
"---",
'id: "tutorial"',
'title: "Tutorial"',
'sidebar_label: "Tutorial"',
`description: ${JSON.stringify(`Tutorial availability for Rush Delivery ${version}.`)}`,
"---",
"",
`The tutorial was not published for Rush Delivery ${version}.`,
"",
"Use the archived docs sidebar for this version, or switch to the current docs version for the tutorial.",
"",
].join("\n"),
);
}
for (const page of pages) {
const markdown = rewriteMarkdownLinks(
stripLeadingHeading(await gitShow(version, page.source)),
page,
sourceToId,
version,
);
const outputPath = path.join(outputDir, `${page.id}.md`);
await mkdir(path.dirname(outputPath), { recursive: true });
await writeFile(
outputPath,
`${frontmatterString(page)}${markdown.trim()}\n`,
);
}
await mkdir(versionedSidebarsDir, { recursive: true });
await writeFile(
path.join(versionedSidebarsDir, `version-${version}-sidebars.json`),
`${JSON.stringify(buildSidebars(tree), null, 2)}\n`,
);
}
async function main() {
await rm(versionedDocsDir, { force: true, recursive: true });
await rm(versionedSidebarsDir, { force: true, recursive: true });
await mkdir(versionedDocsDir, { recursive: true });
await mkdir(versionedSidebarsDir, { recursive: true });
for (const version of publishedVersions) {
await writeVersionDocs(version);
}
await writeFile(
versionsJsonPath,
`${JSON.stringify(publishedVersions, null, 2)}\n`,
);
}
await main();