Skip to content

Commit da026c7

Browse files
authored
Refactor comment insertion logic in mdn-comments.ts
1 parent 5044dd7 commit da026c7

File tree

1 file changed

+13
-20
lines changed

1 file changed

+13
-20
lines changed

src/build/mdn-comments.ts

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,10 @@ function extractSlug(mdnUrl: string): string[] {
3131
if (!mdnUrl.toLowerCase().startsWith(subdirectory)) {
3232
continue;
3333
}
34-
const slugArr = mdnUrl
34+
return mdnUrl
3535
.slice(subdirectory.length)
3636
.replace(/_static/g, "")
37-
.split("/")
38-
.map((part) =>
39-
hyphenToCamelCase(part === "Reference" ? "CSSStyleProperties" : part),
40-
);
41-
if (slugArr[0] === "CSSStyleProperties") {
42-
// Drop the second itmem
43-
slugArr.splice(1, 1);
44-
}
45-
return slugArr;
37+
.split("/");
4638
}
4739
return [];
4840
}
@@ -61,15 +53,10 @@ function insertComment(
6153
slug: string[],
6254
summary: string,
6355
path: string[],
56+
name: string,
6457
) {
65-
if (!path.length) {
66-
const iface = ensureLeaf(root, slug);
67-
iface.comment = summary;
68-
} else {
69-
const [ifaceName, memberName] = slug;
70-
const target = ensureLeaf(root, [ifaceName, ...path, memberName]);
71-
target.comment = summary;
72-
}
58+
const target = ensureLeaf(root, [...slug.slice(0, -1), ...path, name]);
59+
target.comment = summary;
7360
}
7461

7562
function generateComment(summary: string, name: string): string | undefined {
@@ -103,11 +90,17 @@ export async function generateDescriptions(): Promise<{
10390
if (!slugArr.length || !path) {
10491
continue;
10592
}
106-
const comment = generateComment(entry.summary, slugArr.at(-1)!);
93+
const leaf = slugArr.at(-1)!;
94+
const name = ["css-property", "css-shorthand-property"].includes(
95+
entry.pageType,
96+
)
97+
? hyphenToCamelCase(leaf)
98+
: leaf;
99+
const comment = generateComment(entry.summary, name);
107100
if (!comment) {
108101
continue;
109102
}
110-
insertComment(results, slugArr, comment, path);
103+
insertComment(results, slugArr, comment, path, name);
111104
}
112105
return { interfaces: { interface: results } };
113106
}

0 commit comments

Comments
 (0)