-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmeili-indexer.mjs
71 lines (54 loc) · 1.66 KB
/
meili-indexer.mjs
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
import fs from 'fs';
import {v4 as uuidv4} from 'uuid';
import { dirname } from "path";
import parseMD from 'parse-md';
import { MeiliSearch } from 'meilisearch';
import dotenv from 'dotenv';
dotenv.config();
let data = [];
console.log(process.env.MEILI_ADMIN_KEY)
const meiliClient = new MeiliSearch({
host: process.env['MEILI_ADMIN_HOST'],
apiKey: process.env['MEILI_ADMIN_KEY'],
headers: {'Access-Control-Allow-Origin': '*'}
})
function readFilesSync(dirname){
fs.readdirSync(dirname).forEach((filename) => {
if(!fs.lstatSync(dirname + filename).isDirectory()) {
const fileContent = fs.readFileSync(dirname + filename, 'utf-8');
if(filename.endsWith('.md') || filename.endsWith('.mdx')) {
const { content, metadata } = parseMD(fileContent);
data.push({
id: uuidv4(),
path: generatePathName(dirname + filename, metadata.slug),
...metadata,
body: content,
})
}
} else {
readFilesSync(dirname + filename + '/');
}
});
}
function generatePathName(filename, slug){
const split = filename.split('/docs');
let path = '';
// if the slug is present, use it instead of the file path
if(slug == undefined){
path = split[1].split('.')[0];
} else {
path = dirname(split[1]) + '/' + slug;
}
return path;
}
async function indexing(){
try {
await meiliClient.index(process.env.MEILI_INDEX).delete();
meiliClient.index(process.env.MEILI_INDEX).addDocuments(data);
console.info('meilisearch is done');
} catch (error) {
console.error('meilisearch is not available', error);
}
}
readFilesSync(process.env.PATH_TO_DOCUMENTS);
indexing();