Detect highlights

This commit is contained in:
Simon Ser
2020-06-29 11:08:47 +02:00
parent ed6dccbb58
commit abece1e3fd
4 changed files with 55 additions and 1 deletions

View File

@@ -196,3 +196,47 @@ export function parseMembership(s) {
nick: s.slice(i),
};
}
const alphaNum = (() => {
try {
return new RegExp(/^\p{L}$/, "u");
} catch (e) {
return new RegExp(/^[a-zA-Z0-9]$/, "u");
}
})();
function isWordBoundary(ch) {
switch (ch) {
case "-":
case "_":
case "|":
return false;
case "\u00A0":
return true;
default:
return !alphaNum.test(ch);
}
}
export function isHighlight(text, nick) {
while (true) {
var i = text.indexOf(nick);
if (i < 0) {
return false;
}
// Detect word boundaries
var left = "\x00", right = "\x00";
if (i > 0) {
left = text[i - 1];
}
if (i < text.length) {
right = text[i + nick.length];
}
if (isWordBoundary(left) && isWordBoundary(right)) {
return true;
}
text = text.slice(i + nick.length);
}
}