1
>
/*---------------------------------------------------------------------------------------------
chatWordCounter.ts
2
>
* Copyright (c) Microsoft Corporation. All rights reserved.
3
>
* Licensed under the MIT License. See License.txt in the project root for license information.
4
>
*--------------------------------------------------------------------------------------------*/
5
>
6
>
import * as markedKatexExtension from '../../../markdown/common/markedKatexExtension.js';
7
>
8
>
export interface IWordCountResult {
9
>
value: string;
10
>
returnedWordCount: number;
11
>
totalWordCount: number;
12
>
isFullString: boolean;
13
>
}
14
>
15
>
const r = String.raw;
16
>
17
>
/**
18
>
* Matches `[text](link title?)` or `[text](<link> title?)`
19
>
*
20
>
* Taken from vscode-markdown-languageservice
21
>
*/
22
>
const linkPattern =
23
>
r`(?<!\\)` + // Must not start with escape
24
>
25
>
// text
26
>
r`(!?\[` + // open prefix match -->
27
>
/**/r`(?:` +
28
>
/*****/r`[^\[\]\\]|` + // Non-bracket chars, or...
29
>
/*****/r`\\.|` + // Escaped char, or...
30
>
/*****/r`\[[^\[\]]*\]` + // Matched bracket pair
31
>
/**/r`)*` +
32
>
r`\])` + // <-- close prefix match
33
>
34
>
// Destination
35
>
r`(\(\s*)` + // Pre href
36
>
/**/r`(` +
37
>
/*****/r`[^\s\(\)<](?:[^\s\(\)]|\([^\s\(\)]*?\))*|` + // Link without whitespace, or...
38
>
/*****/r`<(?:\\[<>]|[^<>])+>` + // In angle brackets
39
>
/**/r`)` +
40
>
41
>
// Title
42
>
/**/r`\s*(?:"[^"]*"|'[^']*'|\([^\(\)]*\))?\s*` +
43
>
r`\)`;
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:
47
// A markdown link