Skip to content
This repository was archived by the owner on Nov 24, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 30 additions & 30 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,29 @@
var fs = require('fs');
var path = require('path');
var lunr = require('lunr');
var Entities = require('html-entities').AllHtmlEntities;

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 +74,25 @@ 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, { encoding: 'utf8' }) : '{"index":{},"store":{}}');

var searchIndex = targetJSON.index.documentStore ?
lunr.Index.load(targetJSON.index)
: getSearchIndex(this);

// Write index to disk
'finish': function() {
if (this.output.name != 'website') return;
searchIndex.remove(doc);
searchIndex.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: searchIndex,
store: Object.assign(targetJSON.store, documentsStore)
}));

return page;
}
}
};
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "gitbook-plugin-lunr",
"name": "@aleen42/gitbook-plugin-lunr",
"description": "Index book in a lunr index accessible from the search plugin",
"main": "index.js",
"version": "1.2.0",
"version": "1.0.0",
"engines": {
"gitbook": ">=3.0.0-pre.0"
},
Expand Down