Skip to content

Commit 0e15fee

Browse files
Fixed markdown regular expression (microsoft#189423)
* fixed md regex * update shared.ts
1 parent ca5cfcc commit 0e15fee

File tree

2 files changed

+10
-12
lines changed

2 files changed

+10
-12
lines changed

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

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,7 @@ export const mediaMimes = new Set([
6363
]);
6464

6565
const smartPasteRegexes = [
66-
{ regex: /\[.*\]\(.*\)/g, isMarkdownLink: true, isInline: true }, // Is a Markdown Link
67-
{ regex: /!\[.*\]\(.*\)/g, isMarkdownLink: true, isInline: true }, // Is a Markdown Image Link
68-
{ regex: /\[([^\]]*)\]\(([^)]*)\)/g, isMarkdownLink: false, isInline: true }, // In a Markdown link
66+
{ regex: /(\[[^\[\]]*](?:\([^\(\)]*\)|\[[^\[\]]*]))/g, isMarkdownLink: false, isInline: true }, // In a Markdown link
6967
{ regex: /^```[\s\S]*?```$/gm, isMarkdownLink: false, isInline: false }, // In a backtick fenced code block
7068
{ regex: /^~~~[\s\S]*?~~~$/gm, isMarkdownLink: false, isInline: false }, // In a tildefenced code block
7169
{ regex: /^\$\$[\s\S]*?\$\$$/gm, isMarkdownLink: false, isInline: false }, // In a fenced math block
@@ -79,15 +77,6 @@ export interface SkinnyTextDocument {
7977
readonly uri: vscode.Uri;
8078
}
8179

82-
export interface SmartPaste {
83-
84-
/**
85-
* `true` if the link is not being pasted within a markdown link, code, or math.
86-
*/
87-
pasteAsMarkdownLink: boolean;
88-
89-
}
90-
9180
export enum PasteUrlAsFormattedLink {
9281
Always = 'always',
9382
Smart = 'smart',
@@ -146,6 +135,9 @@ export function checkSmartPaste(document: SkinnyTextDocument, selectedRange: vsc
146135
if (selectedRange.isEmpty || /^[\s\n]*$/.test(document.getText(range)) || validateLink(document.getText(range)).isValid) {
147136
return false;
148137
}
138+
if (/\[.*\]\(.*\)/.test(document.getText(range)) || /!\[.*\]\(.*\)/.test(document.getText(range))) {
139+
return false;
140+
}
149141
for (const regex of smartPasteRegexes) {
150142
const matches = [...document.getText().matchAll(regex.regex)];
151143
for (const match of matches) {

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,12 @@ suite('createEditAddingLinksForUriList', () => {
170170
assert.strictEqual(pasteAsMarkdownLink, false);
171171
});
172172

173+
test('Should evaluate pasteAsMarkdownLink as true for a link pasted in square brackets', () => {
174+
skinnyDocument.getText = function () { return '[abc]'; };
175+
const pasteAsMarkdownLink = checkSmartPaste(skinnyDocument, new vscode.Range(0, 1, 0, 4), new vscode.Range(0, 1, 0, 4));
176+
assert.strictEqual(pasteAsMarkdownLink, true);
177+
});
178+
173179
test('Should evaluate pasteAsMarkdownLink as false for no selection', () => {
174180
const pasteAsMarkdownLink = checkSmartPaste(skinnyDocument, new vscode.Range(0, 0, 0, 0), new vscode.Range(0, 0, 0, 0));
175181
assert.strictEqual(pasteAsMarkdownLink, false);

0 commit comments

Comments
 (0)