jekyll-gitbook/gitbook/gitbook-plugin-search/search-engine.js
HE, Tao e18e0cee7e
Add static resources from gitbook.
Signed-off-by: HE, Tao <sighingnow@gmail.com>
2019-04-28 16:37:41 +08:00

50 lines
1.2 KiB
JavaScript

require([
'gitbook',
'jquery'
], function(gitbook, $) {
// Global search objects
var engine = null;
var initialized = false;
// Set a new search engine
function setEngine(Engine, config) {
initialized = false;
engine = new Engine(config);
init(config);
}
// Initialize search engine with config
function init(config) {
if (!engine) throw new Error('No engine set for research. Set an engine using gitbook.research.setEngine(Engine).');
return engine.init(config)
.then(function() {
initialized = true;
gitbook.events.trigger('search.ready');
});
}
// Launch search for query q
function query(q, offset, length) {
if (!initialized) throw new Error('Search has not been initialized');
return engine.search(q, offset, length);
}
// Get stats about search
function getEngine() {
return engine? engine.name : null;
}
function isInitialized() {
return initialized;
}
// Initialize gitbook.search
gitbook.search = {
setEngine: setEngine,
getEngine: getEngine,
query: query,
isInitialized: isInitialized
};
});