Fixed markdown regular expression (#189423)

* fixed md regex

* update shared.ts
This commit is contained in:
Meghan Kulkarni 2023-08-01 14:32:33 -07:00 committed by GitHub
parent ca5cfcca06
commit 0e15feeb95
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 12 deletions

View file

@ -63,9 +63,7 @@ export const mediaMimes = new Set([
]);
const smartPasteRegexes = [
{ regex: /\[.*\]\(.*\)/g, isMarkdownLink: true, isInline: true }, // Is a Markdown Link
{ regex: /!\[.*\]\(.*\)/g, isMarkdownLink: true, isInline: true }, // Is a Markdown Image Link
{ regex: /\[([^\]]*)\]\(([^)]*)\)/g, isMarkdownLink: false, isInline: true }, // In a Markdown link
{ regex: /(\[[^\[\]]*](?:\([^\(\)]*\)|\[[^\[\]]*]))/g, isMarkdownLink: false, isInline: true }, // In a Markdown link
{ regex: /^```[\s\S]*?```$/gm, isMarkdownLink: false, isInline: false }, // In a backtick fenced code block
{ regex: /^~~~[\s\S]*?~~~$/gm, isMarkdownLink: false, isInline: false }, // In a tildefenced code block
{ regex: /^\$\$[\s\S]*?\$\$$/gm, isMarkdownLink: false, isInline: false }, // In a fenced math block
@ -79,15 +77,6 @@ export interface SkinnyTextDocument {
readonly uri: vscode.Uri;
}
export interface SmartPaste {
/**
* `true` if the link is not being pasted within a markdown link, code, or math.
*/
pasteAsMarkdownLink: boolean;
}
export enum PasteUrlAsFormattedLink {
Always = 'always',
Smart = 'smart',
@ -146,6 +135,9 @@ export function checkSmartPaste(document: SkinnyTextDocument, selectedRange: vsc
if (selectedRange.isEmpty || /^[\s\n]*$/.test(document.getText(range)) || validateLink(document.getText(range)).isValid) {
return false;
}
if (/\[.*\]\(.*\)/.test(document.getText(range)) || /!\[.*\]\(.*\)/.test(document.getText(range))) {
return false;
}
for (const regex of smartPasteRegexes) {
const matches = [...document.getText().matchAll(regex.regex)];
for (const match of matches) {

View file

@ -170,6 +170,12 @@ suite('createEditAddingLinksForUriList', () => {
assert.strictEqual(pasteAsMarkdownLink, false);
});
test('Should evaluate pasteAsMarkdownLink as true for a link pasted in square brackets', () => {
skinnyDocument.getText = function () { return '[abc]'; };
const pasteAsMarkdownLink = checkSmartPaste(skinnyDocument, new vscode.Range(0, 1, 0, 4), new vscode.Range(0, 1, 0, 4));
assert.strictEqual(pasteAsMarkdownLink, true);
});
test('Should evaluate pasteAsMarkdownLink as false for no selection', () => {
const pasteAsMarkdownLink = checkSmartPaste(skinnyDocument, new vscode.Range(0, 0, 0, 0), new vscode.Range(0, 0, 0, 0));
assert.strictEqual(pasteAsMarkdownLink, false);