Skip to content

Commit 746f705

Browse files
committed
feat: extract summary from MDN comments for better documentation
1 parent 7b92b7d commit 746f705

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/build/emitter.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
arrayBufferViewTypes,
1313
} from "./helpers.js";
1414
import { collectLegacyNamespaceTypes } from "./legacy-namespace.js";
15+
import { extractSummaryFromFile } from "./mdn-comments.js";
1516

1617
/// Decide which members of a function to emit
1718
enum EmitScope {
@@ -906,7 +907,10 @@ export function emitWebIdl(
906907
comments.push("Available only in secure contexts.");
907908
}
908909
if (entity.mdnUrl) {
909-
if (comments.length > 0) comments.push("");
910+
if (comments.length == 0) {
911+
comments.push(extractSummaryFromFile(entity.mdnUrl));
912+
}
913+
comments.push("");
910914
comments.push(`[MDN Reference](${entity.mdnUrl})`);
911915
}
912916

src/build/mdn-comments.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,25 @@ export function generateDescriptions(): Record<string, string> {
107107

108108
return {};
109109
}
110+
111+
export function extractSummaryFromFile(url: string): string {
112+
const relativePath = url
113+
.replace("https://developer.mozilla.org/docs/", "")
114+
.toLowerCase();
115+
116+
const filePath = new URL(
117+
`../../inputfiles/mdn/files/en-us/${relativePath}/index.md`,
118+
import.meta.url,
119+
);
120+
121+
try {
122+
const content = fs.readFileSync(filePath, "utf-8");
123+
return extractSummary(content);
124+
} catch (error) {
125+
console.error(
126+
`Failed to read or extract summary from: ${filePath.href}`,
127+
error,
128+
);
129+
return "";
130+
}
131+
}

0 commit comments

Comments
 (0)