@discordjs/formatters
Advanced tools
Comparing version 0.1.1-dev.1673611476-8c265b6.0 to 0.1.1-dev.1673654962-13ce78a.0
import { URL } from 'node:url'; | ||
import { Snowflake } from 'discord-api-types/globals'; | ||
interface EscapeMarkdownOptions { | ||
/** | ||
* Whether to escape bolds | ||
* | ||
* @defaultValue true | ||
*/ | ||
bold?: boolean; | ||
/** | ||
* Whether to escape bulleted lists | ||
* | ||
* @defaultValue false | ||
*/ | ||
bulletedList?: boolean; | ||
/** | ||
* Whether to escape code blocks | ||
* | ||
* @defaultValue true | ||
*/ | ||
codeBlock?: boolean; | ||
/** | ||
* Whether to escape text inside code blocks | ||
* | ||
* @defaultValue true | ||
*/ | ||
codeBlockContent?: boolean; | ||
/** | ||
* Whether to escape escape characters | ||
* | ||
* @defaultValue true | ||
*/ | ||
escape?: boolean; | ||
/** | ||
* Whether to escape headings | ||
* | ||
* @defaultValue false | ||
*/ | ||
heading?: boolean; | ||
/** | ||
* Whether to escape inline code | ||
* | ||
* @defaultValue true | ||
*/ | ||
inlineCode?: boolean; | ||
/** | ||
* Whether to escape text inside inline code | ||
* | ||
* @defaultValue true | ||
*/ | ||
inlineCodeContent?: boolean; | ||
/** | ||
* Whether to escape italics | ||
* | ||
* @defaultValue true | ||
*/ | ||
italic?: boolean; | ||
/** | ||
* Whether to escape masked links | ||
* | ||
* @defaultValue false | ||
*/ | ||
maskedLink?: boolean; | ||
/** | ||
* Whether to escape numbered lists | ||
* | ||
* @defaultValue false | ||
*/ | ||
numberedList?: boolean; | ||
/** | ||
* Whether to escape spoilers | ||
* | ||
* @defaultValue true | ||
*/ | ||
spoiler?: boolean; | ||
/** | ||
* Whether to escape strikethroughs | ||
* | ||
* @defaultValue true | ||
*/ | ||
strikethrough?: boolean; | ||
/** | ||
* Whether to escape underlines | ||
* | ||
* @defaultValue true | ||
*/ | ||
underline?: boolean; | ||
} | ||
/** | ||
* Escapes any Discord-flavour markdown in a string. | ||
* | ||
* @param text - Content to escape | ||
* @param options - Options for escaping the markdown | ||
*/ | ||
declare function escapeMarkdown(text: string, options?: EscapeMarkdownOptions): string; | ||
/** | ||
* Escapes code block markdown in a string. | ||
* | ||
* @param text - Content to escape | ||
*/ | ||
declare function escapeCodeBlock(text: string): string; | ||
/** | ||
* Escapes inline code markdown in a string. | ||
* | ||
* @param text - Content to escape | ||
*/ | ||
declare function escapeInlineCode(text: string): string; | ||
/** | ||
* Escapes italic markdown in a string. | ||
* | ||
* @param text - Content to escape | ||
*/ | ||
declare function escapeItalic(text: string): string; | ||
/** | ||
* Escapes bold markdown in a string. | ||
* | ||
* @param text - Content to escape | ||
*/ | ||
declare function escapeBold(text: string): string; | ||
/** | ||
* Escapes underline markdown in a string. | ||
* | ||
* @param text - Content to escape | ||
*/ | ||
declare function escapeUnderline(text: string): string; | ||
/** | ||
* Escapes strikethrough markdown in a string. | ||
* | ||
* @param text - Content to escape | ||
*/ | ||
declare function escapeStrikethrough(text: string): string; | ||
/** | ||
* Escapes spoiler markdown in a string. | ||
* | ||
* @param text - Content to escape | ||
*/ | ||
declare function escapeSpoiler(text: string): string; | ||
/** | ||
* Escapes escape characters in a string. | ||
* | ||
* @param text - Content to escape | ||
*/ | ||
declare function escapeEscape(text: string): string; | ||
/** | ||
* Escapes heading characters in a string. | ||
* | ||
* @param text - Content to escape | ||
*/ | ||
declare function escapeHeading(text: string): string; | ||
/** | ||
* Escapes bulleted list characters in a string. | ||
* | ||
* @param text - Content to escape | ||
*/ | ||
declare function escapeBulletedList(text: string): string; | ||
/** | ||
* Escapes numbered list characters in a string. | ||
* | ||
* @param text - Content to escape | ||
*/ | ||
declare function escapeNumberedList(text: string): string; | ||
/** | ||
* Escapes masked link characters in a string. | ||
* | ||
* @param text - Content to escape | ||
*/ | ||
declare function escapeMaskedLink(text: string): string; | ||
/** | ||
* Wraps the content inside a codeblock with no language | ||
@@ -278,2 +444,2 @@ * | ||
export { Faces, TimestampStyles, TimestampStylesString, blockQuote, bold, channelLink, channelMention, chatInputApplicationCommandMention, codeBlock, formatEmoji, hideLinkEmbed, hyperlink, inlineCode, italic, messageLink, quote, roleMention, spoiler, strikethrough, time, underscore, userMention }; | ||
export { EscapeMarkdownOptions, Faces, TimestampStyles, TimestampStylesString, blockQuote, bold, channelLink, channelMention, chatInputApplicationCommandMention, codeBlock, escapeBold, escapeBulletedList, escapeCodeBlock, escapeEscape, escapeHeading, escapeInlineCode, escapeItalic, escapeMarkdown, escapeMaskedLink, escapeNumberedList, escapeSpoiler, escapeStrikethrough, escapeUnderline, formatEmoji, hideLinkEmbed, hyperlink, inlineCode, italic, messageLink, quote, roleMention, spoiler, strikethrough, time, underscore, userMention }; |
@@ -32,2 +32,15 @@ "use strict"; | ||
codeBlock: () => codeBlock, | ||
escapeBold: () => escapeBold, | ||
escapeBulletedList: () => escapeBulletedList, | ||
escapeCodeBlock: () => escapeCodeBlock, | ||
escapeEscape: () => escapeEscape, | ||
escapeHeading: () => escapeHeading, | ||
escapeInlineCode: () => escapeInlineCode, | ||
escapeItalic: () => escapeItalic, | ||
escapeMarkdown: () => escapeMarkdown, | ||
escapeMaskedLink: () => escapeMaskedLink, | ||
escapeNumberedList: () => escapeNumberedList, | ||
escapeSpoiler: () => escapeSpoiler, | ||
escapeStrikethrough: () => escapeStrikethrough, | ||
escapeUnderline: () => escapeUnderline, | ||
formatEmoji: () => formatEmoji, | ||
@@ -49,2 +62,157 @@ hideLinkEmbed: () => hideLinkEmbed, | ||
// src/escapers.ts | ||
function escapeMarkdown(text, options = {}) { | ||
const { | ||
codeBlock: codeBlock2 = true, | ||
inlineCode: inlineCode2 = true, | ||
bold: bold2 = true, | ||
italic: italic2 = true, | ||
underline = true, | ||
strikethrough: strikethrough2 = true, | ||
spoiler: spoiler2 = true, | ||
codeBlockContent = true, | ||
inlineCodeContent = true, | ||
escape = true, | ||
heading = false, | ||
bulletedList = false, | ||
numberedList = false, | ||
maskedLink = false | ||
} = options; | ||
if (!codeBlockContent) { | ||
return text.split("```").map((subString, index, array) => { | ||
if (index % 2 && index !== array.length - 1) | ||
return subString; | ||
return escapeMarkdown(subString, { | ||
inlineCode: inlineCode2, | ||
bold: bold2, | ||
italic: italic2, | ||
underline, | ||
strikethrough: strikethrough2, | ||
spoiler: spoiler2, | ||
inlineCodeContent, | ||
escape, | ||
heading, | ||
bulletedList, | ||
numberedList, | ||
maskedLink | ||
}); | ||
}).join(codeBlock2 ? "\\`\\`\\`" : "```"); | ||
} | ||
if (!inlineCodeContent) { | ||
return text.split(/(?<=^|[^`])`(?=[^`]|$)/g).map((subString, index, array) => { | ||
if (index % 2 && index !== array.length - 1) | ||
return subString; | ||
return escapeMarkdown(subString, { | ||
codeBlock: codeBlock2, | ||
bold: bold2, | ||
italic: italic2, | ||
underline, | ||
strikethrough: strikethrough2, | ||
spoiler: spoiler2, | ||
escape, | ||
heading, | ||
bulletedList, | ||
numberedList, | ||
maskedLink | ||
}); | ||
}).join(inlineCode2 ? "\\`" : "`"); | ||
} | ||
let res = text; | ||
if (escape) | ||
res = escapeEscape(res); | ||
if (inlineCode2) | ||
res = escapeInlineCode(res); | ||
if (codeBlock2) | ||
res = escapeCodeBlock(res); | ||
if (italic2) | ||
res = escapeItalic(res); | ||
if (bold2) | ||
res = escapeBold(res); | ||
if (underline) | ||
res = escapeUnderline(res); | ||
if (strikethrough2) | ||
res = escapeStrikethrough(res); | ||
if (spoiler2) | ||
res = escapeSpoiler(res); | ||
if (heading) | ||
res = escapeHeading(res); | ||
if (bulletedList) | ||
res = escapeBulletedList(res); | ||
if (numberedList) | ||
res = escapeNumberedList(res); | ||
if (maskedLink) | ||
res = escapeMaskedLink(res); | ||
return res; | ||
} | ||
__name(escapeMarkdown, "escapeMarkdown"); | ||
function escapeCodeBlock(text) { | ||
return text.replaceAll("```", "\\`\\`\\`"); | ||
} | ||
__name(escapeCodeBlock, "escapeCodeBlock"); | ||
function escapeInlineCode(text) { | ||
return text.replaceAll(/(?<=^|[^`])``?(?=[^`]|$)/g, (match) => match.length === 2 ? "\\`\\`" : "\\`"); | ||
} | ||
__name(escapeInlineCode, "escapeInlineCode"); | ||
function escapeItalic(text) { | ||
let idx = 0; | ||
const newText = text.replaceAll(/(?<=^|[^*])\*([^*]|\*\*|$)/g, (_, match) => { | ||
if (match === "**") | ||
return ++idx % 2 ? `\\*${match}` : `${match}\\*`; | ||
return `\\*${match}`; | ||
}); | ||
idx = 0; | ||
return newText.replaceAll(/(?<=^|[^_])(?<!<a?:.+)_(?!:\d+>)([^_]|__|$)/g, (_, match) => { | ||
if (match === "__") | ||
return ++idx % 2 ? `\\_${match}` : `${match}\\_`; | ||
return `\\_${match}`; | ||
}); | ||
} | ||
__name(escapeItalic, "escapeItalic"); | ||
function escapeBold(text) { | ||
let idx = 0; | ||
return text.replaceAll(/\*\*(\*)?/g, (_, match) => { | ||
if (match) | ||
return ++idx % 2 ? `${match}\\*\\*` : `\\*\\*${match}`; | ||
return "\\*\\*"; | ||
}); | ||
} | ||
__name(escapeBold, "escapeBold"); | ||
function escapeUnderline(text) { | ||
let idx = 0; | ||
return text.replaceAll(/(?<!<a?:.+)__(_)?(?!:\d+>)/g, (_, match) => { | ||
if (match) | ||
return ++idx % 2 ? `${match}\\_\\_` : `\\_\\_${match}`; | ||
return "\\_\\_"; | ||
}); | ||
} | ||
__name(escapeUnderline, "escapeUnderline"); | ||
function escapeStrikethrough(text) { | ||
return text.replaceAll("~~", "\\~\\~"); | ||
} | ||
__name(escapeStrikethrough, "escapeStrikethrough"); | ||
function escapeSpoiler(text) { | ||
return text.replaceAll("||", "\\|\\|"); | ||
} | ||
__name(escapeSpoiler, "escapeSpoiler"); | ||
function escapeEscape(text) { | ||
return text.replaceAll("\\", "\\\\"); | ||
} | ||
__name(escapeEscape, "escapeEscape"); | ||
function escapeHeading(text) { | ||
return text.replaceAll(/^( {0,2})([*-] )?( *)(#{1,3} )/gm, "$1$2$3\\$4"); | ||
} | ||
__name(escapeHeading, "escapeHeading"); | ||
function escapeBulletedList(text) { | ||
return text.replaceAll(/^( *)([*-])( +)/gm, "$1\\$2$3"); | ||
} | ||
__name(escapeBulletedList, "escapeBulletedList"); | ||
function escapeNumberedList(text) { | ||
return text.replaceAll(/^( *\d+)\./gm, "$1\\."); | ||
} | ||
__name(escapeNumberedList, "escapeNumberedList"); | ||
function escapeMaskedLink(text) { | ||
return text.replaceAll(/\[.+]\(.+\)/gm, "\\$&"); | ||
} | ||
__name(escapeMaskedLink, "escapeMaskedLink"); | ||
// src/formatters.ts | ||
@@ -165,2 +333,15 @@ function codeBlock(language, content) { | ||
codeBlock, | ||
escapeBold, | ||
escapeBulletedList, | ||
escapeCodeBlock, | ||
escapeEscape, | ||
escapeHeading, | ||
escapeInlineCode, | ||
escapeItalic, | ||
escapeMarkdown, | ||
escapeMaskedLink, | ||
escapeNumberedList, | ||
escapeSpoiler, | ||
escapeStrikethrough, | ||
escapeUnderline, | ||
formatEmoji, | ||
@@ -167,0 +348,0 @@ hideLinkEmbed, |
{ | ||
"name": "@discordjs/formatters", | ||
"version": "0.1.1-dev.1673611476-8c265b6.0", | ||
"version": "0.1.1-dev.1673654962-13ce78a.0", | ||
"description": "A set of functions to format strings for Discord.", | ||
@@ -5,0 +5,0 @@ "scripts": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
101056
1093