deno.land / x / mongoose@6.7.5 / docs / search.js

نووسراو ببینە
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
'use strict';
const config = require('../.config');const cheerio = require('cheerio');const filemap = require('./source');const fs = require('fs');const pug = require('pug');const mongoose = require('../');
const { marked: markdown } = require('marked');const highlight = require('highlight.js');markdown.setOptions({ highlight: function(code) { return highlight.highlight(code, { language: 'JavaScript' }).value; }});
const contentSchema = new mongoose.Schema({ title: { type: String, required: true }, body: { type: String, required: true }, url: { type: String, required: true }});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]; console.log(file) 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}` }); const err = content.validateSync(); if (err != null) { console.log(content); throw err; } contents.push(content); } } } else if (file.markdown) { let text = fs.readFileSync(filename, 'utf8'); text = markdown.parse(text);
const content = new Content({ title: file.title, body: text, url: filename.replace('.md', '.html').replace(/^docs/, '') });
content.validateSync();
const $ = cheerio.load(text);
contents.push(content);
// Break up individual h3's into separate content for more fine grained search $('h3').each((index, el) => { el = $(el); const title = el.text(); const html = el.nextUntil('h3').html(); const content = new Content({ title: `${file.title}: ${title}`, body: html, url: `${filename.replace('.md', '.html').replace(/^docs/, '')}#${el.prop('id')}` });
content.validateSync(); contents.push(content); }); } else if (file.guide) { let text = fs.readFileSync(filename, 'utf8'); text = text.substr(text.indexOf('block content') + 'block content\n'.length); text = pug.render(`div\n${text}`, { filters: { markdown }, filename });
const content = new Content({ title: file.title, body: text, url: filename.replace('.pug', '.html').replace(/^docs/, '') });
content.validateSync();
const $ = cheerio.load(text);
contents.push(content);
// Break up individual h3's into separate content for more fine grained search $('h3').each((index, el) => { el = $(el); const title = el.text(); const html = el.nextUntil('h3').html(); const content = new Content({ title: `${file.title}: ${title}`, body: html, url: `${filename.replace('.pug', '.html').replace(/^docs/, '')}#${el.prop('id')}` }); content.validateSync(); contents.push(content); }); }}
run().catch(error => console.error(error.stack));
async function run() { await mongoose.connect(config.uri, { dbName: 'mongoose' });
await Content.deleteMany({}); for (const content of contents) { await content.save(); }
const results = await Content. find({ $text: { $search: 'validate' } }, { score: { $meta: 'textScore' } }). sort({ score: { $meta: 'textScore' } }). limit(10);
console.log(results.map(res => res.url));
process.exit(0);}
mongoose

Version Info

Tagged at
a year ago