Skip to content

Commit 1fcc384

Browse files
Automatic markdown pasting should escape parentheses (microsoft#189357)
auto link with mismatched parens
1 parent c75dd82 commit 1fcc384

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

extensions/markdown-language-features/src/languageFeatures/copyFiles/shared.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,9 @@ export function appendToLinkSnippet(
214214
if (pasteAsMarkdownLink) {
215215
snippet.appendText('[');
216216
snippet.appendPlaceholder(escapeBrackets(title) || 'Title', placeholderValue);
217-
snippet.appendText(isExternalLink ? `](${uriString})` : `](${escapeMarkdownLinkPath(mdPath)})`);
217+
snippet.appendText(`](${escapeMarkdownLinkPath(isExternalLink ? uriString : mdPath, isExternalLink)})`);
218218
} else {
219-
snippet.appendText(isExternalLink ? uriString : escapeMarkdownLinkPath(mdPath));
219+
snippet.appendText((escapeMarkdownLinkPath(isExternalLink ? uriString : mdPath, isExternalLink)));
220220
}
221221
return snippet;
222222
}
@@ -268,9 +268,9 @@ export function createUriListSnippet(
268268
const placeholderText = escapeBrackets(title) || options?.placeholderText || 'Alt text';
269269
const placeholderIndex = typeof options?.placeholderStartIndex !== 'undefined' ? options?.placeholderStartIndex + i : (placeholderValue === 0 ? undefined : placeholderValue);
270270
snippet.appendPlaceholder(placeholderText, placeholderIndex);
271-
snippet.appendText(`](${escapeMarkdownLinkPath(mdPath)})`);
271+
snippet.appendText(`](${escapeMarkdownLinkPath(mdPath, isExternalLink)})`);
272272
} else {
273-
snippet.appendText(escapeMarkdownLinkPath(mdPath));
273+
snippet.appendText(escapeMarkdownLinkPath(mdPath, isExternalLink));
274274
}
275275
}
276276
} else {
@@ -397,12 +397,12 @@ function escapeHtmlAttribute(attr: string): string {
397397
return encodeURI(attr).replaceAll('"', '"');
398398
}
399399

400-
function escapeMarkdownLinkPath(mdPath: string): string {
400+
function escapeMarkdownLinkPath(mdPath: string, isExternalLink: boolean): string {
401401
if (needsBracketLink(mdPath)) {
402402
return '<' + mdPath.replaceAll('<', '\\<').replaceAll('>', '\\>') + '>';
403403
}
404404

405-
return encodeURI(mdPath);
405+
return isExternalLink ? mdPath : encodeURI(mdPath);
406406
}
407407

408408
function escapeBrackets(value: string): string {

extensions/markdown-language-features/src/test/markdownLink.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,18 @@ suite('createEditAddingLinksForUriList', () => {
102102
});
103103

104104
suite('appendToLinkSnippet', () => {
105+
test('Should create auto link when pasted link has an mismatched parentheses', () => {
106+
const uriString = 'https://www.mic(rosoft.com';
107+
const snippet = appendToLinkSnippet(new vscode.SnippetString(''), false, 'https:/www.microsoft.com', '', uriString, 0, true);
108+
assert.strictEqual(snippet?.value, '<https://www.mic(rosoft.com>');
109+
});
110+
111+
test('Should create snippet with < > when pasted link has an mismatched parentheses', () => {
112+
const uriString = 'https://www.mic(rosoft.com';
113+
const snippet = appendToLinkSnippet(new vscode.SnippetString(''), true, 'https:/www.microsoft.com', 'abc', uriString, 0, true);
114+
assert.strictEqual(snippet?.value, '[${0:abc}](<https://www.mic(rosoft.com>)');
115+
});
116+
105117
test('Should not create Markdown link snippet when pasteAsMarkdownLink is false', () => {
106118
const uriString = 'https://www.microsoft.com';
107119
const snippet = appendToLinkSnippet(new vscode.SnippetString(''), false, 'https:/www.microsoft.com', '', uriString, 0, true);

0 commit comments

Comments
 (0)