Skip to content

Commit

Permalink
Treat normal markdown links as a single token for progressive chat re…
Browse files Browse the repository at this point in the history
…ndering (#213091)

Fixes our progressive chat renderer breaking up any markdown links that contain `-` or a space character. Doesn't support all the fancy markdown link syntaxes, just basic links for now
  • Loading branch information
mjbvz authored May 21, 2024
1 parent 3bda9ff commit 0e91358
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
27 changes: 9 additions & 18 deletions src/vs/workbench/contrib/chat/common/chatWordCounter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,27 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

const wordSeparatorCharPattern = /[\s\|\-]/;

export interface IWordCountResult {
value: string;
actualWordCount: number;
isFullString: boolean;
}

export function getNWords(str: string, numWordsToCount: number): IWordCountResult {
let wordCount = numWordsToCount;
let i = 0;
while (i < str.length && wordCount > 0) {
// Consume word separator chars
while (i < str.length && str[i].match(wordSeparatorCharPattern)) {
i++;
}
// Match words and markdown style links
const allWordMatches = Array.from(str.matchAll(/\[([^\]]+)\]\(([^)]+)\)|[^\s\|\-]+/g));

// Consume word chars
while (i < str.length && !str[i].match(wordSeparatorCharPattern)) {
i++;
}
const targetWords = allWordMatches.slice(0, numWordsToCount);

wordCount--;
}
const endIndex = numWordsToCount > allWordMatches.length
? str.length // Reached end of string
: targetWords.length ? targetWords.at(-1)!.index + targetWords.at(-1)![0].length : 0;

const value = str.substring(0, i);
const value = str.substring(0, endIndex);
return {
value,
actualWordCount: numWordsToCount - wordCount,
isFullString: i >= str.length
actualWordCount: targetWords.length === 0 ? (value.length ? 1 : 0) : targetWords.length,
isFullString: endIndex >= str.length
};
}

Expand Down
11 changes: 11 additions & 0 deletions src/vs/workbench/contrib/chat/test/common/chatWordCounter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,15 @@ suite('ChatWordCounter', () => {

cases.forEach(([str, nWords, result]) => doTest(str, nWords, result));
});

test('getNWords, matching links', () => {
const cases: [string, number, string][] = [
['[hello](https://example.com) world', 1, '[hello](https://example.com)'],
['[hello](https://example.com) world', 2, '[hello](https://example.com) world'],
['oh [hello](https://example.com "title") world', 1, 'oh'],
['oh [hello](https://example.com "title") world', 2, 'oh [hello](https://example.com "title")'],
];

cases.forEach(([str, nWords, result]) => doTest(str, nWords, result));
});
});

0 comments on commit 0e91358

Please sign in to comment.