summaryrefslogtreecommitdiffstats
path: root/lib/book.js
diff options
context:
space:
mode:
authorSamy Pessé <samypesse@gmail.com>2015-01-22 22:31:47 +0100
committerSamy Pessé <samypesse@gmail.com>2015-01-22 22:31:47 +0100
commit74ea419c530921374ffbe893f01b6a4695995349 (patch)
treeeb58ec1c960f2747831cfae0dcee2d0447b3705a /lib/book.js
parent0c8513801c131ec6e2c7b348403a4c66a9bf2a11 (diff)
downloadgitbook-74ea419c530921374ffbe893f01b6a4695995349.zip
gitbook-74ea419c530921374ffbe893f01b6a4695995349.tar.gz
gitbook-74ea419c530921374ffbe893f01b6a4695995349.tar.bz2
Write search index to output folder
Diffstat (limited to 'lib/book.js')
-rw-r--r--lib/book.js26
1 files changed, 25 insertions, 1 deletions
diff --git a/lib/book.js b/lib/book.js
index 90322f2..8c9d8ae 100644
--- a/lib/book.js
+++ b/lib/book.js
@@ -1,7 +1,7 @@
var Q = require("q");
var _ = require("lodash");
var path = require("path");
-
+var lunr = require('lunr');
var parsers = require("gitbook-parsers");
var fs = require("./utils/fs");
@@ -54,6 +54,14 @@ var Book = function(root, options, parent) {
// Readme file
this.readmeFile = "README.md";
+
+ // Search Index
+ this.searchIndex = lunr(function () {
+ this.ref('url');
+
+ this.field('title', { boost: 10 });
+ this.field('body');
+ });
};
// Initialize and parse the book: config, summary, glossary
@@ -333,6 +341,10 @@ Book.prototype.parsePage = function(filename) {
});
return page;
+ })
+ .then(function(page) {
+ that.indexPage(page);
+ return page;
});
};
@@ -400,4 +412,16 @@ Book.prototype.parentRoot = function() {
return this.root;
};
+// Index a page into the search index
+Book.prototype.indexPage = function(page) {
+ var nav = this.navigation[page.path];
+ if (!nav) return;
+
+ this.searchIndex.add({
+ url: page.path,
+ title: nav.title,
+ body: pageUtil.extractText(page.sections),
+ });
+};
+
module.exports= Book;