forked from microsoft/TypeScript-DOM-lib-generator
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfetch-comment.ts
155 lines (143 loc) · 4.04 KB
/
fetch-comment.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import * as fs from "fs";
import fetch from "node-fetch";
import { JSDOM } from "jsdom";
import innerText from "styleless-innertext";
import { createRequire } from "module";
const require = createRequire(import.meta.url);
await fetchIDLs(process.argv.slice(2));
interface IDLSource {
url: string;
title: string;
shortName: string;
deprecated?: boolean;
}
async function fetchIDLs(filter: string[]) {
const idlSources = (
require("../inputfiles/idlSources.json") as IDLSource[]
).filter((source) => !filter.length || filter.includes(source.shortName));
await Promise.all(
idlSources.map(async (source) => {
const { comments } = await fetchIDL(source);
if (comments) {
fs.writeFileSync(
new URL(
`../inputfiles/idl/${source.shortName}.commentmap.json`,
import.meta.url
),
comments + "\n"
);
}
})
);
}
async function fetchIDL(source: IDLSource) {
const response = await fetch(source.url);
const dom = JSDOM.fragment(await response.text());
const comments = processComments(dom);
return { comments };
}
function processComments(dom: DocumentFragment) {
const elements = [...dom.querySelectorAll("dl.domintro")];
if (!elements.length) {
return undefined;
}
const result: Record<string, string> = {};
for (const element of elements) {
for (const { dt, dd } of generateDescriptionPairs(element)) {
elements.push(...importNestedList(dd));
const comment = dd
.map((desc) => {
desc.normalize();
convertChildPre(desc);
return innerText(desc).replace(/’/g, "'");
})
.filter((text) => text)
.join("\n\n");
for (const key of dt.map((term) => getKey(term.innerHTML))) {
if (!key) {
continue;
}
const retargeted = retargetCommentKey(key, dom);
// prefer the first description
if (!result[retargeted]) {
result[retargeted] = comment;
}
}
}
}
if (!Object.keys(result).length) {
return undefined;
}
return JSON.stringify(result, undefined, 4);
}
function convertChildPre(e: Element) {
for (const pre of e.querySelectorAll("pre")) {
const code = pre.querySelector(":scope > code") as HTMLElement;
if (!code) {
continue;
}
const text = innerText(code, {
getComputedStyle() {
return { whiteSpace: "pre" } as CSSStyleDeclaration;
},
});
pre.textContent = "```\n" + text + "\n```";
}
}
function getKey(s: string) {
const keyRegexp = /#dom-([a-zA-Z0-9-_]+)/i;
const match = s.match(keyRegexp);
if (match) {
return match[1];
}
return undefined;
}
function* generateDescriptionPairs(domIntro: Element) {
const dt: HTMLElement[] = [];
const dd: HTMLElement[] = [];
let element = domIntro.firstElementChild;
while (element) {
switch (element.localName) {
case "dt":
if (dd.length) {
yield { dt: [...dt], dd: [...dd] };
dt.length = dd.length = 0;
}
dt.push(element as HTMLElement);
break;
case "dd":
dd.push(element as HTMLElement);
break;
default:
throw new Error(`Unexpected element ${element.localName}`);
}
element = element.nextElementSibling;
}
if (dd.length) {
yield { dt: [...dt], dd: [...dd] };
}
}
function* importNestedList(elements: Element[]) {
for (const element of elements) {
for (const dl of element.getElementsByTagName("dl")) {
dl.remove();
yield dl;
}
}
}
/**
* Specifications tends to keep existing keys even after a member relocation
* so that external links can be stable and won't broken.
*/
function retargetCommentKey(key: string, dom: DocumentFragment) {
const [parent, member] = key.split(/-/g);
if (!member) {
return parent;
}
const dfn = dom.getElementById(`dom-${key}`);
if (!dfn || !dfn.dataset.dfnFor) {
// The optional third word is for overloads and can be safely ignored.
return `${parent}-${member}`;
}
return `${dfn.dataset.dfnFor.toLowerCase()}-${member}`;
}