-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
69 lines (61 loc) · 1.35 KB
/
index.js
File metadata and controls
69 lines (61 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
const Fuse = require("fuse.js");
const data = require("./data.json");
let input = process.argv[2] || "";
const searchData = data.map((v) => {
v.key = v.path.replace(/docs\/(.+)\.html$/, "$1");
return v;
});
if (input === "") {
output(searchData.map((item) => mapOutputItem(item)));
process.exit(0);
}
const options = {
includeScore: false,
shouldSort: true,
threshold: 0.6,
keys: [
{ name: "name", weight: 100 },
{ name: "key", weight: 100 },
{ name: "tags", weight: 10 },
{ name: "intro", weight: 2 },
],
};
const fuse = new Fuse(searchData, options);
const result = fuse.search(input);
output(result.map(({ item }) => mapOutputItem(item)));
function mapOutputItem(item) {
return {
valid: true,
title: `${item.name}`,
subtitle: `${item.intro}`,
arg: item.key,
variables: {
MODE: "openpage",
TOPICPATH: item.path,
TOPICHASH: "",
TOPICNAME: item.name,
},
mods: {
cmd: {
valid: true,
arg: item.key,
subtitle: `检索【${item.name}】的章节`,
variables: {
MODE: "sectionsearch",
TOPICPATH: item.path,
TOPICLEVEL: 2,
TOPICKEY: item.key,
TOPICNAME: item.name,
},
},
},
};
}
function output(items) {
console.log(
JSON.stringify({
return: 0.1,
items,
})
);
}