-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsearchPlugin.js
More file actions
53 lines (43 loc) · 1.35 KB
/
Copy pathsearchPlugin.js
File metadata and controls
53 lines (43 loc) · 1.35 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
const { create, search, insert } = require('@orama/orama');
const docusaurusUtils = require('docusaurus-utils');
module.exports = {
name: 'my-docusaurus-search-plugin',
search: async (siteConfig, options) => {
// Create a database with the following schema:
const schema = {
title: 'string',
content: 'string',
meta: {
rating: 'number',
},
};
// Create a database instance.
const db = await create({
schema,
});
// Get a list of all of the Markdown files in the project.
const markdownFiles = await docusaurusUtils.getAllMarkdownFiles(siteConfig);
// Insert each Markdown file into the database.
for (let i = 0; i < markdownFiles.length; i++) {
const file = markdownFiles[i];
const content = await docusaurusUtils.readFile(file);
// Extract the title and rating from the Markdown file.
const title = file.name;
const rating = Number(file.data.meta?.rating);
// Insert the document into the database.
await insert(db, {
title,
content,
meta: {
rating,
},
});
}
// Search the database for the given term.
const searchResult = await search(db, {
term: options.term,
});
// Return the search results.
return searchResult.hits.map((hit) => hit.document);
},
};