-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathfetchTags.ts
More file actions
68 lines (61 loc) · 2.01 KB
/
Copy pathfetchTags.ts
File metadata and controls
68 lines (61 loc) · 2.01 KB
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
import { JSDOM } from "jsdom";
import { html2md } from "./util/html2md.ts";
import * as typer from "./util/hypertyper.ts";
export async function* fetchTags() {
const html = await fetch("https://developer.mozilla.org/en-US/docs/Web/HTML/Element").then(res => res.text());
const { document } = new JSDOM(html).window;
const baseURL = "https://developer.mozilla.org";
const tags = (
[
...document.querySelectorAll("section:not([aria-labelledby=obsolete_and_deprecated_elements]) tr"),
] as Element[]
)
.flatMap(x => {
const y = x.children[0];
const description = html2md(x.children[1].innerHTML, { baseURL });
return [...(y.querySelectorAll("a") as unknown as HTMLCollection)].map(x => {
let href = "";
for (const attr of x.attributes) if (attr.nodeName === "href") href = attr.value;
return { title: x.textContent!.replace(/<|>/g, ""), href, description };
});
})
.sort((a, b) => a.title.localeCompare(b.title));
{
const custom = typer.statement(typer.exports(typer.type("CustomTag", "`${string}-${string}`")));
const tag = typer.statement(
typer.exports(typer.type("Tag", typer.union(["CustomTag"].concat(tags.map(x => `"${x.title}"`))))),
);
yield {
file: "tags.ts",
*content() {
yield typer.preamble;
yield "\n\n";
yield* custom;
yield "\n\n";
yield* tag;
},
};
}
{
// why does <var> even exist? Won't be supported via import syntax because it's a reserved keyword
const types = typer.flatMap(
tags.filter(tag => tag.title !== "var"),
function* (opts) {
yield* (function* ({ title, href, description }: typeof opts) {
yield "\n\n";
yield* typer.desc([description, typer.see(baseURL + href, "MDN | " + title)].join("\n\n"));
yield* typer.statement(typer.exports(typer.constant(title, `create("${title}")`)));
})(opts);
},
);
yield {
file: "../elements.ts",
*content() {
yield typer.preamble;
yield "\n\n";
yield* typer.imports("./element.ts", { imports: ["create"] });
yield* types;
},
};
}
}