Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip encoding of markdown links #200588

Merged
merged 1 commit into from
Dec 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,11 @@ export function appendToLinkSnippet(
title: string,
link: string,
placeholderValue: number,
isExternalLink: boolean,
_isExternalLink: boolean,
): void {
snippet.appendText('[');
snippet.appendPlaceholder(escapeBrackets(title) || 'Title', placeholderValue);
snippet.appendText(`](${escapeMarkdownLinkPath(link, isExternalLink)})`);
snippet.appendText(`](${escapeMarkdownLinkPath(link)})`);
}

export function createUriListSnippet(
Expand Down Expand Up @@ -238,17 +238,15 @@ export function createUriListSnippet(
snippet.appendPlaceholder(escapeBrackets(title) || 'Title', placeholderValue);
snippet.appendText('"></audio>');
} else if (insertAsMedia) {
if (insertAsMedia) {
insertedImageCount++;
if (pasteAsMarkdownLink) {
snippet.appendText('![');
const placeholderText = escapeBrackets(title) || options?.placeholderText || 'Alt text';
const placeholderIndex = typeof options?.placeholderStartIndex !== 'undefined' ? options?.placeholderStartIndex + i : (placeholderValue === 0 ? undefined : placeholderValue);
snippet.appendPlaceholder(placeholderText, placeholderIndex);
snippet.appendText(`](${escapeMarkdownLinkPath(mdPath, isExternalLink)})`);
} else {
snippet.appendText(escapeMarkdownLinkPath(mdPath, isExternalLink));
}
insertedImageCount++;
if (pasteAsMarkdownLink) {
snippet.appendText('![');
const placeholderText = escapeBrackets(title) || options?.placeholderText || 'Alt text';
const placeholderIndex = typeof options?.placeholderStartIndex !== 'undefined' ? options?.placeholderStartIndex + i : (placeholderValue === 0 ? undefined : placeholderValue);
snippet.appendPlaceholder(placeholderText, placeholderIndex);
snippet.appendText(`](${escapeMarkdownLinkPath(mdPath)})`);
} else {
snippet.appendText(escapeMarkdownLinkPath(mdPath));
}
} else {
insertedLinkCount++;
Expand Down Expand Up @@ -371,20 +369,20 @@ function escapeHtmlAttribute(attr: string): string {
return encodeURI(attr).replaceAll('"', '&quot;');
}

function escapeMarkdownLinkPath(mdPath: string, isExternalLink: boolean): string {
function escapeMarkdownLinkPath(mdPath: string): string {
if (needsBracketLink(mdPath)) {
return '<' + mdPath.replaceAll('<', '\\<').replaceAll('>', '\\>') + '>';
}

return isExternalLink ? mdPath : encodeURI(mdPath);
return mdPath;
}

function escapeBrackets(value: string): string {
value = value.replace(/[\[\]]/g, '\\$&'); // CodeQL [SM02383] The Markdown is fully sanitized after being rendered.
return value;
}

function needsBracketLink(mdPath: string) {
function needsBracketLink(mdPath: string): boolean {
// Links with whitespace or control characters must be enclosed in brackets
if (mdPath.startsWith('<') || /\s|[\u007F\u0000-\u001f]/.test(mdPath)) {
return true;
Expand Down