-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerateSearchIndex.js
42 lines (36 loc) · 1.39 KB
/
generateSearchIndex.js
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
const fs = require('fs');
const path = require('path');
const matter = require('gray-matter');
const { marked } = require('marked');
const docsDir = path.join(__dirname, 'docs');
const outputFilePath = path.join(__dirname, 'static', 'search-index.json'); // Adjust path as needed
// Function to get all .md files in a directory
function getAllMarkdownFiles(dirPath, arrayOfFiles = []) {
const files = fs.readdirSync(dirPath);
files.forEach((file) => {
const filePath = path.join(dirPath, file);
if (fs.statSync(filePath).isDirectory()) {
arrayOfFiles = getAllMarkdownFiles(filePath, arrayOfFiles);
} else if (file.endsWith('.md')) {
arrayOfFiles.push(filePath);
}
});
return arrayOfFiles;
}
// Function to generate search data
function generateSearchIndex() {
const files = getAllMarkdownFiles(docsDir);
const index = files.map((file) => {
const fileContent = fs.readFileSync(file, 'utf-8');
const { data, content } = matter(fileContent);
const snippet = marked(content).replace(/(<([^>]+)>)/gi, '').slice(0, 200);
return {
title: data.title || path.basename(file, '.md'),
url: `/docs/${path.relative(docsDir, file).replace(/\\/g, '/').replace('.md', '')}`,
content: snippet,
};
});
fs.writeFileSync(outputFilePath, JSON.stringify(index, null, 2));
console.log(`Search index generated at ${outputFilePath}`);
}
generateSearchIndex();