zunda 449aa77337 [Glitch] Accept non-Latin characters to suggest hash tag
Port 72085f7d5288692f85e411916d3f0f18a3603574 to glitch-soc

Signed-off-by: Claire <claire.github-309c@sitedethib.com>
2026-08-14 07:27:21 -04:00

35 lines
735 B
TypeScript

import { WORD } from '../../utils/hashtags';
export const textAtCursorMatchesToken = (
str: string,
caretPosition: number,
searchTokens: string[],
) => {
let word: string;
const regex = new RegExp(
`[${searchTokens.join('')}${WORD}]+(\\s[${WORD}]+)?$`,
'iu',
);
const left = str.slice(0, caretPosition).search(regex);
const right = str.slice(caretPosition).search(/\s/);
if (right < 0) {
word = str.slice(left);
} else {
word = str.slice(left, right + caretPosition);
}
word = word.trim();
if (word.length < 3 || (word[0] && !searchTokens.includes(word[0]))) {
return [null, null];
}
if (word.length > 0) {
return [left + 1, word];
} else {
return [null, null];
}
};