mirror of
https://github.com/Microsoft/vscode
synced 2024-08-27 21:09:43 +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) {
|
||||
const snippetEdits = coalesce(activeEditor.selections.map((selection, i): vscode.SnippetTextEdit | undefined => {
|
||||
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,
|
||||
placeholderText: selectionText,
|
||||
placeholderStartIndex: (i + 1) * selectedFiles.length,
|
||||
|
|
|
@ -171,17 +171,17 @@ export async function tryGetUriListSnippet(document: SkinnyTextDocument, urlList
|
|||
if (token.isCancellationRequested) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const uriStrings: string[] = [];
|
||||
const uris: vscode.Uri[] = [];
|
||||
for (const resource of urlList.split(/\r?\n/g)) {
|
||||
try {
|
||||
uris.push(vscode.Uri.parse(resource));
|
||||
uriStrings.push(resource);
|
||||
} catch {
|
||||
// noop
|
||||
}
|
||||
}
|
||||
|
||||
return createUriListSnippet(document, uris, title, placeHolderValue, pasteAsMarkdownLink, isExternalLink);
|
||||
return createUriListSnippet(document, uris, uriStrings, title, placeHolderValue, pasteAsMarkdownLink, isExternalLink);
|
||||
}
|
||||
|
||||
interface UriListSnippetOptions {
|
||||
|
@ -204,11 +204,10 @@ export function appendToLinkSnippet(
|
|||
pasteAsMarkdownLink: boolean,
|
||||
mdPath: string,
|
||||
title: string,
|
||||
uri: vscode.Uri,
|
||||
uriString: string,
|
||||
placeholderValue: number,
|
||||
isExternalLink: boolean,
|
||||
): vscode.SnippetString {
|
||||
const uriString = uri.toString(true);
|
||||
if (pasteAsMarkdownLink) {
|
||||
snippet.appendText('[');
|
||||
snippet.appendPlaceholder(escapeBrackets(title) || 'Title', placeholderValue);
|
||||
|
@ -222,6 +221,7 @@ export function appendToLinkSnippet(
|
|||
export function createUriListSnippet(
|
||||
document: SkinnyTextDocument,
|
||||
uris: readonly vscode.Uri[],
|
||||
uriStrings?: readonly string[],
|
||||
title = '',
|
||||
placeholderValue = 0,
|
||||
pasteAsMarkdownLink = true,
|
||||
|
@ -272,7 +272,9 @@ export function createUriListSnippet(
|
|||
}
|
||||
} else {
|
||||
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) {
|
||||
|
|
|
@ -98,21 +98,33 @@ suite('createEditAddingLinksForUriList', () => {
|
|||
|
||||
suite('appendToLinkSnippet', () => {
|
||||
test('Should not create Markdown link snippet when pasteAsMarkdownLink is false', () => {
|
||||
const uri = vscode.Uri.parse('https://www.microsoft.com/');
|
||||
const snippet = appendToLinkSnippet(new vscode.SnippetString(''), false, 'https:/www.microsoft.com', '', uri, 0, true);
|
||||
assert.strictEqual(snippet?.value, 'https://www.microsoft.com/');
|
||||
const uriString = 'https://www.microsoft.com';
|
||||
const snippet = appendToLinkSnippet(new vscode.SnippetString(''), false, 'https:/www.microsoft.com', '', uriString, 0, true);
|
||||
assert.strictEqual(snippet?.value, 'https://www.microsoft.com');
|
||||
});
|
||||
|
||||
test('Should create Markdown link snippet when pasteAsMarkdownLink is true', () => {
|
||||
const uri = vscode.Uri.parse('https://www.microsoft.com/');
|
||||
const snippet = appendToLinkSnippet(new vscode.SnippetString(''), true, 'https:/www.microsoft.com', '', uri, 0, true);
|
||||
assert.strictEqual(snippet?.value, '[${0:Title}](https://www.microsoft.com/)');
|
||||
const uriString = 'https://www.microsoft.com';
|
||||
const snippet = appendToLinkSnippet(new vscode.SnippetString(''), true, 'https:/www.microsoft.com', '', uriString, 0, true);
|
||||
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', () => {
|
||||
const uri = vscode.Uri.parse('https://www.microsoft.com/');
|
||||
const snippet = appendToLinkSnippet(new vscode.SnippetString(''), true, 'https:/www.microsoft.com', '', uri, 0, true);
|
||||
assert.strictEqual(snippet?.value, '[${0:Title}](https://www.microsoft.com/)');
|
||||
const uriString = 'https://www.microsoft.com';
|
||||
const snippet = appendToLinkSnippet(new vscode.SnippetString(''), true, 'https:/www.microsoft.com', '', uriString, 0, true);
|
||||
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