Mikael's blog

A developers sixth time trying to maintain a blog

Source Code - Root / searchManager.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
/*****************************************
 *   Search Manager
 *****************************************
 *   Author:  mikael.lofjard@gmail.com
 *   Website: http://lofjard.se
 *   License: MIT License
 ****************************************/
 
var SearchManager = (function () {
 
  var env = require('./environmentManager').EnvironmentManager;
  var misc = require('./misc').Misc;
  var lunr = require('./forked/lunr');
 
  var db = null;
  var searchIndex = null;
  var allPosts = null;
 
  var createOuttake = function (text) {
   // remove <del> content
    var outtake = text.replace(/<del.*?>.*?<\/del>/g, "");
    // remove html
    outtake = outtake.replace(/<.*?>/g, "");
    // remove markdown headers
    outtake = outtake.replace(/####.*/g, "");
    // remove markdown links
    outtake = outtake.replace(/\[(.*?)\]\(.*?\)/g, "$1");
    // fixup and shorten
    outtake = outtake.replace(/\.\n/g, '. ').replace(/[\n\t]/g,' ');
    // remove markdown code
    outtake = outtake.replace(/```.*?```/g, "");
    // shorten
    outtake = outtake.substring(0,250) + '...';
    return outtake;
  };
 
  var scan = function () {
    searchIndex = lunr(function () {
      this.field('title', { boost: 10 });
      this.field('post');
      this.field('imagetext');
    });
 
    env.info("SearchManager: Building index");
 
    // init lunr index
    db.view('posts/byDateTime', {}, function (err, doc) {
      if (err) {
        env.info("CouchDB: DB Read error");
        return;
      }
 
      allPosts = {};
 
      for(var i in doc) {
        var dbDoc = doc[i].value;
        var searchDoc = {
          id: dbDoc.readableKey,
          title: dbDoc.title,
          post: dbDoc.htmlContent,
          imagetext: dbDoc.pictureSubText + ''
        };
 
        var postDoc = {
          readableKey: dbDoc.readableKey,
          title: dbDoc.title,
          outtake: createOuttake(dbDoc.htmlContent),
          readableDateTime: misc.prettyDate(dbDoc.dateTime),
          author: dbDoc.author
        }
 
        env.info(" - Indexing: " + searchDoc.id);
        searchIndex.add(searchDoc);
        allPosts[dbDoc.readableKey] = postDoc;
      }
    });
  }
 
  return {
 
    init: function (dbInit) {
      db = dbInit;
      scan();
    },
 
    rescan: function () {
      scan();
    },
 
    search: function (term) {
      env.info('SearchManager: Searching for "' + term + '"');
      var result = searchIndex.search(term);
      var docResults = [];
 
      // populate docResults with docs from allPosts
      for(var i = 0; i < result.length; i++) {
        docResults.push(allPosts[result[i]["ref"]]);
      }
      return docResults;
    }
  };
}());
 
typeof(exports) != 'undefined' ? exports.SearchManager = SearchManager : null;