forked from vectordotdev/vector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgolia-index.ts
273 lines (237 loc) · 7.02 KB
/
algolia-index.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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import chalk from "chalk";
import cheerio from "cheerio";
import { Element } from "domhandler";
import dotEnv from "dotenv-defaults";
import fs from "fs";
import glob from "glob-promise";
import path from "path";
dotEnv.config();
// Types
type Payload = {
level: number;
domId?: string;
tagName: string;
content: string;
}[];
type AlgoliaRecord = {
objectID: string;
pageTitle: string;
pageUrl: string;
itemUrl: string;
level: number;
title: string;
hierarchy: string[];
tags: string[];
ranking: number;
section: string;
content: string;
};
type Section = {
name: string;
path: string;
displayPath: string;
ranking: number;
};
// Constants
const DEBUG = process.env.DEBUG === "true" || false;
const targetFile = "./public/search.json";
const publicPath = path.resolve(__dirname, "..", "public");
const tagHierarchy = {
h1: 6,
h2: 5,
h3: 4,
h4: 3,
h5: 2,
h6: 1,
li: 1,
p: 1,
};
function getPageUrl(file: string) {
const filePath = file.split("public/")[1].split(path.sep).slice(0, -1);
return `/${filePath.join("/")}`;
}
function getItemUrl(file: string, { level, domId }: Payload[0]) {
const fileUrl = getPageUrl(file);
if (level > 1 && level < 6 && !domId) {
console.log(chalk.yellow(`Missing domId for level ${level}`));
console.log(chalk.yellow(`File ${file}`));
}
return level > 1 && level < 6 && domId ? `${fileUrl}#${domId}` : fileUrl;
}
async function indexHTMLFiles(
records: AlgoliaRecord[],
section: string,
files: string[],
ranking: number
): Promise<void> {
const usedIds = {};
const algoliaRecords: AlgoliaRecord[] = [];
for (const file of files) {
const html = fs.readFileSync(file, "utf-8");
const $ = cheerio.load(html);
const containers = $("#page-content");
const pageTitle = $("meta[name='algolia:title']").attr("content") || "";
const pageTagsString = $("meta[name='keywords']").attr('content') || "";
const pageTags: string[] = (pageTagsString === "") ? [] : pageTagsString.split(",");
// @ts-ignore
$(".algolia-no-index").each((_, d) => $(d).remove());
// @ts-ignore
$(".highlight").each((_, d) => $(d).remove());
const payload: Payload = [];
const traverse = (node?: Element) => {
if (!node) {
return;
}
const level = tagHierarchy[node.tagName];
if (level) {
payload.push({
level,
domId: $(node).attr("id"),
tagName: node.tagName,
content: $(node)
.text()
.replace(/[\n\t]/g, " "),
});
}
$(node)
.children()
.map((_, d) => traverse(d));
};
for (let i = 0; i < containers.length; i++) {
traverse(containers.get(i) as Element);
}
let activeRecord: AlgoliaRecord | null = null;
for (const item of payload) {
const pageUrl = getPageUrl(file);
const itemUrl = getItemUrl(file, item);
if (!activeRecord) {
activeRecord = {
objectID: itemUrl,
pageTitle,
pageUrl,
itemUrl,
level: item.level,
title: item.content.trim(),
section,
ranking,
hierarchy: [],
tags: pageTags,
content: "",
};
} else if (item.level === 1) { // h1 logic
activeRecord.content += item.content;
} else if (item.level < activeRecord.level) {
algoliaRecords.push({ ...activeRecord });
activeRecord = {
objectID: itemUrl,
pageTitle,
pageUrl,
itemUrl,
level: item.level,
title: item.content.trim(),
section,
ranking,
hierarchy: [...activeRecord.hierarchy, activeRecord.title.trim()],
tags: pageTags,
content: "",
};
} else { // h2-h6 logic
algoliaRecords.push({ ...activeRecord });
const hierarchySize = activeRecord.hierarchy.length;
const levelDiff = item.level - activeRecord.level;
const lastIndex = hierarchySize - levelDiff;
activeRecord = {
objectID: itemUrl,
pageTitle,
pageUrl,
itemUrl,
level: item.level,
title: item.content.trim(),
section,
ranking,
hierarchy: [...activeRecord.hierarchy.slice(0, lastIndex)],
tags: pageTags,
content: "",
};
}
if (activeRecord) {
algoliaRecords.push({ ...activeRecord });
}
for (const rec of algoliaRecords) {
// The objectID is the url of the section of the page that the record covers.
// If you have a duplicate here somehow two records point to the same thing.
if (DEBUG && usedIds[rec.objectID]) {
console.log(chalk.yellow(`Duplicate ID for ${rec.objectID}`));
console.log(JSON.stringify(rec, null, 2));
}
usedIds[rec.objectID] = true;
// The h2 -> h5 should have a set of tags that are the "path" within the file.
if (DEBUG && rec.level > 1 && rec.level < 6 && rec.hierarchy.length == 0) {
console.log(chalk.yellow("Found h2 -> h5 with no tags."));
console.log(JSON.stringify(rec, null, 2));
}
}
}
}
console.log(
chalk.green(`Success. Updated records for ${files.length} file(s).`)
);
records.push(...algoliaRecords);
}
async function buildIndex() {
var allRecords: AlgoliaRecord[] = [];
console.log(`Building Vector search index`);
const sections: Section[] = [
{
name: "Docs",
path: `${publicPath}/docs/about/**/**.html`,
displayPath: "docs/about",
ranking: 50,
},
{
name: "Docs",
path: `${publicPath}/docs/administration/**/**.html`,
displayPath: "docs/administration",
ranking: 50,
},
{
name: "Docs",
path: `${publicPath}/docs/reference/**/**.html`,
displayPath: "docs/reference",
ranking: 50,
},
{
name: "Docs",
path: `${publicPath}/docs/setup/**/**.html`,
displayPath: "docs/setup",
ranking: 50,
},
{
name: "Advanced guides",
path: `${publicPath}/guides/advanced/**/**.html`,
displayPath: "guides/advanced",
ranking: 40,
},
{
name: "Level up guides",
path: `${publicPath}/guides/level-up/**/**.html`,
displayPath: "guides/level-up",
ranking: 40,
}
];
// Recurse through each section and push the resulting records to `allRecords`
for (const section of sections) {
let files = await glob(section.path);
console.log(chalk.blue(`Indexing ${section.displayPath}...`));
indexHTMLFiles(allRecords, section.name, files, section.ranking);
}
console.log(chalk.green(`Success. ${allRecords.length} records have been successfully indexed.`));
console.log(chalk.blue(`Writing final index JSON to ${targetFile}...`));
const recordsJson: string = JSON.stringify(allRecords);
fs.writeFile(targetFile, recordsJson, () => {
console.log(chalk.green(`Success. Wrote final index JSON to ${targetFile}.`));
});
}
buildIndex().catch((err) => {
console.trace(chalk.yellow(err));
});