This repository was archived by the owner on Feb 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathdocs.ts
175 lines (154 loc) · 4.83 KB
/
docs.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/// <reference types="node" />
import * as fs from "fs";
import * as path from "path";
// marked is great for this: it includes the raw text in its tokens so
// we don't need to write code that renders tokens back to markdown
import { marked } from "marked";
import { Comment, CommentParam } from "./types";
interface CommentedField {
name: string;
comment?: Comment;
}
interface CommentedDeclaration {
name: string;
comment?: Comment;
members?: CommentedField[];
}
// Get files to build docs from
const docsDir = path.join(__dirname, "..", "docs");
const filePaths = fs
.readdirSync(docsDir)
.map((fileName) => path.join(docsDir, fileName));
// Maps fenced code-block languages to those recognised by declaration renderers
const exampleLangRenames = {
js: "typescript",
ts: "typescript",
javascript: "typescript",
rs: "rust",
};
function trimComment(comment?: Comment) {
if (comment === undefined) return;
comment.text = comment.text.trim();
if (comment.params) {
comment.params = comment.params.map(({ name, text }) => ({
name,
text: text.trim(),
}));
}
if (comment.returns) {
comment.returns = comment.returns.trim();
}
}
const declarations: Record<string, CommentedDeclaration> = {};
let declaration: CommentedDeclaration | undefined = undefined;
let field: CommentedField | undefined = undefined;
enum FieldState {
// Enum member names must case-sensitive match expected 4th level heading texts
Parameters,
Returns,
Examples,
}
let fieldState: FieldState | undefined = undefined;
function pushDeclaration() {
/// Adds the current declaration (if any) to the map
if (declaration !== undefined) {
trimComment(declaration.comment);
declarations[declaration.name] = declaration;
declaration = undefined;
fieldState = undefined;
}
}
function pushField() {
/// Adds the current field (if any) to the current declaration
if (declaration !== undefined && field !== undefined) {
trimComment(field.comment);
declaration.members ??= [];
declaration.members.push(field);
field = undefined;
fieldState = undefined;
}
}
for (const filePath of filePaths) {
const tokens = marked.lexer(fs.readFileSync(filePath, "utf8"));
for (const token of tokens) {
if (token.type === "heading" && token.depth === 2) {
// New declaration
pushDeclaration();
// token.text === "`Declaration`"
declaration = { name: token.text.substring(1, token.text.length - 1) };
continue;
} else if (declaration === undefined) {
// If this isn't a new declaration, wait until we've got a declaration to add to
continue;
}
if (token.type === "heading" && token.depth === 3) {
// New field
pushField();
// token.text === "`Declaration.field`" or "`Declaration#field`"
field = {
name: token.text.substring(
1 + declaration.name.length + 1,
token.text.length - 1
),
};
continue;
}
if (field && token.type === "heading" && token.depth === 4) {
fieldState = undefined;
if (token.text in FieldState) {
fieldState = FieldState[token.text];
continue;
}
}
if (
field &&
fieldState === FieldState.Parameters &&
token.type === "list"
) {
// Field parameters
field.comment ??= { text: "" };
field.comment.params ??= [];
field.comment.params.push(
...token.items.map<CommentParam>((item) => {
// item.text === "`name`: text" (text will be trimmed later by trimComment)
const colon = item.text.indexOf(":");
return {
name: item.text.substring(1, colon - 1),
text: item.text.substring(colon + 1),
};
})
);
continue;
}
if (field && fieldState === FieldState.Returns) {
// Field returns
field.comment ??= { text: "" };
field.comment.returns ??= "";
field.comment.returns += token.raw;
continue;
}
if (field && fieldState === FieldState.Examples && token.type === "code") {
// Field examples
if (!token.lang) continue;
let lang = token.lang;
if (lang in exampleLangRenames) lang = exampleLangRenames[lang];
field.comment ??= { text: "" };
field.comment.examples ??= {};
field.comment.examples[lang] ??= [];
field.comment.examples[lang].push(token.text);
continue;
}
// If we're in a field, add comments to that, otherwise add them to the declaration itself
if (field) {
field.comment ??= { text: "" };
field.comment.text += token.raw;
} else if (declaration) {
declaration.comment ??= { text: "" };
declaration.comment.text += token.raw;
}
}
// Record final field and declaration (if any)
pushField();
pushDeclaration();
}
fs.writeFileSync("docs.json", JSON.stringify(declarations, null, 2), "utf8");