-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.ts
49 lines (39 loc) · 1.25 KB
/
plugin.ts
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
import { registerPlugin, HandledRoute, getMyConfig } from '@scullyio/scully';
import fs from 'fs';
import path from 'path';
export interface LunrPluginOptions {
lunr: Function;
lang?: string;
indexJsonOutputPath: string;
}
const Plugin = async (routes: HandledRoute[]): Promise<HandledRoute[]> => {
const config: LunrPluginOptions = getMyConfig(Plugin);
const lunr = config.lunr;
const lunrIndex = lunr(function () {
let lunr_ = this;
lunr_.ref('route');
lunr_.field('content');
if (config.lang) {
lunr_.use((lunr as any)[config.lang]);
}
routes.map((route) => {
if (route.type !== 'contentFolder') return route;
if (!route.templateFile) return route;
const content = fs.readFileSync(route.templateFile).toString('utf-8');
lunr_.add({
route: route.route,
content: content,
});
return route;
});
});
// Save index of lunr
const lunrIndexJsonOutputDir = path.dirname(config.indexJsonOutputPath);
if (!fs.existsSync(lunrIndexJsonOutputDir)) {
fs.mkdirSync(lunrIndexJsonOutputDir, { recursive: true });
}
fs.writeFileSync(config.indexJsonOutputPath, JSON.stringify(lunrIndex));
// Done
return routes;
};
registerPlugin('routeProcess', 'lunr', Plugin, 100);