-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.js
More file actions
66 lines (54 loc) · 2.02 KB
/
Copy pathvite.config.js
File metadata and controls
66 lines (54 loc) · 2.02 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
61
62
63
64
65
66
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import fs from 'fs'
import path from 'path'
import { fileURLToPath } from 'url'
const __dirname = path.dirname(fileURLToPath(import.meta.url))
function generateSearchIndex() {
const pagesDir = path.resolve(__dirname, 'src/pages');
const outputDir = path.resolve(__dirname, 'src/data');
const outputFile = path.join(outputDir, 'searchIndex.json');
if (!fs.existsSync(pagesDir)) return;
if (!fs.existsSync(outputDir)) fs.mkdirSync(outputDir, { recursive: true });
const files = fs.readdirSync(pagesDir).filter(f => f.endsWith('.jsx'));
const index = files.map((file, id) => {
const content = fs.readFileSync(path.join(pagesDir, file), 'utf-8');
const titleMatch = content.match(/<h1>(.*?)<\/h1>/);
const title = titleMatch ? titleMatch[1].replace(/<[^>]*>?/gm, '') : file.replace('.jsx', '');
const pMatches = content.match(/<p>(.*?)<\/p>/gs) || [];
const bulletMatches = content.match(/<BulletPoint>(.*?)<\/BulletPoint>/gs) || [];
const allMatches = [...pMatches, ...bulletMatches];
const textContent = allMatches
.map(tag => tag.replace(/<[^>]*>?/gm, '').trim())
.filter(text => text.length > 0)
.join(' ');
let routePath = `/${file.replace('.jsx', '').toLowerCase()}`;
if (file === 'Introduction.jsx' || file === 'Home.jsx') routePath = '/';
if (file === 'GettingStarted.jsx') routePath = '/getting-started';
return {
id: id + 1,
title,
path: routePath,
content: textContent
};
});
fs.writeFileSync(outputFile, JSON.stringify(index, null, 2));
console.log('Search index regenerated!');
}
function autoSearchIndexPlugin() {
return {
name: 'auto-search-index',
buildStart() {
generateSearchIndex();
},
handleHotUpdate({ file }) {
if (file.includes('src/pages') && file.endsWith('.jsx')) {
generateSearchIndex();
}
}
};
}
export default defineConfig({
plugins: [react(), autoSearchIndexPlugin()],
base: '/',
})