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

feat(Util): escape more markdown characters #8701

Merged
merged 5 commits into from Oct 10, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
70 changes: 70 additions & 0 deletions packages/discord.js/src/util/Util.js
Expand Up @@ -65,6 +65,11 @@ function flatten(obj, ...props) {
* @property {boolean} [spoiler=true] Whether to escape spoilers or not
* @property {boolean} [codeBlockContent=true] Whether to escape text inside code blocks or not
* @property {boolean} [inlineCodeContent=true] Whether to escape text inside inline code or not
* @property {boolean} [escape=true] Whether to escape escape characters or not
* @property {boolean} [heading=false] Whether to escape headings or not
* @property {boolean} [bulletedList=false] Whether to escape bulleted lists or not
* @property {boolean} [numberedList=false] Whether to escape numbered lists or not
* @property {boolean} [maskedLink=false] Whether to escape masked links or not
RedGuy12 marked this conversation as resolved.
Show resolved Hide resolved
*/

/**
Expand All @@ -85,6 +90,11 @@ function escapeMarkdown(
spoiler = true,
codeBlockContent = true,
inlineCodeContent = true,
escape = true,
heading = false,
bulletedList = false,
numberedList = false,
maskedLink = false,
} = {},
) {
if (!codeBlockContent) {
Expand All @@ -100,6 +110,11 @@ function escapeMarkdown(
strikethrough,
spoiler,
inlineCodeContent,
escape,
heading,
bulletedList,
numberedList,
maskedLink,
});
})
.join(codeBlock ? '\\`\\`\\`' : '```');
Expand All @@ -116,6 +131,11 @@ function escapeMarkdown(
underline,
strikethrough,
spoiler,
escape,
heading,
bulletedList,
numberedList,
maskedLink,
});
})
.join(inlineCode ? '\\`' : '`');
Expand All @@ -127,6 +147,11 @@ function escapeMarkdown(
if (underline) text = escapeUnderline(text);
if (strikethrough) text = escapeStrikethrough(text);
if (spoiler) text = escapeSpoiler(text);
if (escape) text = escapeEscape(text);
if (heading) text = escapeHeading(text);
if (bulletedList) text = escapeBulletedList(text);
if (numberedList) text = escapeNumberedList(text);
if (maskedLink) text = escapeMaskedLink(text);
return text;
}

Expand Down Expand Up @@ -210,6 +235,51 @@ function escapeSpoiler(text) {
return text.replaceAll('||', '\\|\\|');
}

/**
* Escapes escape characters in a string.
* @param {string} text Content to escape
* @returns {string}
*/
function escapeEscape(text) {
return text.replaceAll('\\', '\\\\');
}

/**
* Escapes heading characters in a string.
* @param {string} text Content to escape
* @returns {string}
*/
function escapeHeading(text) {
return text.replaceAll(/^( {0,2}[*-] +)?(#{1,3} )/gm, '$1\\$2');
}

/**
* Escapes bulleted list characters in a string.
* @param {string} text Content to escape
* @returns {string}
*/
function escapeBulletedList(text) {
return text.replaceAll(/^( *)[*-]( +)/gm, '$1\\-$2');
}

/**
* Escapes numbered list characters in a string.
* @param {string} text Content to escape
* @returns {string}
*/
function escapeNumberedList(text) {
return text.replaceAll(/^( *\d+)\./gm, '$1\\.');
}

/**
* Escapes masked link characters in a string.
* @param {string} text Content to escape
* @returns {string}
*/
function escapeMaskedLink(text) {
return text.replaceAll(/\[.+\]\(.+\)/gm, '\\$0');
}
RedGuy12 marked this conversation as resolved.
Show resolved Hide resolved

/**
* @typedef {Object} FetchRecommendedShardCountOptions
* @property {number} [guildsPerShard=1000] Number of guilds assigned per shard
Expand Down
12 changes: 11 additions & 1 deletion packages/discord.js/typings/index.d.ts
Expand Up @@ -2737,6 +2737,11 @@ export function escapeItalic(text: string): string;
export function escapeUnderline(text: string): string;
export function escapeStrikethrough(text: string): string;
export function escapeSpoiler(text: string): string;
export function escapeEscape(text: string): string;
export function escapeHeading(text: string): string;
export function escapeBulletedList(text: string): string;
export function escapeNumberedList(text: string): string;
export function escapeMaskedLink(text: string): string;
export function cleanCodeBlockContent(text: string): string;
export function fetchRecommendedShardCount(token: string, options?: FetchRecommendedShardCountOptions): Promise<number>;
export function flatten(obj: unknown, ...props: Record<string, boolean | string>[]): unknown;
Expand Down Expand Up @@ -4659,8 +4664,13 @@ export interface EscapeMarkdownOptions {
underline?: boolean;
strikethrough?: boolean;
spoiler?: boolean;
inlineCodeContent?: boolean;
RedGuy12 marked this conversation as resolved.
Show resolved Hide resolved
codeBlockContent?: boolean;
inlineCodeContent?: boolean;
escape?: boolean;
heading?: boolean;
bulletedList?: boolean;
numberedList?: boolean;
maskedLink?: boolean;
}

export interface FetchApplicationCommandOptions extends BaseFetchOptions {
Expand Down