Skip to content

Commit 3130a7a

Browse files
committed
.
1 parent fae5ec1 commit 3130a7a

File tree

1 file changed

+7
-28
lines changed

1 file changed

+7
-28
lines changed

src/build/mdn-comments.ts

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import fs from "fs/promises";
2+
import { merge } from "./helpers.js";
23

34
const basePath = new URL(
45
"../../inputfiles/mdn/files/en-us/web/api/",
@@ -68,41 +69,19 @@ async function walkDirectory(dir: URL): Promise<URL[]> {
6869
return results;
6970
}
7071

71-
function generateSlug(content: string): string {
72-
const match = content.match(/slug:\s*["']?([^"'\n]+)["']?/);
73-
if (!match) throw new Error("Slug not found");
72+
function generateSlug(content: string): string[] {
73+
const match = content.match(/slug:\s*["']?([^"'\n]+)["']?/)!;
7474
const url = match[1].split(":").pop()!;
7575
const parts = url.split("/").slice(2); // remove first 2 segments
76-
const result = parts.join("/");
77-
return result;
76+
return parts;
7877
}
7978

80-
function urlToNestedObject(url: string, text: string): Record<string, any> {
81-
const keys = url.split("/");
82-
return keys.reduceRight<Record<string, any>>((acc, key) => ({ [key]: acc }), {
79+
function urlToNestedObject(url: string[], text: string): Record<string, any> {
80+
return url.reduceRight<Record<string, any>>((acc, key) => ({ [key]: acc }), {
8381
__comment: text,
8482
});
8583
}
8684

87-
function deepMerge(
88-
target: Record<string, any>,
89-
source: Record<string, any>,
90-
): Record<string, any> {
91-
for (const key in source) {
92-
if (
93-
source[key] &&
94-
typeof source[key] === "object" &&
95-
!Array.isArray(source[key])
96-
) {
97-
if (!target[key]) target[key] = {};
98-
deepMerge(target[key], source[key]);
99-
} else {
100-
target[key] = source[key];
101-
}
102-
}
103-
return target;
104-
}
105-
10685
export async function generateDescriptions(): Promise<Record<string, any>> {
10786
const stats = await fs.stat(basePath);
10887
if (!stats.isDirectory()) {
@@ -121,7 +100,7 @@ export async function generateDescriptions(): Promise<Record<string, any>> {
121100
const slug = generateSlug(content);
122101
const summary = extractSummary(content);
123102
const nested = urlToNestedObject(slug, summary);
124-
deepMerge(results, nested as any);
103+
merge(results, nested);
125104
} catch (error) {
126105
console.warn(`Skipping ${fileURL.href}: ${error}`);
127106
}

0 commit comments

Comments
 (0)