Skip to content

Commit 79d9131

Browse files
committed
use leaf
1 parent 3130a7a commit 79d9131

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

src/build/mdn-comments.ts

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

43
const basePath = new URL(
54
"../../inputfiles/mdn/files/en-us/web/api/",
@@ -76,10 +75,16 @@ function generateSlug(content: string): string[] {
7675
return parts;
7776
}
7877

79-
function urlToNestedObject(url: string[], text: string): Record<string, any> {
80-
return url.reduceRight<Record<string, any>>((acc, key) => ({ [key]: acc }), {
81-
__comment: text,
82-
});
78+
function ensureLeaf(
79+
obj: Record<string, any>,
80+
keys: string[],
81+
): Record<string, any> {
82+
let leaf = obj;
83+
for (const key of keys) {
84+
leaf[key] ??= {};
85+
leaf = leaf[key];
86+
}
87+
return leaf;
8388
}
8489

8590
export async function generateDescriptions(): Promise<Record<string, any>> {
@@ -99,8 +104,8 @@ export async function generateDescriptions(): Promise<Record<string, any>> {
99104
const content = await fs.readFile(fileURL, "utf-8");
100105
const slug = generateSlug(content);
101106
const summary = extractSummary(content);
102-
const nested = urlToNestedObject(slug, summary);
103-
merge(results, nested);
107+
const leaf = ensureLeaf(results, slug);
108+
leaf.__comment = summary;
104109
} catch (error) {
105110
console.warn(`Skipping ${fileURL.href}: ${error}`);
106111
}

0 commit comments

Comments
 (0)