-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathsearch-generator.js
More file actions
executable file
·212 lines (167 loc) · 5.48 KB
/
search-generator.js
File metadata and controls
executable file
·212 lines (167 loc) · 5.48 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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/usr/bin/env node
const markdownit = require('markdown-it');
const { program } = require('commander');
const minisearch = require('minisearch');
const path = require('node:path');
const fs = require('node:fs/promises');
const linkPrefix = '/docs/reference';
const defaultBranch = 'main';
function uniqueifyId(api, nodes) {
let suffix = "", i = 1;
while (true) {
const possibleId = `${api.kind}-${api.name}${suffix}`;
let collision = false;
for (const item of nodes) {
if (item.id === possibleId) {
collision = true;
break;
}
}
if (!collision) {
return possibleId;
}
suffix = `-${++i}`;
}
}
async function produceSearchIndex(version, apiData) {
const nodes = [ ];
for (const group in apiData['groups']) {
for (const name in apiData['groups'][group]['apis']) {
const api = apiData['groups'][group]['apis'][name];
let displayName = name;
if (api.kind === 'macro') {
displayName = displayName.replace(/\(.*/, '');
}
const apiSearchData = {
id: uniqueifyId(api, nodes),
name: displayName,
group: group,
kind: api.kind
};
apiSearchData.description = Array.isArray(api.comment) ?
api.comment[0] : api.comment;
let detail = "";
if (api.kind === 'macro') {
detail = api.value;
}
else if (api.kind === 'alias') {
detail = api.type;
}
else {
let details = undefined;
if (api.kind === 'struct' || api.kind === 'enum') {
details = api.members;
}
else if (api.kind === 'function' || api.kind === 'callback') {
details = api.params;
}
else {
throw new Error(`unknown api type '${api.kind}'`);
}
for (const item of details || [ ]) {
if (detail.length > 0) {
detail += ' ';
}
detail += item.name;
if (item.comment) {
detail += ' ';
detail += item.comment;
}
}
if (api.kind === 'function' || api.kind === 'callback') {
if (detail.length > 0 && api.returns?.type) {
detail += ' ' + api.returns.type;
}
if (detail.length > 0 && api.returns?.comment) {
detail += ' ' + api.returns.comment;
}
}
}
detail = detail.replaceAll(/\s+/g, ' ')
.replaceAll(/[\"\'\`]/g, '');
apiSearchData.detail = detail;
nodes.push(apiSearchData);
}
}
const index = new minisearch({
fields: [ 'name', 'description', 'detail' ],
storeFields: [ 'name', 'group', 'kind', 'description' ],
searchOptions: { boost: { name: 5, description: 2 } }
});
index.addAll(nodes);
const filename = `${outputPath}/${version}.json`;
await fs.mkdir(outputPath, { recursive: true });
await fs.writeFile(filename, JSON.stringify(index, null, 2));
}
function versionSort(a, b) {
if (a === b) {
return 0;
}
const aVersion = a.match(/^v(\d+)(?:\.(\d+)(?:\.(\d+)(?:\.(\d+))?)?)?(?:-(.*))?$/);
const bVersion = b.match(/^v(\d+)(?:\.(\d+)(?:\.(\d+)(?:\.(\d+))?)?)?(?:-(.*))?$/);
if (!aVersion && !bVersion) {
return a.localeCompare(b);
}
else if (aVersion && !bVersion) {
return -1;
}
else if (!aVersion && bVersion) {
return 1;
}
for (let i = 1; i < 5; i++) {
if (!aVersion[i] && !bVersion[i]) {
break;
}
else if (aVersion[i] && !bVersion[i]) {
return 1;
}
else if (!aVersion[i] && bVersion[i]) {
return -1;
}
else if (aVersion[i] !== bVersion[i]) {
return aVersion[i] - bVersion[i];
}
}
if (aVersion[5] && !bVersion[5]) {
return -1;
}
else if (!aVersion[5] && bVersion[5]) {
return 1;
}
else if (aVersion[5] && bVersion[5]) {
return aVersion[5].localeCompare(bVersion[5]);
}
return 0;
}
program.option('--verbose')
.option('--version <version...>');
program.parse();
const options = program.opts();
if (program.args.length != 2) {
console.error(`usage: ${path.basename(process.argv[1])} raw_api_dir output_dir`);
process.exit(1);
}
const docsPath = program.args[0];
const outputPath = program.args[1];
(async () => {
try {
const v = options.version ? options.version :
(await fs.readdir(docsPath))
.filter(a => a.endsWith('.json'))
.map(a => a.replace(/\.json$/, ''));
const versions = v.sort(versionSort).reverse();
for (const version of versions) {
if (options.verbose) {
console.log(`Reading documentation data for ${version}...`);
}
const apiData = JSON.parse(await fs.readFile(`${docsPath}/${version}.json`));
if (options.verbose) {
console.log(`Creating minisearch index for ${version}...`);
}
await produceSearchIndex(version, apiData);
}
} catch (e) {
console.error(e);
process.exit(1);
}
})();