1 | var router = require("express").Router(),
|
2 | path = require("path")
|
3 | models = require("../lib/models");
|
4 |
|
5 | models.use(Git);
|
6 |
|
7 | router.get("/search", _getSearch);
|
8 |
|
9 | function _getSearch(req, res) {
|
10 |
|
11 | var items = [],
|
12 | record;
|
13 |
|
14 | res.locals.matches = [];
|
15 |
|
16 | if (req.query.term) {
|
17 | res.locals.term = req.query.term.trim();
|
18 | } else {
|
19 | res.locals.term = "";
|
20 | }
|
21 |
|
22 | if (res.locals.term.length == 0) {
|
23 | renderResults();
|
24 | return;
|
25 | }
|
26 |
|
27 | if (res.locals.term.length < 2) {
|
28 |
|
29 | res.locals.warning = "Search string is too short.";
|
30 | renderResults();
|
31 | } else {
|
32 |
|
33 | try {
|
34 | new RegExp(res.locals.term);
|
35 | } catch(e) {
|
36 | res.locals.warning = "The format of the search string is invalid.";
|
37 | renderResults();
|
38 | return;
|
39 | }
|
40 |
|
41 | models.pages.findStringAsync(res.locals.term).then(function(items) {
|
42 |
|
43 | items.forEach(function(item) {
|
44 | if (item.trim() !== "") {
|
45 | record = item.split(":");
|
46 | res.locals.matches.push({
|
47 | pageName: path.basename(record[0].split(".")[0]),
|
48 | line: record[1] ? ":" + record[1] : "",
|
49 | text: record.slice(2).join('')
|
50 | });
|
51 | }
|
52 | });
|
53 |
|
54 | renderResults();
|
55 | });
|
56 | }
|
57 |
|
58 | function renderResults() {
|
59 | res.render("search", {
|
60 | title: "Search results"
|
61 | });
|
62 | }
|
63 | }
|
64 |
|
65 | module.exports = router;
|