Skip to content

Commit

Permalink
Implement ignorePunctuation option (#135)
Browse files Browse the repository at this point in the history
Closes ##119
  • Loading branch information
Mottie authored and julkue committed Jun 20, 2017
1 parent a808cc5 commit 4986870
Show file tree
Hide file tree
Showing 34 changed files with 790 additions and 66 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mark.js",
"version": "8.10.1",
"version": "8.11.0",
"keywords": [
"mark.js",
"markjs",
Expand Down
33 changes: 25 additions & 8 deletions dist/jquery.mark.es6.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!***************************************************
* mark.js v8.10.1
* mark.js v8.11.0
* https://github.com/julmot/mark.js
* Copyright (c) 2014–2017, Julian Motz
* Released under the MIT license https://git.io/vwTVl
Expand Down Expand Up @@ -44,6 +44,7 @@
"caseSensitive": false,
"ignoreJoiners": false,
"ignoreGroups": 0,
"ignorePunctuation": [],
"wildcards": "disabled",
"each": () => {},
"noMatch": () => {},
Expand Down Expand Up @@ -84,15 +85,15 @@
if (Object.keys(this.opt.synonyms).length) {
str = this.createSynonymsRegExp(str);
}
if (this.opt.ignoreJoiners) {
if (this.opt.ignoreJoiners || this.opt.ignorePunctuation.length) {
str = this.setupIgnoreJoinersRegExp(str);
}
if (this.opt.diacritics) {
str = this.createDiacriticsRegExp(str);
}
str = this.createMergedBlanksRegExp(str);
if (this.opt.ignoreJoiners) {
str = this.createIgnoreJoinersRegExp(str);
if (this.opt.ignoreJoiners || this.opt.ignorePunctuation.length) {
str = this.createJoinersRegExp(str);
}
if (this.opt.wildcards !== "disabled") {
str = this.createWildcardsRegExp(str);
Expand All @@ -103,20 +104,28 @@

createSynonymsRegExp(str) {
const syn = this.opt.synonyms,
sens = this.opt.caseSensitive ? "" : "i";
sens = this.opt.caseSensitive ? "" : "i",
joinerPlaceholder = this.opt.ignoreJoiners || this.opt.ignorePunctuation.length ? "\u0000" : "";
for (let index in syn) {
if (syn.hasOwnProperty(index)) {
const value = syn[index],
k1 = this.opt.wildcards !== "disabled" ? this.setupWildcardsRegExp(index) : this.escapeStr(index),
k2 = this.opt.wildcards !== "disabled" ? this.setupWildcardsRegExp(value) : this.escapeStr(value);
if (k1 !== "" && k2 !== "") {
str = str.replace(new RegExp(`(${k1}|${k2})`, `gm${sens}`), `(${k1}|${k2})`);
str = str.replace(new RegExp(`(${k1}|${k2})`, `gm${sens}`), joinerPlaceholder + `(${this.processSynomyms(k1)}|` + `${this.processSynomyms(k2)})` + joinerPlaceholder);
}
}
}
return str;
}

processSynomyms(str) {
if (this.opt.ignoreJoiners || this.opt.ignorePunctuation.length) {
str = this.setupIgnoreJoinersRegExp(str);
}
return str;
}

setupWildcardsRegExp(str) {
str = str.replace(/(?:\\)*\?/g, val => {
return val.charAt(0) === "\\" ? "?" : "\u0001";
Expand All @@ -143,8 +152,16 @@
});
}

createIgnoreJoinersRegExp(str) {
return str.split("\u0000").join("[\\u00ad|\\u200b|\\u200c|\\u200d]?");
createJoinersRegExp(str) {
let joiner = [];
const ignorePunctuation = this.opt.ignorePunctuation;
if (Array.isArray(ignorePunctuation) && ignorePunctuation.length) {
joiner.push(this.escapeStr(ignorePunctuation.join("")));
}
if (this.opt.ignoreJoiners) {
joiner.push("\\u00ad\\u200b\\u200c\\u200d");
}
return joiner.length ? str.split(/\u0000+/).join(`[${joiner.join("")}]*`) : str;
}

createDiacriticsRegExp(str) {
Expand Down
4 changes: 2 additions & 2 deletions dist/jquery.mark.es6.min.js

Large diffs are not rendered by default.

36 changes: 27 additions & 9 deletions dist/jquery.mark.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!***************************************************
* mark.js v8.10.1
* mark.js v8.11.0
* https://github.com/julmot/mark.js
* Copyright (c) 2014–2017, Julian Motz
* Released under the MIT license https://git.io/vwTVl
Expand Down Expand Up @@ -67,15 +67,15 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
if (Object.keys(this.opt.synonyms).length) {
str = this.createSynonymsRegExp(str);
}
if (this.opt.ignoreJoiners) {
if (this.opt.ignoreJoiners || this.opt.ignorePunctuation.length) {
str = this.setupIgnoreJoinersRegExp(str);
}
if (this.opt.diacritics) {
str = this.createDiacriticsRegExp(str);
}
str = this.createMergedBlanksRegExp(str);
if (this.opt.ignoreJoiners) {
str = this.createIgnoreJoinersRegExp(str);
if (this.opt.ignoreJoiners || this.opt.ignorePunctuation.length) {
str = this.createJoinersRegExp(str);
}
if (this.opt.wildcards !== "disabled") {
str = this.createWildcardsRegExp(str);
Expand All @@ -87,19 +87,28 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
key: "createSynonymsRegExp",
value: function createSynonymsRegExp(str) {
var syn = this.opt.synonyms,
sens = this.opt.caseSensitive ? "" : "i";
sens = this.opt.caseSensitive ? "" : "i",
joinerPlaceholder = this.opt.ignoreJoiners || this.opt.ignorePunctuation.length ? "\0" : "";
for (var index in syn) {
if (syn.hasOwnProperty(index)) {
var value = syn[index],
k1 = this.opt.wildcards !== "disabled" ? this.setupWildcardsRegExp(index) : this.escapeStr(index),
k2 = this.opt.wildcards !== "disabled" ? this.setupWildcardsRegExp(value) : this.escapeStr(value);
if (k1 !== "" && k2 !== "") {
str = str.replace(new RegExp("(" + k1 + "|" + k2 + ")", "gm" + sens), "(" + k1 + "|" + k2 + ")");
str = str.replace(new RegExp("(" + k1 + "|" + k2 + ")", "gm" + sens), joinerPlaceholder + ("(" + this.processSynomyms(k1) + "|") + (this.processSynomyms(k2) + ")") + joinerPlaceholder);
}
}
}
return str;
}
}, {
key: "processSynomyms",
value: function processSynomyms(str) {
if (this.opt.ignoreJoiners || this.opt.ignorePunctuation.length) {
str = this.setupIgnoreJoinersRegExp(str);
}
return str;
}
}, {
key: "setupWildcardsRegExp",
value: function setupWildcardsRegExp(str) {
Expand Down Expand Up @@ -130,9 +139,17 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
});
}
}, {
key: "createIgnoreJoinersRegExp",
value: function createIgnoreJoinersRegExp(str) {
return str.split("\0").join("[\\u00ad|\\u200b|\\u200c|\\u200d]?");
key: "createJoinersRegExp",
value: function createJoinersRegExp(str) {
var joiner = [];
var ignorePunctuation = this.opt.ignorePunctuation;
if (Array.isArray(ignorePunctuation) && ignorePunctuation.length) {
joiner.push(this.escapeStr(ignorePunctuation.join("")));
}
if (this.opt.ignoreJoiners) {
joiner.push("\\u00ad\\u200b\\u200c\\u200d");
}
return joiner.length ? str.split(/\u0000+/).join("[" + joiner.join("") + "]*") : str;
}
}, {
key: "createDiacriticsRegExp",
Expand Down Expand Up @@ -628,6 +645,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
"caseSensitive": false,
"ignoreJoiners": false,
"ignoreGroups": 0,
"ignorePunctuation": [],
"wildcards": "disabled",
"each": function each() {},
"noMatch": function noMatch() {},
Expand Down
4 changes: 2 additions & 2 deletions dist/jquery.mark.min.js

Large diffs are not rendered by default.

33 changes: 25 additions & 8 deletions dist/mark.es6.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!***************************************************
* mark.js v8.10.1
* mark.js v8.11.0
* https://github.com/julmot/mark.js
* Copyright (c) 2014–2017, Julian Motz
* Released under the MIT license https://git.io/vwTVl
Expand Down Expand Up @@ -44,6 +44,7 @@
"caseSensitive": false,
"ignoreJoiners": false,
"ignoreGroups": 0,
"ignorePunctuation": [],
"wildcards": "disabled",
"each": () => {},
"noMatch": () => {},
Expand Down Expand Up @@ -84,15 +85,15 @@
if (Object.keys(this.opt.synonyms).length) {
str = this.createSynonymsRegExp(str);
}
if (this.opt.ignoreJoiners) {
if (this.opt.ignoreJoiners || this.opt.ignorePunctuation.length) {
str = this.setupIgnoreJoinersRegExp(str);
}
if (this.opt.diacritics) {
str = this.createDiacriticsRegExp(str);
}
str = this.createMergedBlanksRegExp(str);
if (this.opt.ignoreJoiners) {
str = this.createIgnoreJoinersRegExp(str);
if (this.opt.ignoreJoiners || this.opt.ignorePunctuation.length) {
str = this.createJoinersRegExp(str);
}
if (this.opt.wildcards !== "disabled") {
str = this.createWildcardsRegExp(str);
Expand All @@ -103,20 +104,28 @@

createSynonymsRegExp(str) {
const syn = this.opt.synonyms,
sens = this.opt.caseSensitive ? "" : "i";
sens = this.opt.caseSensitive ? "" : "i",
joinerPlaceholder = this.opt.ignoreJoiners || this.opt.ignorePunctuation.length ? "\u0000" : "";
for (let index in syn) {
if (syn.hasOwnProperty(index)) {
const value = syn[index],
k1 = this.opt.wildcards !== "disabled" ? this.setupWildcardsRegExp(index) : this.escapeStr(index),
k2 = this.opt.wildcards !== "disabled" ? this.setupWildcardsRegExp(value) : this.escapeStr(value);
if (k1 !== "" && k2 !== "") {
str = str.replace(new RegExp(`(${k1}|${k2})`, `gm${sens}`), `(${k1}|${k2})`);
str = str.replace(new RegExp(`(${k1}|${k2})`, `gm${sens}`), joinerPlaceholder + `(${this.processSynomyms(k1)}|` + `${this.processSynomyms(k2)})` + joinerPlaceholder);
}
}
}
return str;
}

processSynomyms(str) {
if (this.opt.ignoreJoiners || this.opt.ignorePunctuation.length) {
str = this.setupIgnoreJoinersRegExp(str);
}
return str;
}

setupWildcardsRegExp(str) {
str = str.replace(/(?:\\)*\?/g, val => {
return val.charAt(0) === "\\" ? "?" : "\u0001";
Expand All @@ -143,8 +152,16 @@
});
}

createIgnoreJoinersRegExp(str) {
return str.split("\u0000").join("[\\u00ad|\\u200b|\\u200c|\\u200d]?");
createJoinersRegExp(str) {
let joiner = [];
const ignorePunctuation = this.opt.ignorePunctuation;
if (Array.isArray(ignorePunctuation) && ignorePunctuation.length) {
joiner.push(this.escapeStr(ignorePunctuation.join("")));
}
if (this.opt.ignoreJoiners) {
joiner.push("\\u00ad\\u200b\\u200c\\u200d");
}
return joiner.length ? str.split(/\u0000+/).join(`[${joiner.join("")}]*`) : str;
}

createDiacriticsRegExp(str) {
Expand Down
4 changes: 2 additions & 2 deletions dist/mark.es6.min.js

Large diffs are not rendered by default.

36 changes: 27 additions & 9 deletions dist/mark.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!***************************************************
* mark.js v8.10.1
* mark.js v8.11.0
* https://github.com/julmot/mark.js
* Copyright (c) 2014–2017, Julian Motz
* Released under the MIT license https://git.io/vwTVl
Expand Down Expand Up @@ -67,15 +67,15 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
if (Object.keys(this.opt.synonyms).length) {
str = this.createSynonymsRegExp(str);
}
if (this.opt.ignoreJoiners) {
if (this.opt.ignoreJoiners || this.opt.ignorePunctuation.length) {
str = this.setupIgnoreJoinersRegExp(str);
}
if (this.opt.diacritics) {
str = this.createDiacriticsRegExp(str);
}
str = this.createMergedBlanksRegExp(str);
if (this.opt.ignoreJoiners) {
str = this.createIgnoreJoinersRegExp(str);
if (this.opt.ignoreJoiners || this.opt.ignorePunctuation.length) {
str = this.createJoinersRegExp(str);
}
if (this.opt.wildcards !== "disabled") {
str = this.createWildcardsRegExp(str);
Expand All @@ -87,19 +87,28 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
key: "createSynonymsRegExp",
value: function createSynonymsRegExp(str) {
var syn = this.opt.synonyms,
sens = this.opt.caseSensitive ? "" : "i";
sens = this.opt.caseSensitive ? "" : "i",
joinerPlaceholder = this.opt.ignoreJoiners || this.opt.ignorePunctuation.length ? "\0" : "";
for (var index in syn) {
if (syn.hasOwnProperty(index)) {
var value = syn[index],
k1 = this.opt.wildcards !== "disabled" ? this.setupWildcardsRegExp(index) : this.escapeStr(index),
k2 = this.opt.wildcards !== "disabled" ? this.setupWildcardsRegExp(value) : this.escapeStr(value);
if (k1 !== "" && k2 !== "") {
str = str.replace(new RegExp("(" + k1 + "|" + k2 + ")", "gm" + sens), "(" + k1 + "|" + k2 + ")");
str = str.replace(new RegExp("(" + k1 + "|" + k2 + ")", "gm" + sens), joinerPlaceholder + ("(" + this.processSynomyms(k1) + "|") + (this.processSynomyms(k2) + ")") + joinerPlaceholder);
}
}
}
return str;
}
}, {
key: "processSynomyms",
value: function processSynomyms(str) {
if (this.opt.ignoreJoiners || this.opt.ignorePunctuation.length) {
str = this.setupIgnoreJoinersRegExp(str);
}
return str;
}
}, {
key: "setupWildcardsRegExp",
value: function setupWildcardsRegExp(str) {
Expand Down Expand Up @@ -130,9 +139,17 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
});
}
}, {
key: "createIgnoreJoinersRegExp",
value: function createIgnoreJoinersRegExp(str) {
return str.split("\0").join("[\\u00ad|\\u200b|\\u200c|\\u200d]?");
key: "createJoinersRegExp",
value: function createJoinersRegExp(str) {
var joiner = [];
var ignorePunctuation = this.opt.ignorePunctuation;
if (Array.isArray(ignorePunctuation) && ignorePunctuation.length) {
joiner.push(this.escapeStr(ignorePunctuation.join("")));
}
if (this.opt.ignoreJoiners) {
joiner.push("\\u00ad\\u200b\\u200c\\u200d");
}
return joiner.length ? str.split(/\u0000+/).join("[" + joiner.join("") + "]*") : str;
}
}, {
key: "createDiacriticsRegExp",
Expand Down Expand Up @@ -628,6 +645,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
"caseSensitive": false,
"ignoreJoiners": false,
"ignoreGroups": 0,
"ignorePunctuation": [],
"wildcards": "disabled",
"each": function each() {},
"noMatch": function noMatch() {},
Expand Down
4 changes: 2 additions & 2 deletions dist/mark.min.js

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion karma.conf-ci.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,12 @@ module.exports = config => {
"test/specs/basic/context-string.js",
"test/specs/basic/array-keyword.js",
"test/specs/basic/custom-element-class.js",
"test/specs/basic/!(accuracy|no-options|case-sensitive|ignore-joiners|wildcards)*.js",
"test/specs/basic/!(accuracy|no-options|case-sensitive|ignore-joiners|ignore-punctuation|wildcards)*.js",
// depends on diacritics, separateWordSearch or synonyms:
"test/specs/basic/accuracy*.js",
"test/specs/basic/case-sensitive*.js",
"test/specs/basic/ignore-joiners*.js",
"test/specs/basic/ignore-punctuation*.js",
"test/specs/basic/wildcards*.js",
"test/specs/iframes/main.js",
"test/specs/iframes/unmark.js",
Expand Down
3 changes: 2 additions & 1 deletion karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ module.exports = config => {
"test/specs/basic/context-string.js",
"test/specs/basic/array-keyword.js",
"test/specs/basic/custom-element-class.js",
"test/specs/basic/!(accuracy|no-options|case-sensitive|ignore-joiners|wildcards)*.js",
"test/specs/basic/!(accuracy|no-options|case-sensitive|ignore-joiners|ignore-punctuation|wildcards)*.js",
// depends on diacritics, separateWordSearch or synonyms:
"test/specs/basic/accuracy*.js",
"test/specs/basic/case-sensitive*.js",
"test/specs/basic/ignore-joiners*.js",
"test/specs/basic/ignore-punctuation*.js",
"test/specs/basic/wildcards*.js",
"test/specs/iframes/main.js",
"test/specs/iframes/unmark.js",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mark.js",
"version": "8.10.1",
"version": "8.11.0",
"keywords": [
"mark.js",
"markjs",
Expand Down

0 comments on commit 4986870

Please sign in to comment.