Detect highlights
This commit is contained in:
44
lib/irc.js
44
lib/irc.js
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user