cullmann.io/themes/CodeIT/assets/lib/lunr/lunr.th.js

110 lines
3.1 KiB
JavaScript
Raw Normal View History

2021-02-18 21:44:01 +01:00
/*!
* Lunr languages, `Thai` language
* https://github.com/MihaiValentin/lunr-languages
*
* Copyright 2017, Keerati Thiwanruk
* http://www.mozilla.org/MPL/
*/
/*!
* based on
* Snowball JavaScript Library v0.3
* http://code.google.com/p/urim/
* http://snowball.tartarus.org/
*
* Copyright 2010, Oleg Mazko
* http://www.mozilla.org/MPL/
*/
/**
* export the module via AMD, CommonJS or as a browser global
* Export code from https://github.com/umdjs/umd/blob/master/returnExports.js
*/
2021-02-18 22:21:36 +01:00
(function (root, factory) {
if (typeof define === "function" && define.amd) {
2021-02-18 21:44:01 +01:00
// AMD. Register as an anonymous module.
2021-02-18 22:21:36 +01:00
define(factory);
} else if (typeof exports === "object") {
2021-02-18 21:44:01 +01:00
/**
* Node. Does not work with strict CommonJS, but
* only CommonJS-like environments that support module.exports,
* like Node.
*/
2021-02-18 22:21:36 +01:00
module.exports = factory();
2021-02-18 21:44:01 +01:00
} else {
// Browser globals (root is window)
factory()(root.lunr);
}
2021-02-18 22:21:36 +01:00
})(this, function () {
2021-02-18 21:44:01 +01:00
/**
* 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.
*/
2021-02-18 22:21:36 +01:00
return function (lunr) {
2021-02-18 21:44:01 +01:00
/* throw error if lunr is not yet included */
2021-02-18 22:21:36 +01:00
if ("undefined" === typeof lunr) {
throw new Error(
"Lunr is not present. Please include / require Lunr before this script."
);
2021-02-18 21:44:01 +01:00
}
/* throw error if lunr stemmer support is not yet included */
2021-02-18 22:21:36 +01:00
if ("undefined" === typeof lunr.stemmerSupport) {
throw new Error(
"Lunr stemmer support is not present. Please include / require Lunr stemmer support before this script."
);
2021-02-18 21:44:01 +01:00
}
/*
Thai tokenization is the same to Japanense, which does not take into account spaces.
So, it uses the same logic to assign tokenization function due to different Lunr versions.
*/
var isLunr2 = lunr.version[0] == "2";
/* register specific locale function */
2021-02-18 22:21:36 +01:00
lunr.th = function () {
this.pipeline.reset();
this.pipeline.add(
/*lunr.th.stopWordFilter,*/
lunr.th.trimmer
);
2021-02-18 21:44:01 +01:00
2021-02-18 22:21:36 +01:00
if (isLunr2) {
// for lunr version 2.0.0
this.tokenizer = lunr.th.tokenizer;
} else {
if (lunr.tokenizer) {
// for lunr version 0.6.0
lunr.tokenizer = lunr.th.tokenizer;
2021-02-18 21:44:01 +01:00
}
2021-02-18 22:21:36 +01:00
if (this.tokenizerFn) {
// for lunr version 0.7.0 -> 1.0.0
this.tokenizerFn = lunr.th.tokenizer;
}
}
2021-02-18 21:44:01 +01:00
};
/* lunr trimmer function */
lunr.th.wordCharacters = "[\u0e00-\u0e7f]";
2021-02-18 22:21:36 +01:00
lunr.th.trimmer = lunr.trimmerSupport.generateTrimmer(
lunr.th.wordCharacters
);
lunr.Pipeline.registerFunction(lunr.th.trimmer, "trimmer-th");
2021-02-18 21:44:01 +01:00
var segmenter = lunr.wordcut;
segmenter.init();
lunr.th.tokenizer = function (obj) {
//console.log(obj);
2021-02-18 22:21:36 +01:00
if (!arguments.length || obj == null || obj == undefined) return [];
if (Array.isArray(obj))
return obj.map(function (t) {
return isLunr2 ? new lunr.Token(t) : t;
});
2021-02-18 21:44:01 +01:00
2021-02-18 22:21:36 +01:00
var str = obj.toString().replace(/^\s+/, "");
return segmenter.cut(str).split("|");
};
2021-02-18 21:44:01 +01:00
};
2021-02-18 22:21:36 +01:00
});