-
-
Notifications
You must be signed in to change notification settings - Fork 66
/
index.js
185 lines (166 loc) · 5.72 KB
/
index.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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
const fs = require("fs");
const path = require("path");
const util = require("util");
const readFileAsync = util.promisify(fs.readFile);
const writeFileAsync = util.promisify(fs.writeFile);
const lunr = require("lunr");
const { html2text } = require("./parse");
const { logger } = require("./logger");
const flatMap = (array, mapper) => {
return array.reduce((acc, element) => {
return acc.concat(mapper(element));
}, []);
};
module.exports = function(context, options) {
let blogBasePath =
options.blogBasePath !== undefined ? options.blogBasePath : "/blog";
let docsBasePath =
options.docsBasePath !== undefined ? options.docsBasePath : "/docs";
const indexPages =
options.indexPages !== undefined ? options.indexPages : false;
const indexBlog = options.indexBlog !== undefined ? options.indexBlog : true;
const indexDocs = options.indexDocs !== undefined ? options.indexDocs : true;
let language = options.language !== undefined ? options.language : "en";
if (Array.isArray(language) && language.length === 1) {
language = language[0];
}
if (!blogBasePath.startsWith("/")) {
throw new Error(
`blogBasePath must start with /, received: '${blogBasePath}'.`
);
}
if (!docsBasePath.startsWith("/")) {
throw new Error(
`docsBasePath must start with /, received: '${docsBasePath}'.`
);
}
blogBasePath = blogBasePath.substr(1);
docsBasePath = docsBasePath.substr(1);
let lunrClient =
"// THIS FILE IS AUTOGENERATED\n" +
"// DO NOT EDIT THIS FILE!\n\n" +
'import * as lunr from "lunr";\n';
if (language !== "en") {
require("lunr-languages/lunr.stemmer.support")(lunr);
lunrClient += 'require("lunr-languages/lunr.stemmer.support")(lunr);\n';
if (Array.isArray(language)) {
language
.filter(code => code !== "en")
.forEach(code => {
require(`lunr-languages/lunr.${code}`)(lunr);
lunrClient += `require("lunr-languages/lunr.${code}")(lunr);\n`;
});
require("lunr-languages/lunr.multi")(lunr);
lunrClient += `require("lunr-languages/lunr.multi")(lunr);\n`;
} else {
require(`lunr-languages/lunr.${language}`)(lunr);
lunrClient += `require("lunr-languages/lunr.${language}")(lunr);\n`;
}
}
lunrClient += `export default lunr;\n`;
const lunrClientPath = path.join(__dirname, "client-lunr.js");
fs.writeFileSync(lunrClientPath, lunrClient);
return {
name: "docusaurus-plugin",
getThemePath() {
return path.resolve(__dirname, "./theme");
},
async postBuild({ routesPaths = [], outDir, baseUrl }) {
logger.info("Gathering documents");
const data = flatMap(routesPaths, url => {
if (!url.startsWith(baseUrl)) {
throw new Error(
`The route must start with the baseUrl ${baseUrl}, but was ${route}. This is a bug, please report it.`
);
}
const route = url.substr(baseUrl.length);
if (route === "404.html") {
// Do not index error page.
return [];
}
if (indexBlog && route.startsWith(blogBasePath)) {
if (
route === blogBasePath ||
route.startsWith(`${blogBasePath}/tags/`) ||
route === `${blogBasePath}/tags`
) {
// Do not index list of blog posts and tags filter pages
return [];
}
return { route, url, type: "blog" };
}
if (indexDocs && route.startsWith(docsBasePath)) {
return { route, url, type: "docs" };
}
if (indexPages) {
return { route, url, type: "page" };
}
return [];
}).map(({ route, url, type }) => {
const file = path.join(outDir, route, "index.html");
return {
file,
url,
type
};
});
logger.info("Parsing documents");
// Give every index entry a unique id so that the index does not need to store long URLs.
let nextDocId = 1;
const documents = (
await Promise.all(
data.map(async ({ file, url, type }) => {
logger.debug(`Parsing ${type} file ${file}`, { url });
const html = await readFileAsync(file, { encoding: "utf8" });
const { pageTitle, sections } = html2text(html, type, url);
return sections.map(section => ({
id: nextDocId++,
pageTitle,
pageRoute: url,
sectionRoute: url + section.hash,
sectionTitle: section.title,
sectionContent: section.content
}));
})
)
).reduce((acc, val) => acc.concat(val), []); // .flat()
logger.info("Building index");
const index = lunr(function() {
if (language !== "en") {
if (Array.isArray(language)) {
this.use(lunr.multiLanguage(...language));
} else {
this.use(lunr[language]);
}
}
this.ref("id");
this.field("title");
this.field("content");
documents.forEach(function({ id, sectionTitle, sectionContent }) {
this.add({
id: id.toString(), // the ref must be a string
title: sectionTitle,
content: sectionContent
});
}, this);
});
logger.info("Writing index to disk");
await writeFileAsync(
path.join(outDir, "search-index.json"),
JSON.stringify({
documents: documents.map(
({ id, pageTitle, sectionTitle, sectionRoute }) => ({
id,
pageTitle,
sectionTitle,
sectionRoute
})
),
index
}),
{ encoding: "utf8" }
);
logger.info("Index written to disk, success!");
}
};
};