Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve search generation script #13044

Merged
merged 7 commits into from
Feb 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
"docs:checkout:gh-pages": "git checkout gh-pages",
"docs:checkout:legacy": "git checkout 5.x",
"docs:generate": "node ./scripts/website.js",
"docs:generate:search": "node docs/search.js",
"docs:generate:search": "node ./scripts/generateSearch.js",
"docs:merge:stable": "git merge master",
"docs:merge:legacy": "git merge 5.x",
"docs:test": "npm run docs:generate && npm run docs:generate:search",
Expand Down
33 changes: 23 additions & 10 deletions docs/search.js → scripts/generateSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const config = require('../.config');
const cheerio = require('cheerio');
const filemap = require('./source');
const filemap = require('../docs/source');
const fs = require('fs');
const pug = require('pug');
const mongoose = require('../');
Expand All @@ -16,6 +16,8 @@ markdown.setOptions({
}
});

mongoose.set('strictQuery', false);

// 5.13.5 -> 5.x, 6.8.2 -> 6.x, etc.
version = version.slice(0, version.indexOf('.')) + '.x';

Expand All @@ -29,22 +31,20 @@ contentSchema.index({ title: 'text', body: 'text' });
const Content = mongoose.model('Content', contentSchema, 'Content');

const contents = [];
const files = Object.keys(filemap);

for (const filename of files) {
const file = filemap[filename];
for (const [filename, file] of Object.entries(filemap)) {
if (file.api) {
// API docs are special, raw content is in the `docs` property
for (const _class of file.docs) {
for (const prop of _class.props) {
const content = new Content({
title: `API: ${prop.string}`,
body: prop.description,
url: `api.html#${prop.anchorId}`
url: `api/${_class.fileName}.html#${prop.anchorId}`
});
const err = content.validateSync();
if (err != null) {
console.log(content);
console.error(content);
throw err;
}
contents.push(content);
Expand Down Expand Up @@ -107,24 +107,37 @@ for (const filename of files) {
body: html,
url: `${filename.replace('.pug', '.html').replace(/^docs/, '')}#${el.prop('id')}`
});

content.validateSync();
contents.push(content);
});
}
}

run().catch(error => console.error(error.stack));
run().catch(async error => {
console.error(error.stack);

// ensure the script exists in case of error
await mongoose.disconnect();
});

async function run() {
if (!config || !config.uri) {
console.error('No Config or config.URI given, please create a .config.js file with those values');
process.exit(-1);
}

await mongoose.connect(config.uri, { dbName: 'mongoose' });

// wait for the index to be created
await Content.init();

await Content.deleteMany({ version });
for (const content of contents) {
if (version === '6.x') {
let url = content.url.startsWith('/') ? content.url : `/${content.url}`;
if (!url.startsWith('/docs')) {
url = '/docs' + url;
url = '/docs' + url;
}
content.url = url;
} else {
Expand All @@ -142,4 +155,4 @@ async function run() {
console.log(results.map(res => res.url));

process.exit(0);
}
}