Skip to content

Commit bc32dbe

Browse files
authored
subdirectories
1 parent 2015d4b commit bc32dbe

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

src/build/mdn-comments.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import fs from "fs/promises";
22
const basePath = new URL("../../inputfiles/mdn/files/en-us/", import.meta.url);
3-
const inputPaths = ["web/api/", "webassembly/reference/javascript_interface/"];
3+
const subdirectories = [
4+
"web/api/",
5+
"webassembly/reference/javascript_interface/",
6+
];
47

58
function extractSummary(markdown: string): string {
69
// Remove frontmatter (--- at the beginning)
@@ -89,11 +92,12 @@ function extractSlug(content: string): string[] {
8992
const match = content.match(/\nslug: (.+)\n/)!;
9093
const url = match[1].split(":").pop()!;
9194
const normalized = url.endsWith("_static") ? url.slice(0, -7) : url;
92-
const parts = normalized.split("/");
93-
if (parts[0].toLowerCase() === "web") {
94-
return parts.slice(2);
95+
for (const subdirectory of subdirectories) {
96+
if (normalized.toLowerCase().startsWith(subdirectory)) {
97+
return normalized.slice(subdirectory.length).split("/");
98+
}
9599
}
96-
return parts.slice(3);
100+
return [];
97101
}
98102

99103
function ensureLeaf(obj: Record<string, any>, keys: string[]) {
@@ -133,9 +137,7 @@ export async function generateDescriptions(): Promise<{
133137

134138
const results: Record<string, any> = {};
135139
const indexPaths = await Promise.all(
136-
inputPaths.map(
137-
async (path) => await walkDirectory(new URL(path, basePath)),
138-
),
140+
subdirectories.map((dir) => walkDirectory(new URL(dir, basePath))),
139141
).then((res) => res.flat());
140142

141143
await Promise.all(
@@ -147,7 +149,7 @@ export async function generateDescriptions(): Promise<{
147149
const content = await fs.readFile(fileURL, "utf-8");
148150
const slug = extractSlug(content);
149151
const generatedPath = generatePath(content);
150-
if (!slug || slug.length === 0 || !generatedPath) return;
152+
if (!slug.length || !generatedPath) return;
151153

152154
const summary = extractSummary(content);
153155
insertComment(results, slug, summary, generatedPath);

0 commit comments

Comments
 (0)