Skip to content

Commit 94dc916

Browse files
committed
chore(generateSearch): move content generation to function
1 parent 6e4a649 commit 94dc916

File tree

1 file changed

+69
-62
lines changed

1 file changed

+69
-62
lines changed

scripts/generateSearch.js

Lines changed: 69 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -37,85 +37,89 @@ const contentSchema = new mongoose.Schema({
3737
contentSchema.index({ title: 'text', body: 'text' });
3838
const Content = mongoose.model('Content', contentSchema, 'Content');
3939

40-
const contents = [];
40+
function generateContents() {
41+
const contents = [];
42+
43+
for (const [filename, file] of Object.entries(docsFilemap.fileMap)) {
44+
if (file.api) {
45+
for (const prop of file.props) {
46+
const content = new Content({
47+
title: `API: ${prop.name}`,
48+
body: prop.description,
49+
url: `${filename}#${prop.anchorId}`
50+
});
51+
const err = content.validateSync();
52+
if (err != null) {
53+
console.error(content);
54+
throw err;
55+
}
56+
contents.push(content);
57+
}
58+
} else if (file.markdown) {
59+
let text = fs.readFileSync(filename, 'utf8');
60+
text = markdown.parse(text);
4161

42-
for (const [filename, file] of Object.entries(docsFilemap.fileMap)) {
43-
if (file.api) {
44-
for (const prop of file.props) {
4562
const content = new Content({
46-
title: `API: ${prop.name}`,
47-
body: prop.description,
48-
url: `${filename}#${prop.anchorId}`
63+
title: file.title,
64+
body: text,
65+
url: filename.replace('.md', '.html').replace(/^docs/, '')
4966
});
50-
const err = content.validateSync();
51-
if (err != null) {
52-
console.error(content);
53-
throw err;
54-
}
55-
contents.push(content);
56-
}
57-
} else if (file.markdown) {
58-
let text = fs.readFileSync(filename, 'utf8');
59-
text = markdown.parse(text);
6067

61-
const content = new Content({
62-
title: file.title,
63-
body: text,
64-
url: filename.replace('.md', '.html').replace(/^docs/, '')
65-
});
68+
content.validateSync();
6669

67-
content.validateSync();
70+
const $ = cheerio.load(text);
6871

69-
const $ = cheerio.load(text);
72+
contents.push(content);
7073

71-
contents.push(content);
74+
// Break up individual h3's into separate content for more fine grained search
75+
$('h3').each((index, el) => {
76+
el = $(el);
77+
const title = el.text();
78+
const html = el.nextUntil('h3').html();
79+
const content = new Content({
80+
title: `${file.title}: ${title}`,
81+
body: html,
82+
url: `${filename.replace('.md', '.html').replace(/^docs/, '')}#${el.prop('id')}`
83+
});
84+
85+
content.validateSync();
86+
contents.push(content);
87+
});
88+
} else if (file.guide) {
89+
let text = fs.readFileSync(filename, 'utf8');
90+
text = text.substring(text.indexOf('block content') + 'block content\n'.length);
91+
text = pug.render(`div\n${text}`, { filters: { markdown }, filename });
7292

73-
// Break up individual h3's into separate content for more fine grained search
74-
$('h3').each((index, el) => {
75-
el = $(el);
76-
const title = el.text();
77-
const html = el.nextUntil('h3').html();
7893
const content = new Content({
79-
title: `${file.title}: ${title}`,
80-
body: html,
81-
url: `${filename.replace('.md', '.html').replace(/^docs/, '')}#${el.prop('id')}`
94+
title: file.title,
95+
body: text,
96+
url: filename.replace('.pug', '.html').replace(/^docs/, '')
8297
});
8398

8499
content.validateSync();
85-
contents.push(content);
86-
});
87-
} else if (file.guide) {
88-
let text = fs.readFileSync(filename, 'utf8');
89-
text = text.substring(text.indexOf('block content') + 'block content\n'.length);
90-
text = pug.render(`div\n${text}`, { filters: { markdown }, filename });
91-
92-
const content = new Content({
93-
title: file.title,
94-
body: text,
95-
url: filename.replace('.pug', '.html').replace(/^docs/, '')
96-
});
97-
98-
content.validateSync();
99100

100-
const $ = cheerio.load(text);
101+
const $ = cheerio.load(text);
101102

102-
contents.push(content);
103+
contents.push(content);
103104

104-
// Break up individual h3's into separate content for more fine grained search
105-
$('h3').each((index, el) => {
106-
el = $(el);
107-
const title = el.text();
108-
const html = el.nextUntil('h3').html();
109-
const content = new Content({
110-
title: `${file.title}: ${title}`,
111-
body: html,
112-
url: `${filename.replace('.pug', '.html').replace(/^docs/, '')}#${el.prop('id')}`
105+
// Break up individual h3's into separate content for more fine grained search
106+
$('h3').each((index, el) => {
107+
el = $(el);
108+
const title = el.text();
109+
const html = el.nextUntil('h3').html();
110+
const content = new Content({
111+
title: `${file.title}: ${title}`,
112+
body: html,
113+
url: `${filename.replace('.pug', '.html').replace(/^docs/, '')}#${el.prop('id')}`
114+
});
115+
116+
content.validateSync();
117+
contents.push(content);
113118
});
114-
115-
content.validateSync();
116-
contents.push(content);
117-
});
119+
}
118120
}
121+
122+
return contents;
119123
}
120124

121125
run().catch(async error => {
@@ -132,6 +136,9 @@ async function run() {
132136
await Content.init();
133137

134138
await Content.deleteMany({ version });
139+
140+
const contents = generateContents();
141+
135142
let count = 0;
136143
for (const content of contents) {
137144
if (version === '8.x') {

0 commit comments

Comments
 (0)