mirror of
https://github.com/Microsoft/vscode
synced 2024-09-13 21:55:38 +00:00
Markdown link pasting does not encode or decode links (#188992)
* uris pasted with no encoding or decoding * add tests for checking encoding
This commit is contained in:
parent
c838b56b6c
commit
b5a46136c4
|
@ -79,7 +79,7 @@ async function insertLink(activeEditor: vscode.TextEditor, selectedFiles: vscode
|
||||||
function createInsertLinkEdit(activeEditor: vscode.TextEditor, selectedFiles: vscode.Uri[], insertAsMedia: boolean, title = '', placeholderValue = 0, pasteAsMarkdownLink = true, isExternalLink = false) {
|
function createInsertLinkEdit(activeEditor: vscode.TextEditor, selectedFiles: vscode.Uri[], insertAsMedia: boolean, title = '', placeholderValue = 0, pasteAsMarkdownLink = true, isExternalLink = false) {
|
||||||
const snippetEdits = coalesce(activeEditor.selections.map((selection, i): vscode.SnippetTextEdit | undefined => {
|
const snippetEdits = coalesce(activeEditor.selections.map((selection, i): vscode.SnippetTextEdit | undefined => {
|
||||||
const selectionText = activeEditor.document.getText(selection);
|
const selectionText = activeEditor.document.getText(selection);
|
||||||
const snippet = createUriListSnippet(activeEditor.document, selectedFiles, title, placeholderValue, pasteAsMarkdownLink, isExternalLink, {
|
const snippet = createUriListSnippet(activeEditor.document, selectedFiles, [], title, placeholderValue, pasteAsMarkdownLink, isExternalLink, {
|
||||||
insertAsMedia,
|
insertAsMedia,
|
||||||
placeholderText: selectionText,
|
placeholderText: selectionText,
|
||||||
placeholderStartIndex: (i + 1) * selectedFiles.length,
|
placeholderStartIndex: (i + 1) * selectedFiles.length,
|
||||||
|
|
|
@ -171,17 +171,17 @@ export async function tryGetUriListSnippet(document: SkinnyTextDocument, urlList
|
||||||
if (token.isCancellationRequested) {
|
if (token.isCancellationRequested) {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
const uriStrings: string[] = [];
|
||||||
const uris: vscode.Uri[] = [];
|
const uris: vscode.Uri[] = [];
|
||||||
for (const resource of urlList.split(/\r?\n/g)) {
|
for (const resource of urlList.split(/\r?\n/g)) {
|
||||||
try {
|
try {
|
||||||
uris.push(vscode.Uri.parse(resource));
|
uris.push(vscode.Uri.parse(resource));
|
||||||
|
uriStrings.push(resource);
|
||||||
} catch {
|
} catch {
|
||||||
// noop
|
// noop
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return createUriListSnippet(document, uris, uriStrings, title, placeHolderValue, pasteAsMarkdownLink, isExternalLink);
|
||||||
return createUriListSnippet(document, uris, title, placeHolderValue, pasteAsMarkdownLink, isExternalLink);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
interface UriListSnippetOptions {
|
interface UriListSnippetOptions {
|
||||||
|
@ -204,11 +204,10 @@ export function appendToLinkSnippet(
|
||||||
pasteAsMarkdownLink: boolean,
|
pasteAsMarkdownLink: boolean,
|
||||||
mdPath: string,
|
mdPath: string,
|
||||||
title: string,
|
title: string,
|
||||||
uri: vscode.Uri,
|
uriString: string,
|
||||||
placeholderValue: number,
|
placeholderValue: number,
|
||||||
isExternalLink: boolean,
|
isExternalLink: boolean,
|
||||||
): vscode.SnippetString {
|
): vscode.SnippetString {
|
||||||
const uriString = uri.toString(true);
|
|
||||||
if (pasteAsMarkdownLink) {
|
if (pasteAsMarkdownLink) {
|
||||||
snippet.appendText('[');
|
snippet.appendText('[');
|
||||||
snippet.appendPlaceholder(escapeBrackets(title) || 'Title', placeholderValue);
|
snippet.appendPlaceholder(escapeBrackets(title) || 'Title', placeholderValue);
|
||||||
|
@ -222,6 +221,7 @@ export function appendToLinkSnippet(
|
||||||
export function createUriListSnippet(
|
export function createUriListSnippet(
|
||||||
document: SkinnyTextDocument,
|
document: SkinnyTextDocument,
|
||||||
uris: readonly vscode.Uri[],
|
uris: readonly vscode.Uri[],
|
||||||
|
uriStrings?: readonly string[],
|
||||||
title = '',
|
title = '',
|
||||||
placeholderValue = 0,
|
placeholderValue = 0,
|
||||||
pasteAsMarkdownLink = true,
|
pasteAsMarkdownLink = true,
|
||||||
|
@ -272,7 +272,9 @@ export function createUriListSnippet(
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
insertedLinkCount++;
|
insertedLinkCount++;
|
||||||
snippet = appendToLinkSnippet(snippet, pasteAsMarkdownLink, mdPath, title, uri, placeholderValue, isExternalLink);
|
if (uriStrings) {
|
||||||
|
snippet = appendToLinkSnippet(snippet, pasteAsMarkdownLink, mdPath, title, uriStrings[i], placeholderValue, isExternalLink);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (i < uris.length - 1 && uris.length > 1) {
|
if (i < uris.length - 1 && uris.length > 1) {
|
||||||
|
|
|
@ -98,21 +98,33 @@ suite('createEditAddingLinksForUriList', () => {
|
||||||
|
|
||||||
suite('appendToLinkSnippet', () => {
|
suite('appendToLinkSnippet', () => {
|
||||||
test('Should not create Markdown link snippet when pasteAsMarkdownLink is false', () => {
|
test('Should not create Markdown link snippet when pasteAsMarkdownLink is false', () => {
|
||||||
const uri = vscode.Uri.parse('https://www.microsoft.com/');
|
const uriString = 'https://www.microsoft.com';
|
||||||
const snippet = appendToLinkSnippet(new vscode.SnippetString(''), false, 'https:/www.microsoft.com', '', uri, 0, true);
|
const snippet = appendToLinkSnippet(new vscode.SnippetString(''), false, 'https:/www.microsoft.com', '', uriString, 0, true);
|
||||||
assert.strictEqual(snippet?.value, 'https://www.microsoft.com/');
|
assert.strictEqual(snippet?.value, 'https://www.microsoft.com');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Should create Markdown link snippet when pasteAsMarkdownLink is true', () => {
|
test('Should create Markdown link snippet when pasteAsMarkdownLink is true', () => {
|
||||||
const uri = vscode.Uri.parse('https://www.microsoft.com/');
|
const uriString = 'https://www.microsoft.com';
|
||||||
const snippet = appendToLinkSnippet(new vscode.SnippetString(''), true, 'https:/www.microsoft.com', '', uri, 0, true);
|
const snippet = appendToLinkSnippet(new vscode.SnippetString(''), true, 'https:/www.microsoft.com', '', uriString, 0, true);
|
||||||
assert.strictEqual(snippet?.value, '[${0:Title}](https://www.microsoft.com/)');
|
assert.strictEqual(snippet?.value, '[${0:Title}](https://www.microsoft.com)');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Should use an unencoded URI string in Markdown link when passing in an external browser link', () => {
|
test('Should use an unencoded URI string in Markdown link when passing in an external browser link', () => {
|
||||||
const uri = vscode.Uri.parse('https://www.microsoft.com/');
|
const uriString = 'https://www.microsoft.com';
|
||||||
const snippet = appendToLinkSnippet(new vscode.SnippetString(''), true, 'https:/www.microsoft.com', '', uri, 0, true);
|
const snippet = appendToLinkSnippet(new vscode.SnippetString(''), true, 'https:/www.microsoft.com', '', uriString, 0, true);
|
||||||
assert.strictEqual(snippet?.value, '[${0:Title}](https://www.microsoft.com/)');
|
assert.strictEqual(snippet?.value, '[${0:Title}](https://www.microsoft.com)');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Should not decode an encoded URI string when passing in an external browser link', () => {
|
||||||
|
const uriString = 'https://www.microsoft.com/%20';
|
||||||
|
const snippet = appendToLinkSnippet(new vscode.SnippetString(''), true, 'https:/www.microsoft.com', '', uriString, 0, true);
|
||||||
|
assert.strictEqual(snippet?.value, '[${0:Title}](https://www.microsoft.com/%20)');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Should not encode an unencoded URI string when passing in an external browser link', () => {
|
||||||
|
const uriString = 'https://www.example.com/path?query=value&another=value#fragment';
|
||||||
|
const snippet = appendToLinkSnippet(new vscode.SnippetString(''), true, 'https:/www.microsoft.com', '', uriString, 0, true);
|
||||||
|
assert.strictEqual(snippet?.value, '[${0:Title}](https://www.example.com/path?query=value&another=value#fragment)');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue