diff --git a/extensions/markdown-language-features/src/languageFeatures/copyFiles/shared.ts b/extensions/markdown-language-features/src/languageFeatures/copyFiles/shared.ts index 082f569b0b1..895dd6a3538 100644 --- a/extensions/markdown-language-features/src/languageFeatures/copyFiles/shared.ts +++ b/extensions/markdown-language-features/src/languageFeatures/copyFiles/shared.ts @@ -214,9 +214,9 @@ export function appendToLinkSnippet( if (pasteAsMarkdownLink) { snippet.appendText('['); snippet.appendPlaceholder(escapeBrackets(title) || 'Title', placeholderValue); - snippet.appendText(isExternalLink ? `](${uriString})` : `](${escapeMarkdownLinkPath(mdPath)})`); + snippet.appendText(`](${escapeMarkdownLinkPath(isExternalLink ? uriString : mdPath, isExternalLink)})`); } else { - snippet.appendText(isExternalLink ? uriString : escapeMarkdownLinkPath(mdPath)); + snippet.appendText((escapeMarkdownLinkPath(isExternalLink ? uriString : mdPath, isExternalLink))); } return snippet; } @@ -268,9 +268,9 @@ export function createUriListSnippet( const placeholderText = escapeBrackets(title) || options?.placeholderText || 'Alt text'; const placeholderIndex = typeof options?.placeholderStartIndex !== 'undefined' ? options?.placeholderStartIndex + i : (placeholderValue === 0 ? undefined : placeholderValue); snippet.appendPlaceholder(placeholderText, placeholderIndex); - snippet.appendText(`](${escapeMarkdownLinkPath(mdPath)})`); + snippet.appendText(`](${escapeMarkdownLinkPath(mdPath, isExternalLink)})`); } else { - snippet.appendText(escapeMarkdownLinkPath(mdPath)); + snippet.appendText(escapeMarkdownLinkPath(mdPath, isExternalLink)); } } } else { @@ -397,12 +397,12 @@ function escapeHtmlAttribute(attr: string): string { return encodeURI(attr).replaceAll('"', '"'); } -function escapeMarkdownLinkPath(mdPath: string): string { +function escapeMarkdownLinkPath(mdPath: string, isExternalLink: boolean): string { if (needsBracketLink(mdPath)) { return '<' + mdPath.replaceAll('<', '\\<').replaceAll('>', '\\>') + '>'; } - return encodeURI(mdPath); + return isExternalLink ? mdPath : encodeURI(mdPath); } function escapeBrackets(value: string): string { diff --git a/extensions/markdown-language-features/src/test/markdownLink.test.ts b/extensions/markdown-language-features/src/test/markdownLink.test.ts index ea39cba1bdb..13d56fc5306 100644 --- a/extensions/markdown-language-features/src/test/markdownLink.test.ts +++ b/extensions/markdown-language-features/src/test/markdownLink.test.ts @@ -102,6 +102,18 @@ suite('createEditAddingLinksForUriList', () => { }); suite('appendToLinkSnippet', () => { + test('Should create auto link when pasted link has an mismatched parentheses', () => { + const uriString = 'https://www.mic(rosoft.com'; + const snippet = appendToLinkSnippet(new vscode.SnippetString(''), false, 'https:/www.microsoft.com', '', uriString, 0, true); + assert.strictEqual(snippet?.value, ''); + }); + + test('Should create snippet with < > when pasted link has an mismatched parentheses', () => { + const uriString = 'https://www.mic(rosoft.com'; + const snippet = appendToLinkSnippet(new vscode.SnippetString(''), true, 'https:/www.microsoft.com', 'abc', uriString, 0, true); + assert.strictEqual(snippet?.value, '[${0:abc}]()'); + }); + test('Should not create Markdown link snippet when pasteAsMarkdownLink is false', () => { const uriString = 'https://www.microsoft.com'; const snippet = appendToLinkSnippet(new vscode.SnippetString(''), false, 'https:/www.microsoft.com', '', uriString, 0, true);