lunr.multi.js 3.2 KB

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