-
Notifications
You must be signed in to change notification settings - Fork 517
/
spas.ts
463 lines (419 loc) · 13.6 KB
/
spas.ts
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
import cheerio from "cheerio";
import frontmatter from "front-matter";
import { fdir, PathsOutput } from "fdir";
import got from "got";
import { m2h } from "../markdown/index.js";
import {
VALID_LOCALES,
MDN_PLUS_TITLE,
DEFAULT_LOCALE,
} from "../libs/constants/index.js";
import {
CONTENT_ROOT,
CONTENT_TRANSLATED_ROOT,
CONTRIBUTOR_SPOTLIGHT_ROOT,
BUILD_OUT_ROOT,
} from "../libs/env/index.js";
import { isValidLocale } from "../libs/locale-utils/index.js";
import { DocFrontmatter, NewsItem } from "../libs/types/document.js";
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import { renderHTML } from "../ssr/dist/main.js";
import { getSlugByBlogPostUrl, splitSections } from "./utils.js";
import { findByURL } from "../content/document.js";
import { buildDocument } from "./index.js";
import { findPostBySlug } from "./blog.js";
const FEATURED_ARTICLES = [
"blog/regular-expressions-reference-updates/",
"blog/aria-accessibility-html-landmark-roles/",
"docs/Web/API/Performance_API",
"docs/Web/CSS/font-palette",
];
const contributorSpotlightRoot = CONTRIBUTOR_SPOTLIGHT_ROOT;
async function buildContributorSpotlight(
locale: string,
options: { verbose?: boolean }
) {
const prefix = "community/spotlight";
const profileImg = "profile-image.jpg";
for (const contributor of fs.readdirSync(contributorSpotlightRoot)) {
const markdown = fs.readFileSync(
`${contributorSpotlightRoot}/${contributor}/index.md`,
"utf-8"
);
const frontMatter = frontmatter<DocFrontmatter>(markdown);
const contributorHTML = await m2h(frontMatter.body, { locale });
const { sections } = splitSections(contributorHTML);
const hyData = {
sections: sections,
contributorName: frontMatter.attributes.contributor_name,
folderName: frontMatter.attributes.folder_name,
isFeatured: frontMatter.attributes.is_featured,
profileImg,
profileImgAlt: frontMatter.attributes.img_alt,
usernames: frontMatter.attributes.usernames,
quote: frontMatter.attributes.quote,
};
const context = { hyData };
const html = renderHTML(`/${locale}/${prefix}/${contributor}`, context);
const outPath = path.join(
BUILD_OUT_ROOT,
locale.toLowerCase(),
`${prefix}/${hyData.folderName}`
);
const filePath = path.join(outPath, "index.html");
const imgFilePath = `${contributorSpotlightRoot}/${contributor}/profile-image.jpg`;
const imgFileDestPath = path.join(outPath, profileImg);
const jsonFilePath = path.join(outPath, "index.json");
fs.mkdirSync(outPath, { recursive: true });
fs.writeFileSync(filePath, html);
fs.copyFileSync(imgFilePath, imgFileDestPath);
fs.writeFileSync(jsonFilePath, JSON.stringify(context));
if (options.verbose) {
console.log("Wrote", filePath);
}
if (frontMatter.attributes.is_featured) {
return {
contributorName: frontMatter.attributes.contributor_name,
url: `/${locale}/${prefix}/${frontMatter.attributes.folder_name}`,
quote: frontMatter.attributes.quote,
};
}
}
}
export async function buildSPAs(options: {
quiet?: boolean;
verbose?: boolean;
}) {
let buildCount = 0;
// The URL isn't very important as long as it triggers the right route in the <App/>
const url = `/${DEFAULT_LOCALE}/404.html`;
const html = renderHTML(url, { pageNotFound: true });
const outPath = path.join(
BUILD_OUT_ROOT,
DEFAULT_LOCALE.toLowerCase(),
"_spas"
);
fs.mkdirSync(outPath, { recursive: true });
fs.writeFileSync(path.join(outPath, path.basename(url)), html);
buildCount++;
if (options.verbose) {
console.log("Wrote", path.join(outPath, path.basename(url)));
}
// Basically, this builds one (for example) `search/index.html` for every
// locale we intend to build.
for (const root of [CONTENT_ROOT, CONTENT_TRANSLATED_ROOT]) {
if (!root) {
continue;
}
for (const pathLocale of fs.readdirSync(root)) {
if (!fs.statSync(path.join(root, pathLocale)).isDirectory()) {
continue;
}
const SPAs = [
{ prefix: "play", pageTitle: "Playground | MDN" },
{ prefix: "search", pageTitle: "Search" },
{ prefix: "plus", pageTitle: MDN_PLUS_TITLE },
{
prefix: "plus/ai-help",
pageTitle: `AI Help | ${MDN_PLUS_TITLE}`,
noIndexing: true,
},
{
prefix: "plus/collections",
pageTitle: `Collections | ${MDN_PLUS_TITLE}`,
noIndexing: true,
},
{
prefix: "plus/collections/frequently_viewed",
pageTitle: `Frequently viewed articles | ${MDN_PLUS_TITLE}`,
noIndexing: true,
},
{
prefix: "plus/updates",
pageTitle: `Updates | ${MDN_PLUS_TITLE}`,
noIndexing: true,
},
{
prefix: "plus/settings",
pageTitle: `Settings | ${MDN_PLUS_TITLE}`,
noIndexing: true,
},
{ prefix: "about", pageTitle: "About MDN" },
{ prefix: "community", pageTitle: "Contribute to MDN" },
{
prefix: "advertising",
pageTitle: "Advertise with us",
},
{
prefix: "newsletter",
pageTitle: "Stay Informed with MDN",
},
];
const locale = VALID_LOCALES.get(pathLocale) || pathLocale;
for (const { prefix, pageTitle, noIndexing } of SPAs) {
const url = `/${locale}/${prefix}`;
const context = {
pageTitle,
locale,
noIndexing,
};
const html = renderHTML(url, context);
const outPath = path.join(BUILD_OUT_ROOT, pathLocale, prefix);
fs.mkdirSync(outPath, { recursive: true });
const filePath = path.join(outPath, "index.html");
fs.writeFileSync(filePath, html);
buildCount++;
if (options.verbose) {
console.log("Wrote", filePath);
}
}
}
}
// Building the MDN Plus pages.
/**
*
* @param {string} dirpath
* @param {string} slug
* @param {string} title
*/
async function buildStaticPages(
dirpath: string,
slug: string,
title = "MDN"
) {
const crawler = new fdir()
.withFullPaths()
.withErrors()
.filter((path) => path.endsWith(".md"))
.crawl(dirpath);
const filepaths = [...(crawler.sync() as PathsOutput)];
for (const filepath of filepaths) {
const file = filepath.replace(dirpath, "");
const page = file.split(".")[0];
const locale = DEFAULT_LOCALE.toLowerCase();
const markdown = fs.readFileSync(filepath, "utf-8");
const frontMatter = frontmatter<DocFrontmatter>(markdown);
const rawHTML = await m2h(frontMatter.body, { locale });
const { sections, toc } = splitSections(rawHTML);
const url = `/${locale}/${slug}/${page}`;
const hyData = {
id: page,
...frontMatter.attributes,
sections,
toc,
};
const context = {
hyData,
pageTitle: `${frontMatter.attributes.title || ""} | ${title}`,
};
const html = renderHTML(url, context);
const outPath = path.join(
BUILD_OUT_ROOT,
locale,
...slug.split("/"),
page
);
fs.mkdirSync(outPath, { recursive: true });
const filePath = path.join(outPath, "index.html");
fs.writeFileSync(filePath, html);
buildCount++;
if (options.verbose) {
console.log("Wrote", filePath);
}
const filePathContext = path.join(outPath, "index.json");
fs.writeFileSync(filePathContext, JSON.stringify(context));
}
}
await buildStaticPages(
fileURLToPath(new URL("../copy/plus/", import.meta.url)),
"plus/docs",
"MDN Plus"
);
// Build all the home pages in all locales.
// Fetch merged content PRs for the latest contribution section.
const recentContributions = await fetchRecentContributions();
// Fetch latest Hacks articles.
const latestNews = await fetchLatestNews();
for (const root of [CONTENT_ROOT, CONTENT_TRANSLATED_ROOT]) {
if (!root) {
continue;
}
for (const localeLC of fs.readdirSync(root)) {
const locale = VALID_LOCALES.get(localeLC) || localeLC;
if (!isValidLocale(locale)) {
continue;
}
if (!fs.statSync(path.join(root, localeLC)).isDirectory()) {
continue;
}
const featuredContributor = contributorSpotlightRoot
? await buildContributorSpotlight(locale, options)
: null;
const featuredArticles = (
await Promise.all(
FEATURED_ARTICLES.map(async (url) => {
const segment = url.split("/")[0];
if (segment === "docs") {
const document =
findByURL(`/${locale}/${url}`) ||
findByURL(`/${DEFAULT_LOCALE}/${url}`);
if (document) {
const {
doc: { mdn_url, summary, title, parents },
} = await buildDocument(document);
return {
mdn_url,
summary,
title,
tag: parents.length > 2 ? parents[1] : null,
};
}
} else if (segment === "blog") {
const post = await findPostBySlug(
getSlugByBlogPostUrl(`/${DEFAULT_LOCALE}/${url}`)
);
if (post) {
const {
doc: { title },
blogMeta: { description, slug },
} = post;
return {
mdn_url: `/${DEFAULT_LOCALE}/blog/${slug}/`,
summary: description,
title,
};
}
}
})
)
).filter(Boolean);
const url = `/${locale}/`;
const hyData = {
recentContributions,
featuredContributor,
latestNews,
featuredArticles,
};
const context = { hyData };
const html = renderHTML(url, context);
const outPath = path.join(BUILD_OUT_ROOT, localeLC);
fs.mkdirSync(outPath, { recursive: true });
const filePath = path.join(outPath, "index.html");
fs.writeFileSync(filePath, html);
buildCount++;
if (options.verbose) {
console.log("Wrote", filePath);
}
// Also, dump the recent pull requests in a file so the data can be gotten
// in client-side rendering.
const filePathContext = path.join(outPath, "index.json");
fs.writeFileSync(filePathContext, JSON.stringify(context));
buildCount++;
if (options.verbose) {
console.log("Wrote", filePathContext);
}
}
}
if (!options.quiet) {
console.log(`Built ${buildCount} SPA related files`);
}
}
async function fetchGitHubPRs(repo, count = 5) {
const twoDaysAgo = new Date(Date.now() - 48 * 60 * 60 * 1000);
const pullRequestsQuery = [
`repo:${repo}`,
"is:pr",
"is:merged",
`merged:>${twoDaysAgo.toISOString()}`,
"sort:updated",
].join("+");
const pullRequestUrl = `https://api.github.com/search/issues?q=${pullRequestsQuery}&per_page=${count}`;
const pullRequestsData = (await got(pullRequestUrl).json()) as {
items: any[];
};
const prDataRepo = pullRequestsData.items.map((item) => ({
...item,
repo: { name: repo, url: `https://github.com/${repo}` },
}));
return prDataRepo;
}
async function fetchRecentContributions() {
const repos = ["mdn/content", "mdn/translated-content"];
const countPerRepo = 5;
const pullRequests = (
await Promise.all(
repos.map(async (repo) => await fetchGitHubPRs(repo, countPerRepo))
)
).flat();
const pullRequestsData = pullRequests.sort((a, b) =>
a.updated_at < b.updated_at ? 1 : -1
);
return {
items: pullRequestsData.map(
({ number, title, updated_at, pull_request: { html_url }, repo }) => ({
number,
title,
updated_at,
url: html_url,
repo,
})
),
};
}
async function fetchLatestNews() {
const xml = await got("https://hacks.mozilla.org/category/mdn/feed/").text();
const $ = cheerio.load(xml, { xmlMode: true });
const items: NewsItem[] = [];
items.push(
{
title: "Introducing AI Help: Your Trusted Companion for Web Development",
url: `/${DEFAULT_LOCALE}/blog/introducing-ai-help/`,
author: "Hermina Condei",
published_at: new Date("2023-06-27").toString(),
source: {
name: "developer.mozilla.org",
url: `/${DEFAULT_LOCALE}/blog/`,
},
},
{
title: "Introducing the MDN Playground: Bring your code to life!",
url: `/${DEFAULT_LOCALE}/blog/introducing-the-mdn-playground/`,
author: "Florian Dieminger",
published_at: new Date("2023-06-22").toString(),
source: {
name: "developer.mozilla.org",
url: `/${DEFAULT_LOCALE}/blog/`,
},
},
{
title: "Introducing Baseline: a unified view of stable web features",
url: `/${DEFAULT_LOCALE}/blog/baseline-unified-view-stable-web-features/`,
author: "Hermina Condei",
published_at: new Date("2023-05-10").toString(),
source: {
name: "developer.mozilla.org",
url: `/${DEFAULT_LOCALE}/blog/`,
},
}
);
$("item").each((i, item) => {
const $item = $(item);
items.push({
title: $item.find("title").text(),
url: $item.find("guid").text(),
author: $item.find("dc\\:creator").text(),
published_at: $item.find("pubDate").text(),
source: {
name: "hacks.mozilla.org",
url: "https://hacks.mozilla.org/category/mdn/",
},
});
});
return {
items,
};
}