feat!: add escapeQuote and escapeBlockQuote (#11129)

BREAKING CHANGE: `escapeMarkdown` now escapes quotes and block quotes by default.

Co-authored-by: Jiralite <33201955+Jiralite@users.noreply.github.com>
This commit is contained in:
Almeida
2025-10-06 10:30:36 +01:00
committed by GitHub
parent cf88ef91fd
commit fbdec3d828
2 changed files with 146 additions and 21 deletions

View File

@@ -4,6 +4,13 @@
* The options that affect what will be escaped.
*/
export interface EscapeMarkdownOptions {
/**
* Whether to escape block quotes.
*
* @defaultValue `true`
*/
blockQuote?: boolean;
/**
* Whether to escape bold text.
*
@@ -80,6 +87,13 @@ export interface EscapeMarkdownOptions {
*/
numberedList?: boolean;
/**
* Whether to escape block quotes.
*
* @defaultValue `true`
*/
quote?: boolean;
/**
* Whether to escape spoilers.
*
@@ -124,6 +138,8 @@ export function escapeMarkdown(text: string, options: EscapeMarkdownOptions = {}
bulletedList = true,
numberedList = true,
maskedLink = true,
blockQuote = true,
quote = true,
} = options;
if (!codeBlockContent) {
@@ -144,6 +160,8 @@ export function escapeMarkdown(text: string, options: EscapeMarkdownOptions = {}
bulletedList,
numberedList,
maskedLink,
blockQuote,
quote,
});
})
.join(codeBlock ? '\\`\\`\\`' : '```');
@@ -166,6 +184,8 @@ export function escapeMarkdown(text: string, options: EscapeMarkdownOptions = {}
bulletedList,
numberedList,
maskedLink,
blockQuote,
quote,
});
})
.join(inlineCode ? '\\`' : '`');
@@ -184,6 +204,8 @@ export function escapeMarkdown(text: string, options: EscapeMarkdownOptions = {}
if (bulletedList) res = escapeBulletedList(res);
if (numberedList) res = escapeNumberedList(res);
if (maskedLink) res = escapeMaskedLink(res);
if (quote) res = escapeQuote(res);
if (blockQuote) res = escapeBlockQuote(res);
return res;
}
@@ -311,3 +333,21 @@ export function escapeNumberedList(text: string): string {
export function escapeMaskedLink(text: string): string {
return text.replaceAll(/\[.+]\(.+\)/gm, '\\$&');
}
/**
* Escapes quote characters in a string.
*
* @param text - Content to escape
*/
export function escapeQuote(text: string): string {
return text.replaceAll(/^(\s*)>(\s+)/gm, '$1\\>$2');
}
/**
* Escapes block quote characters in a string.
*
* @param text - Content to escape
*/
export function escapeBlockQuote(text: string): string {
return text.replaceAll(/^(\s*)>>>(\s+)/gm, '$1\\>>>$2');
}