Skip to content

Commit 85ba9f9

Browse files
Copilotalexr00
andcommitted
Add optional simple markdown support to PlainTextRenderer for inline code
Co-authored-by: alexr00 <38270282+alexr00@users.noreply.github.com>
1 parent 9e041a0 commit 85ba9f9

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

src/github/copilotRemoteAgent/chatSessionContentBuilder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export class ChatSessionContentBuilder {
7676
// if this is the first response, then also add the PR card
7777
if (sessionIndex === 0) {
7878
const uri = await toOpenPullRequestWebviewUri({ owner: pullRequest.remote.owner, repo: pullRequest.remote.repositoryName, pullRequestNumber: pullRequest.number });
79-
const plaintextBody = marked.parse(pullRequest.body, { renderer: new PlainTextRenderer(), smartypants: true }).trim();
79+
const plaintextBody = marked.parse(pullRequest.body, { renderer: new PlainTextRenderer(true), smartypants: true }).trim();
8080

8181
const card = new vscode.ChatResponsePullRequestPart(uri, pullRequest.title, plaintextBody, pullRequest.author.specialDisplayName ?? pullRequest.author.login, `#${pullRequest.number}`);
8282
const cardTurn = new vscode.ChatResponseTurn2([card], {}, COPILOT_SWE_AGENT);

src/github/markdownUtils.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,13 @@ export async function issueMarkdown(
237237
}
238238

239239
export class PlainTextRenderer extends marked.Renderer {
240+
private allowSimpleMarkdown: boolean;
241+
242+
constructor(allowSimpleMarkdown: boolean = false) {
243+
super();
244+
this.allowSimpleMarkdown = allowSimpleMarkdown;
245+
}
246+
240247
override code(code: string, _infostring: string | undefined): string {
241248
return code;
242249
}
@@ -286,6 +293,9 @@ export class PlainTextRenderer extends marked.Renderer {
286293
return text;
287294
}
288295
override codespan(code: string): string {
296+
if (this.allowSimpleMarkdown) {
297+
return `\`${code}\``;
298+
}
289299
return `\\\`${code}\\\``;
290300
}
291301
override br(): string {
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*---------------------------------------------------------------------------------------------
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 assert from 'assert';
7+
import * as marked from 'marked';
8+
import { PlainTextRenderer } from '../../github/markdownUtils';
9+
10+
suite('PlainTextRenderer', () => {
11+
test('should escape inline code by default', () => {
12+
const renderer = new PlainTextRenderer();
13+
const result = marked.parse('rename the `Foo` class', { renderer, smartypants: true });
14+
assert.strictEqual(result.trim(), 'rename the \\`Foo\\` class');
15+
});
16+
17+
test('should preserve inline code when allowSimpleMarkdown is true', () => {
18+
const renderer = new PlainTextRenderer(true);
19+
const result = marked.parse('rename the `Foo` class', { renderer, smartypants: true });
20+
assert.strictEqual(result.trim(), 'rename the `Foo` class');
21+
});
22+
23+
test('should handle multiple inline code spans', () => {
24+
const renderer = new PlainTextRenderer(true);
25+
const result = marked.parse('rename the `Foo` class to `Bar`', { renderer, smartypants: true });
26+
assert.strictEqual(result.trim(), 'rename the `Foo` class to `Bar`');
27+
});
28+
29+
test('should still escape when allowSimpleMarkdown is false', () => {
30+
const renderer = new PlainTextRenderer(false);
31+
const result = marked.parse('rename the `Foo` class to `Bar`', { renderer, smartypants: true });
32+
assert.strictEqual(result.trim(), 'rename the \\`Foo\\` class to \\`Bar\\`');
33+
});
34+
});

0 commit comments

Comments
 (0)