Fix markdown link pasting when selection is inline code (#203657)

If the user selects a complete inline code block, we should paste as a markdown link with the code as the link text
This commit is contained in:
Matt Bierner
2024-01-29 08:10:52 +00:00
committed by GitHub
parent 0c6248932c
commit 772791e9e5
2 changed files with 29 additions and 4 deletions

View File

@@ -89,7 +89,7 @@ const smartPasteLineRegexes = [
{ regex: /\$\$[\s\S]*?\$\$/gm }, // In a fenced math block
{ regex: /`[^`]*`/g }, // In inline code
{ regex: /\$[^$]*\$/g }, // In inline math
{ regex: /^[ ]{0,3}\[\w+\]:\s.*$/g }, // Block link definition (needed as tokens are not generated for these)
{ regex: /^[ ]{0,3}\[\w+\]:\s.*$/g, isWholeLine: true }, // Block link definition (needed as tokens are not generated for these)
];
export async function shouldInsertMarkdownLinkByDefault(
@@ -184,7 +184,15 @@ async function shouldSmartPasteForSelection(
const line = document.getText(new vscode.Range(selectedRange.start.line, 0, selectedRange.start.line, Number.MAX_SAFE_INTEGER));
for (const regex of smartPasteLineRegexes) {
for (const match of line.matchAll(regex.regex)) {
if (match.index !== undefined && selectedRange.start.character >= match.index && selectedRange.start.character <= match.index + match[0].length) {
if (match.index === undefined) {
continue;
}
if (regex.isWholeLine) {
return false;
}
if (selectedRange.start.character > match.index && selectedRange.start.character < match.index + match[0].length) {
return false;
}
}