Skip to content

Commit

Permalink
Skip encoding of markdown links (#200588)
Browse files Browse the repository at this point in the history
Fixes #200213

This encoding should no longer be needed now that we can smartly insert angle bracket links
  • Loading branch information
mjbvz authored Dec 11, 2023
1 parent c2ed45b commit 4c5336d
Showing 1 changed file with 14 additions and 16 deletions.
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

0 comments on commit 4c5336d

Please sign in to comment.