forked from MihaiValentin/lunr-languages
-
Notifications
You must be signed in to change notification settings - Fork 9
/
lunr.multi.js
63 lines (60 loc) · 2.48 KB
/
lunr.multi.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/**
* export the module via AMD, CommonJS or as a browser global
* Export code from https://github.com/umdjs/umd/blob/master/returnExports.js
*/
;(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(factory)
} else if (typeof exports === 'object') {
/**
* Node. Does not work with strict CommonJS, but
* only CommonJS-like environments that support module.exports,
* like Node.
*/
module.exports = factory()
} else {
// Browser globals (root is window)
factory()(root.lunr);
}
}(this, function () {
/**
* Just return a value to define the module export.
* This example returns an object, but the module
* can return a function as the exported value.
*/
return function(lunr) {
/* Set up the pipeline for indexing content in multiple languages. The
corresponding lunr.{lang} files must be loaded before calling this
function; English ('en') is built in.
Returns: a lunr plugin for use in your indexer.
Known drawback: every word will be stemmed with stemmers for every
language. This could mean that sometimes words that have the same
stemming root will not be stemmed as such.
*/
lunr.multiLanguage = function(/* lang1, lang2, ... */) {
var languages = Array.prototype.slice.call(arguments);
var nameSuffix = languages.join('-');
var wordCharacters = "";
var pipeline = [];
for (var i = 0; i < languages.length; ++i) {
if (languages[i] == 'en') {
wordCharacters += '\\w';
pipeline.unshift(lunr.stopWordFilter);
pipeline.push(lunr.stemmer);
} else {
wordCharacters += lunr[languages[i]].wordCharacters;
pipeline.unshift(lunr[languages[i]].stopWordFilter);
pipeline.push(lunr[languages[i]].stemmer);
}
};
var multiTrimmer = lunr.trimmerSupport.generateTrimmer(wordCharacters);
lunr.Pipeline.registerFunction(multiTrimmer, 'lunr-multi-trimmer-' + nameSuffix);
pipeline.unshift(multiTrimmer);
return function() {
this.pipeline.reset();
this.pipeline.add.apply(this.pipeline, pipeline);
};
}
}
}));