44
45
export function getNWords(str: string, numWordsToCount: number): IWordCountResult {
46
>
// This regex matches each word and skips over whitespace and separators. A word is:
chatWordCounter.ts
47
>
// A markdown link
48
>
// Inline math
49
>
// One chinese character
50
>
// One or more + - =, handled so that code like "a=1+2-3" is broken up better
51
>
// One or more characters that aren't whitepace or any of the above
52
>
const backtick = '`';
53
>
54
>
const wordRegExp = new RegExp('(?:' + linkPattern + ')|(?:' + markedKatexExtension.mathInlineRegExp.source + r`)|\p{sc=Han}|=+|\++|-+|[^\s\|\p{sc=Han}|=|\+|\-|${backtick}]+`, 'gu');
55
>
const allWordMatches = Array.from(str.matchAll(wordRegExp));
56
>
57
>
const targetWords = allWordMatches.slice(0, numWordsToCount);
58
>
59
>
const endIndex = numWordsToCount >= allWordMatches.length
60
? str.length // Reached end of string
61
: targetWords.length ? targetWords.at(-1)!.index + targetWords.at(-1)![0].length : 0;
63
>
const value = str.substring(0, endIndex);
64
>
return {
65
>
value,
66
>
returnedWordCount: targetWords.length === 0 ? (value.length ? 1 : 0) : targetWords.length,
67
>
isFullString: endIndex >= str.length,
68
>
totalWordCount: allWordMatches.length
69
>
};
70
>
}
71
72
export function countWords(str: string): number {