forked from DustinBrett/daedalOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearchIndex.js
More file actions
133 lines (110 loc) · 2.92 KB
/
searchIndex.js
File metadata and controls
133 lines (110 loc) · 2.92 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
const {
existsSync,
readdirSync,
readFileSync,
statSync,
writeFileSync,
mkdirSync,
} = require("fs");
const { basename, extname, join } = require("path");
const { parse } = require("ini");
const lunr = require("lunr");
const PUBLIC_PATH = "public";
const SEARCH_EXTENSIONS = require("./searchExtensions.json");
const IGNORE_FILES = new Set([
"desktop.ini",
"favicon.ico",
"fs.9p.json",
"preload.json",
"robots.txt",
"search.lunr.json",
"sitemap.xml",
]);
const IGNORE_PATHS = [
".index",
"Program Files",
"System",
"Users/Public/Icons",
"Users/Public/Pictures/Blog",
];
const indexData = [];
const normalizePath = (path) =>
path.replace(/\\/g, "/").replace(PUBLIC_PATH, "");
const normalizeText = (text) =>
text.replace(/\r?\n|\r/g, " ").replace(/<\/?[^>]+(>|$)/g, "");
const keyPathMap = {};
const keyPathMapper = (path) =>
(keyPathMap[path] ||= Object.keys(keyPathMap).length);
const createSearchIndex = (path) => {
const directoryContents = readdirSync(path);
const normalizedPath = normalizePath(path);
if (normalizedPath) {
const name = basename(path);
indexData.push({
name,
path: keyPathMapper(normalizedPath),
text: normalizeText(
[
name,
...directoryContents.filter(
(entry) => !statSync(join(path, entry)).isDirectory()
),
].join(" ")
),
});
}
directoryContents.forEach((entry) => {
if (
IGNORE_PATHS.some((ignoredPath) =>
path.startsWith(join(PUBLIC_PATH, ignoredPath))
)
) {
return;
}
const fullPath = join(path, entry);
const stats = statSync(fullPath);
if (stats.isDirectory()) {
createSearchIndex(fullPath);
} else if (
!IGNORE_FILES.has(entry) &&
!SEARCH_EXTENSIONS.ignore.includes(extname(entry).toLowerCase())
) {
const keyPath = normalizePath(fullPath);
if (extname(entry).toLowerCase() === ".url") {
const {
InternetShortcut: { URL: url = "" },
} = parse(readFileSync(fullPath).toString());
if (url.length > 1 && url.startsWith("/")) {
return;
}
}
const name = basename(keyPath, extname(keyPath));
indexData.push({
name,
path: keyPathMapper(keyPath),
text: SEARCH_EXTENSIONS.index.includes(extname(entry).toLowerCase())
? `${name} ${normalizeText(readFileSync(fullPath, "utf8"))}`
: name,
});
}
});
};
createSearchIndex(PUBLIC_PATH);
const searchIndex = lunr(function () {
this.ref("path");
this.field("name");
this.field("text");
indexData.forEach((doc) => this.add(doc));
});
if (!existsSync(join(PUBLIC_PATH, ".index"))) {
mkdirSync(join(PUBLIC_PATH, ".index"));
}
const searchJson = searchIndex.toJSON();
searchJson.paths = Object.keys(keyPathMap);
writeFileSync(
join(PUBLIC_PATH, ".index/search.lunr.json"),
JSON.stringify(searchJson),
{
flag: "w",
}
);