mirror of
https://github.com/Microsoft/vscode
synced 2024-08-27 21:09:43 +00:00
Fixed markdown regular expression (#189423)
* fixed md regex * update shared.ts
This commit is contained in:
parent
ca5cfcca06
commit
0e15feeb95
|
@ -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) {
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in a new issue