Skip to content
This repository has been archived by the owner on Nov 24, 2020. It is now read-only.

Commit

Permalink
Generate index file when rendering specfic page
Browse files Browse the repository at this point in the history
  • Loading branch information
aleen42 committed Nov 22, 2017
1 parent 05e4a17 commit 8223017
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 22 deletions.
26 changes: 14 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
var fs = require('fs');
var path = require('path');
var lunr = require('lunr');
var Entities = require('html-entities').AllHtmlEntities;
var _ = require('lodash');

var Html = new Entities();

Expand All @@ -26,8 +29,6 @@ function getSearchIndex(context) {
}

// Map of Lunr ref to document
var documentsStore = {};

var searchIndexEnabled = true;
var indexSize = 0;

Expand Down Expand Up @@ -78,21 +79,22 @@ module.exports = {
body: text
};

var documentsStore = {};
documentsStore[doc.url] = doc;
getSearchIndex(this).add(doc);

return page;
},
var targetFile = path.resolve(this.output.root(), 'search_index.json');
var targetJson = JSON.parse(fs.existsSync(targetFile) ?
fs.readFileSync(targetFile)
: '{"index":{},"store":{}}');

// Write index to disk
'finish': function() {
if (this.output.name != 'website') return;
getSearchIndex(this).add(doc);

this.log.debug.ln('write search index');
return this.output.writeFile('search_index.json', JSON.stringify({
index: getSearchIndex(this),
store: documentsStore
this.output.writeFile('search_index.json', JSON.stringify({
index: _.merge(targetJson.index, JSON.parse(JSON.stringify(getSearchIndex(this)))),
store: Object.assign(targetJson.store, documentsStore)
}));

return page;
}
}
};
Expand Down
21 changes: 11 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"dependencies": {
"gitbook-plugin-search": "*",
"html-entities": "1.2.0",
"lodash": "^4.17.4",
"lunr": "0.5.12"
},
"homepage": "https://github.com/GitbookIO/plugin-lunr",
Expand All @@ -22,16 +23,16 @@
},
"gitbook": {
"properties": {
"maxIndexSize": {
"type": "number",
"title": "Limit size for the index",
"default": 1000000
},
"ignoreSpecialCharacters": {
"type": "boolean",
"title": "Ignore special characters in words",
"default": false
}
"maxIndexSize": {
"type": "number",
"title": "Limit size for the index",
"default": 1000000
},
"ignoreSpecialCharacters": {
"type": "boolean",
"title": "Ignore special characters in words",
"default": false
}
}
},
"scripts": {
Expand Down

0 comments on commit 8223017

Please sign in to comment.