From ad2446c367dd5d1c5e21d7f6428d001744a095f6 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Fri, 23 Dec 2022 11:40:51 -0500 Subject: [PATCH 1/2] docs: store content with associated version re: #12548 --- docs/search.js | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/docs/search.js b/docs/search.js index 8aa7d4c6455..83615f0ca96 100644 --- a/docs/search.js +++ b/docs/search.js @@ -6,6 +6,7 @@ const filemap = require('./source'); const fs = require('fs'); const pug = require('pug'); const mongoose = require('../'); +let { version } = require('../package.json'); const { marked: markdown } = require('marked'); const highlight = require('highlight.js'); @@ -15,10 +16,14 @@ markdown.setOptions({ } }); +// 5.13.5 -> 5.x, 6.8.2 -> 6.x, etc. +version = version.slice(0, version.indexOf('.')) + '.x'; + const contentSchema = new mongoose.Schema({ title: { type: String, required: true }, body: { type: String, required: true }, - url: { type: String, required: true } + url: { type: String, required: true }, + version: { type: String, required: true, default: version } }); contentSchema.index({ title: 'text', body: 'text' }); const Content = mongoose.model('Content', contentSchema, 'Content'); @@ -28,7 +33,6 @@ const files = Object.keys(filemap); for (const filename of files) { const file = filemap[filename]; - console.log(file) if (file.api) { // API docs are special, raw content is in the `docs` property for (const _class of file.docs) { @@ -115,8 +119,11 @@ run().catch(error => console.error(error.stack)); async function run() { await mongoose.connect(config.uri, { dbName: 'mongoose' }); - await Content.deleteMany({}); + await Content.deleteMany({ version }); for (const content of contents) { + if (version !== '6.x') { + content.url = `/docs/${version}/docs/${content.url}`; + } await content.save(); } From a4bd7cc91182abea73bb4570213afcc22bbcdf19 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Fri, 23 Dec 2022 12:46:45 -0500 Subject: [PATCH 2/2] docs: search by version in docs --- docs/js/search.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/docs/js/search.js b/docs/js/search.js index 71c623502e2..2d62ed66901 100644 --- a/docs/js/search.js +++ b/docs/js/search.js @@ -9,9 +9,12 @@ for (var i = 0; i < pairs.length; ++i) { } } +var versionFromUrl = window.location.pathname.match(/^\/docs\/(\d+\.x)/); +var version = versionFromUrl ? versionFromUrl[1] : '6.x'; + if (q != null) { document.getElementById('search-input').value = decodeURIComponent(q); - fetch(root + '/search?search=' + q). + fetch(root + '/search?search=' + q + '&version=' + version). then(function(res) { return res.json(); }). then( function(result) {