discord-markdown-parser
Version:
Parse discord-style markdown into an abstract syntax tree.
122 lines (121 loc) • 3.96 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.GuildNavigationRegex = exports.SlashCommandRegex = exports.SubtextRegex = exports.HeadingRegex = exports.TimestampRegex = exports.TextRegex = exports.StrikeThroughRegex = exports.SpoilerRegex = exports.EmoticonRegex = exports.CodeBlockRegex = exports.BlockQuoteRegex = exports.HereRegex = exports.EveryoneRegex = exports.UserMentionRegex = exports.RoleMentionRegex = exports.EmojiRegex = exports.ChannelMentionRegex = void 0;
/**
* Matches Discord channel mentions.
* @example <#123456789012345678>
*
* The regex captures a 17-20 digit channel ID inside <#...> brackets.
*/
exports.ChannelMentionRegex = /^<#(\d{17,20})>/;
/**
* Matches custom emoji mentions.
* @example <:smile:12345678901234567>
*
* The regex captures:
* - Optional "a" for animated emojis
* - Emoji name (2-32 characters)
* - Emoji ID (17-21 digits)
*/
exports.EmojiRegex = /^<(a)?:(\w{2,32}):(\d{17,21})>/;
/**
* Matches Discord role mentions.
* @example <@&123456789012345678>
*
* The regex captures a 17-20 digit role ID inside <@&...> brackets.
*/
exports.RoleMentionRegex = /^<@&(\d{17,20})>/;
/**
* Matches Discord user mentions.
*
* @example <@123456789012345678> or <@!123456789012345678>
*
* The regex captures a 17-20 digit user ID, optionally preceded by "!".
*/
exports.UserMentionRegex = /^<@!?(\d{17,20})>/;
/**
* Matches the @everyone mention.
*/
exports.EveryoneRegex = /^@everyone/;
/**
* Matches the @here mention.
*/
exports.HereRegex = /^@here/;
/**
* Matches block quotes in two formats:
* 1. Single line: ">>> text"
* 2. Multi-line: "> text\n> more text"
*
* @example >>> This is a block quote
*/
exports.BlockQuoteRegex = /^( *>>> ([\s\S]*))|^( *> [^\n]*(\n *> [^\n]*)*\n?)/;
/**
* Matches code blocks with optional language specification.
*
* @example ```javascript
* const example = "code";
* ```
*/
exports.CodeBlockRegex = /^```(([a-z0-9_+\-.#]+?)\n+)?\n*([^]+?)\n*```/i;
/**
* Matches the ¯\_(ツ)_/¯ emoticon.
*/
exports.EmoticonRegex = /^(¯\\_\(ツ\)_\/¯)/;
/**
* Matches spoiler text wrapped in double pipes.
*
* @example ||This is a spoiler||
*/
exports.SpoilerRegex = /^\|\|([\s\S]+?)\|\|/;
/**
* Matches strikethrough text wrapped in tildes.
*
* @example ~~This text is strikethrough~~
*/
exports.StrikeThroughRegex = /^~~([\s\S]+?)~~(?!_)/;
/**
* Matches plain text content in messages.
*
* This regex is designed to capture text up to certain delimiters
* like punctuation, newlines, or specific patterns.
*/
exports.TextRegex = /^[\s\S]+?(?=[^0-9A-Za-z\s]|\n\n|\n|\w+:\S|$)/;
/**
* Matches Discord timestamp mentions.
*
* @example <t:123456> or <t:123456:t> for specific formats
*/
exports.TimestampRegex = /^<t:(-?\d+)(?::(R|t|T|d|D|f|F))?>/;
/**
* Matches markdown headings (H1-H3 level).
*
* @example # Heading
* @example ## Subheading
* @example ### Subsubheading
*/
exports.HeadingRegex = /^(#{1,3}) +([^\n]+?)(\n|$)/;
/**
* Matches subtext in markdown headings.
*
* @example -# Subtext
*/
exports.SubtextRegex = /^-# +([^\n]+?)(\n|$)/;
/**
* Matches for slash command mentions.
*
* @example </command:123456789012345678>
*
* Slash command names can be up to 32 characters long,
* and the regex for the name was taken from the discord api documentation
* Commands can also be nested up to 3 times (</command group subcommand:id>)
*/
exports.SlashCommandRegex = /^<\/([-_'\p{L}\p{N}\p{sc=Deva}\p{sc=Thai}]{1,32}(?: [-_'\p{L}\p{N}\p{sc=Deva}\p{sc=Thai}]{1,32}){0,2})?:(\d{17,21})>/u;
/**
* Matches for guild navigation mentions.
* The `linked-roles` type adds in another opptional :ROLE-ID field.
*
* @example <12345679012345678:customize>
*
* @see {@link https://discord.com/developers/docs/reference#message-formatting-guild-navigation-types} for available types
*/
exports.GuildNavigationRegex = /^<(\d{17,20}):(?:(customize|browse|guide)|(linked-roles)(:\d{17,20})?)>/;