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 23, 2017
1 parent 05e4a17 commit d3359d2
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 40 deletions.
69 changes: 39 additions & 30 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,30 @@
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();

var searchIndex;
// Called with the `this` context provided by Gitbook
function getSearchIndex(context) {
if (!searchIndex) {
// Create search index
var ignoreSpecialCharacters = context.config.get('pluginsConfig.lunr.ignoreSpecialCharacters') || context.config.get('lunr.ignoreSpecialCharacters');
searchIndex = lunr(function () {
this.ref('url');

this.field('title', { boost: 10 });
this.field('keywords', { boost: 15 });
this.field('body');

if (!ignoreSpecialCharacters) {
// Don't trim non words characters (to allow search such as "C++")
this.pipeline.remove(lunr.trimmer);
}
});
}
return searchIndex;
// Create search index
var ignoreSpecialCharacters = context.config.get('pluginsConfig.lunr.ignoreSpecialCharacters') || context.config.get('lunr.ignoreSpecialCharacters');
return lunr(function () {
this.ref('url');

this.field('title', { boost: 10 });
this.field('keywords', { boost: 15 });
this.field('body');

if (!ignoreSpecialCharacters) {
// Don't trim non words characters (to allow search such as "C++")
this.pipeline.remove(lunr.trimmer);
}
});
}

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

var searchIndexEnabled = true;
var indexSize = 0;

Expand Down Expand Up @@ -78,21 +75,33 @@ 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;
var searchIndex = getSearchIndex(this);
searchIndex.add(doc);

this.log.debug.ln('write search index');
return this.output.writeFile('search_index.json', JSON.stringify({
index: getSearchIndex(this),
store: documentsStore
var sourceIndex = JSON.parse(JSON.stringify(searchIndex));

if (targetJson.index.documentStore) {
_.merge(targetJson.index.corpusTokens, sourceIndex.corpusTokens);
_.merge(targetJson.index.documentStore.store, sourceIndex.documentStore.store);
_.merge(targetJson.index.tokenStore.root, sourceIndex.tokenStore.root);
} else {
targetJson.index = sourceIndex;
}

this.output.writeFile('search_index.json', JSON.stringify({
index: targetJson.index,
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 d3359d2

Please sign in to comment.