Skip to content

Commit 0c75a1f

Browse files
committedDec 23, 2022
add version to 5.x docs search build re: #12548
1 parent 2a08814 commit 0c75a1f

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed
 

‎docs/search.js

+13-7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const filemap = require('./source');
66
const fs = require('fs');
77
const pug = require('pug');
88
const mongoose = require('../');
9+
let { version } = require('../package.json');
910

1011
const markdown = require('marked');
1112
const highlight = require('highlight.js');
@@ -15,12 +16,14 @@ markdown.setOptions({
1516
}
1617
});
1718

18-
mongoose.set('useCreateIndex', true);
19+
// 5.13.5 -> 5.x, 6.8.2 -> 6.x, etc.
20+
version = version.slice(0, version.indexOf('.')) + '.x';
1921

2022
const contentSchema = new mongoose.Schema({
2123
title: { type: String, required: true },
2224
body: { type: String, required: true },
23-
url: { type: String, required: true }
25+
url: { type: String, required: true },
26+
version: { type: String, required: true, default: version }
2427
});
2528
contentSchema.index({ title: 'text', body: 'text' });
2629
const Content = mongoose.model('Content', contentSchema, 'Content');
@@ -30,7 +33,6 @@ const files = Object.keys(filemap);
3033

3134
for (const filename of files) {
3235
const file = filemap[filename];
33-
console.log(file)
3436
if (file.api) {
3537
// API docs are special, raw content is in the `docs` property
3638
for (const _class of file.docs) {
@@ -42,14 +44,15 @@ for (const filename of files) {
4244
});
4345
const err = content.validateSync();
4446
if (err != null) {
47+
console.log(content);
4548
throw err;
4649
}
4750
contents.push(content);
4851
}
4952
}
5053
} else if (file.markdown) {
5154
let text = fs.readFileSync(filename, 'utf8');
52-
text = markdown(text);
55+
text = markdown.parse(text);
5356

5457
const content = new Content({
5558
title: file.title,
@@ -114,15 +117,18 @@ for (const filename of files) {
114117
run().catch(error => console.error(error.stack));
115118

116119
async function run() {
117-
await mongoose.connect(config.uri, { useNewUrlParser: true, dbName: 'mongoose' });
120+
await mongoose.connect(config.uri, { dbName: 'mongoose' });
118121

119-
await Content.deleteMany({});
122+
await Content.deleteMany({ version });
120123
for (const content of contents) {
124+
if (version !== '6.x') {
125+
content.url = `/docs/${version}/docs${content.url}`;
126+
}
121127
await content.save();
122128
}
123129

124130
const results = await Content.
125-
find({ $text: { $search: 'validate' } }, { score: { $meta: 'textScore' } }).
131+
find({ $text: { $search: 'validate' }, version }, { score: { $meta: 'textScore' } }).
126132
sort({ score: { $meta: 'textScore' } }).
127133
limit(10);
128134

0 commit comments

Comments
 (0)
Please sign in to comment.