-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-search-data.js
More file actions
111 lines (100 loc) · 3.12 KB
/
build-search-data.js
File metadata and controls
111 lines (100 loc) · 3.12 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
const fs = require("fs-extra");
const path = require("path");
const cheerio = require("cheerio");
const BUILD_PATH = "./build/";
const SEARCH_DATA = [];
const sectionHeaderElements = ['h2', 'h3'];
const getContent = element => {
const text = element.is("table") || element.find('table').length !== 0 ?
element.html().replace(/<[^>]*>/g, " ") :
element.text();
return text
.replace(/\s\s+/g, " ")
.replace(/(\r\n|\n|\r)/gm, " ")
};
const getSectionContent = function (section) {
let content = "";
section = section.next();
while (section.length) {
if (sectionHeaderElements.some(s => section.is(s))) break;
content += getContent(section) + " ";
section = section.next();
}
return content;
};
const searchDirectory = (startPath, extension, callback) => {
if (!fs.existsSync(startPath)) {
return;
}
const files = fs.readdirSync(startPath);
files.forEach(file => {
const filePath = path.join(startPath, file);
const stats = fs.lstatSync(filePath);
if (stats.isDirectory()) {
searchDirectory(filePath, extension, callback);
} else if (path.extname(filePath) === extension) {
callback(filePath);
}
});
};
// Build search data for a html
const buildSearchData = filePath => {
const htmlFile = fs.readFileSync(filePath);
// const dom = new JSDOM(htmlFile);
const $ = cheerio.load(htmlFile);
const article = $("article");
if (!article.length) {
return;
}
const markdown = article.find(".markdown");
if (!markdown.length) {
return;
}
let baseUrl = filePath.split(path.sep);
// remove build folder path from url
baseUrl.splice(0, 1);
// remove index.html from the path
baseUrl.pop();
baseUrl = baseUrl.join("/");
const pageTitleElement = article.find("h1");
if (!pageTitleElement.length) {
return;
}
const pageTitle = article.find("h1").text();
const sectionHeaders = sectionHeaderElements.reduce((acc, selector) => {
acc = acc.concat(Array.from(markdown.find(selector)));
return acc;
}, [])
SEARCH_DATA.push({
title: pageTitle,
type: 0,
sectionRef: "#",
url: baseUrl,
// If there is no sections then push the complete content under page title
content: sectionHeaders.length === 0 ? getContent(markdown) : ""
});
sectionHeaders.forEach((sectionHeader) => {
sectionHeader = $(sectionHeader);
const title = sectionHeader.text().slice(1);
const sectionRef = sectionHeader
.children()
.first()
.attr("id");
const url = `${baseUrl}#${sectionRef}`;
const content = getSectionContent(sectionHeader);
SEARCH_DATA.push({
title,
type: 1,
pageTitle,
url,
content
});
});
};
const init = () => {
searchDirectory(BUILD_PATH, ".html", buildSearchData);
fs.writeFile("./src/theme/SearchBar/search-data.js", `
export default ${JSON.stringify(SEARCH_DATA, null, 2)}
`);
};
init();