-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathgenerate.mjs
More file actions
60 lines (47 loc) · 1.63 KB
/
generate.mjs
File metadata and controls
60 lines (47 loc) · 1.63 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
'use strict';
import { join } from 'node:path';
import { create, save, insertMultiple } from '@orama/orama';
import { SCHEMA } from './constants.mjs';
import { buildHierarchicalTitle } from './utils/title.mjs';
import getConfig from '../../utils/configuration/index.mjs';
import { writeFile } from '../../utils/file.mjs';
import { groupNodesByModule } from '../../utils/generators.mjs';
import { transformNodeToString } from '../../utils/unist.mjs';
/**
* Generates the Orama database.
*
* @type {import('./types').Generator['generate']}
*/
export async function generate(input) {
const config = getConfig('orama-db');
const db = create({ schema: SCHEMA });
const apiGroups = groupNodesByModule(input);
// Process all API groups and flatten into a single document array
const documents = Array.from(apiGroups.values()).flatMap(headings =>
headings.map((entry, index) => {
const hierarchicalTitle = buildHierarchicalTitle(headings, index);
const paragraph = entry.content.children.find(
child => child.type === 'paragraph'
);
return {
title: hierarchicalTitle,
description: paragraph
? transformNodeToString(paragraph, true)
: undefined,
href: `${entry.path}.html#${entry.heading.data.slug}`,
siteSection: headings[0].heading.data.name,
};
})
);
// Insert all documents
await insertMultiple(db, documents);
const result = save(db);
// Persist
if (config.output) {
await writeFile(
join(config.output, 'orama-db.json'),
config.minify ? JSON.stringify(result) : JSON.stringify(result, null, 2)
);
}
return result;
}