|
|
|
@ -1,4 +1,4 @@
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
using DiscordChatExporter.Core.Markdown.Internal;
|
|
|
|
@ -10,94 +10,94 @@ namespace DiscordChatExporter.Core.Markdown
|
|
|
|
|
// The following parsing logic is meant to replicate Discord's markdown grammar as close as possible
|
|
|
|
|
public static class MarkdownParser
|
|
|
|
|
{
|
|
|
|
|
private const RegexOptions DefaultRegexOptions = RegexOptions.Compiled | RegexOptions.CultureInvariant;
|
|
|
|
|
private const RegexOptions DefaultRegexOptions = RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.Multiline;
|
|
|
|
|
|
|
|
|
|
/* Formatting */
|
|
|
|
|
|
|
|
|
|
// Capture any character until the earliest double asterisk not followed by an asterisk
|
|
|
|
|
private static readonly IMatcher<Node> BoldFormattedNodeMatcher = new RegexMatcher<Node>(
|
|
|
|
|
new Regex("\\*\\*(.+?)\\*\\*(?!\\*)", DefaultRegexOptions | RegexOptions.Singleline),
|
|
|
|
|
m => new FormattedNode(m.Value, "**", TextFormatting.Bold, Parse(m.Groups[1].Value)));
|
|
|
|
|
(p, m) => new FormattedNode(TextFormatting.Bold, Parse(p.Shrink(m.Groups[1]))));
|
|
|
|
|
|
|
|
|
|
// Capture any character until the earliest single asterisk not preceded or followed by an asterisk
|
|
|
|
|
// Opening asterisk must not be followed by whitespace
|
|
|
|
|
// Closing asterisk must not be preceded by whitespace
|
|
|
|
|
private static readonly IMatcher<Node> ItalicFormattedNodeMatcher = new RegexMatcher<Node>(
|
|
|
|
|
new Regex("\\*(?!\\s)(.+?)(?<!\\s|\\*)\\*(?!\\*)", DefaultRegexOptions | RegexOptions.Singleline),
|
|
|
|
|
m => new FormattedNode(m.Value, "*", TextFormatting.Italic, Parse(m.Groups[1].Value)));
|
|
|
|
|
(p, m) => new FormattedNode(TextFormatting.Italic, Parse(p.Shrink(m.Groups[1]))));
|
|
|
|
|
|
|
|
|
|
// Capture any character until the earliest triple asterisk not followed by an asterisk
|
|
|
|
|
private static readonly IMatcher<Node> ItalicBoldFormattedNodeMatcher = new RegexMatcher<Node>(
|
|
|
|
|
new Regex("\\*(\\*\\*.+?\\*\\*)\\*(?!\\*)", DefaultRegexOptions | RegexOptions.Singleline),
|
|
|
|
|
m => new FormattedNode(m.Value, "*", TextFormatting.Italic, Parse(m.Groups[1].Value, BoldFormattedNodeMatcher)));
|
|
|
|
|
(p, m) => new FormattedNode(TextFormatting.Italic, Parse(p.Shrink(m.Groups[1]), BoldFormattedNodeMatcher)));
|
|
|
|
|
|
|
|
|
|
// Capture any character except underscore until an underscore
|
|
|
|
|
// Closing underscore must not be followed by a word character
|
|
|
|
|
private static readonly IMatcher<Node> ItalicAltFormattedNodeMatcher = new RegexMatcher<Node>(
|
|
|
|
|
new Regex("_([^_]+)_(?!\\w)", DefaultRegexOptions | RegexOptions.Singleline),
|
|
|
|
|
m => new FormattedNode(m.Value, "_", TextFormatting.Italic, Parse(m.Groups[1].Value)));
|
|
|
|
|
(p, m) => new FormattedNode(TextFormatting.Italic, Parse(p.Shrink(m.Groups[1]))));
|
|
|
|
|
|
|
|
|
|
// Capture any character until the earliest double underscore not followed by an underscore
|
|
|
|
|
private static readonly IMatcher<Node> UnderlineFormattedNodeMatcher = new RegexMatcher<Node>(
|
|
|
|
|
new Regex("__(.+?)__(?!_)", DefaultRegexOptions | RegexOptions.Singleline),
|
|
|
|
|
m => new FormattedNode(m.Value, "__", TextFormatting.Underline, Parse(m.Groups[1].Value)));
|
|
|
|
|
(p, m) => new FormattedNode(TextFormatting.Underline, Parse(p.Shrink(m.Groups[1]))));
|
|
|
|
|
|
|
|
|
|
// Capture any character until the earliest triple underscore not followed by an underscore
|
|
|
|
|
private static readonly IMatcher<Node> ItalicUnderlineFormattedNodeMatcher = new RegexMatcher<Node>(
|
|
|
|
|
new Regex("_(__.+?__)_(?!_)", DefaultRegexOptions | RegexOptions.Singleline),
|
|
|
|
|
m => new FormattedNode(m.Value, "_", TextFormatting.Italic, Parse(m.Groups[1].Value, UnderlineFormattedNodeMatcher)));
|
|
|
|
|
(p, m) => new FormattedNode(TextFormatting.Italic, Parse(p.Shrink(m.Groups[1]), UnderlineFormattedNodeMatcher)));
|
|
|
|
|
|
|
|
|
|
// Capture any character until the earliest double tilde
|
|
|
|
|
private static readonly IMatcher<Node> StrikethroughFormattedNodeMatcher = new RegexMatcher<Node>(
|
|
|
|
|
new Regex("~~(.+?)~~", DefaultRegexOptions | RegexOptions.Singleline),
|
|
|
|
|
m => new FormattedNode(m.Value, "~~", TextFormatting.Strikethrough, Parse(m.Groups[1].Value)));
|
|
|
|
|
(p, m) => new FormattedNode(TextFormatting.Strikethrough, Parse(p.Shrink(m.Groups[1]))));
|
|
|
|
|
|
|
|
|
|
// Capture any character until the earliest double pipe
|
|
|
|
|
private static readonly IMatcher<Node> SpoilerFormattedNodeMatcher = new RegexMatcher<Node>(
|
|
|
|
|
new Regex("\\|\\|(.+?)\\|\\|", DefaultRegexOptions | RegexOptions.Singleline),
|
|
|
|
|
m => new FormattedNode(m.Value, "||", TextFormatting.Spoiler, Parse(m.Groups[1].Value)));
|
|
|
|
|
(p, m) => new FormattedNode(TextFormatting.Spoiler, Parse(p.Shrink(m.Groups[1]))));
|
|
|
|
|
|
|
|
|
|
/* Code blocks */
|
|
|
|
|
|
|
|
|
|
// Capture any character except backtick until a backtick
|
|
|
|
|
// Whitespace surrounding content inside backticks is trimmed
|
|
|
|
|
// Blank lines at the beginning and end of content are trimmed
|
|
|
|
|
private static readonly IMatcher<Node> InlineCodeBlockNodeMatcher = new RegexMatcher<Node>(
|
|
|
|
|
new Regex("`([^`]+)`", DefaultRegexOptions | RegexOptions.Singleline),
|
|
|
|
|
m => new InlineCodeBlockNode(m.Value, m.Groups[1].Value.Trim()));
|
|
|
|
|
m => new InlineCodeBlockNode(m.Groups[1].Value.Trim('\r', '\n')));
|
|
|
|
|
|
|
|
|
|
// Capture language identifier and then any character until the earliest triple backtick
|
|
|
|
|
// Languge identifier is one word immediately after opening backticks, followed immediately by newline
|
|
|
|
|
// Whitespace surrounding content inside backticks is trimmed
|
|
|
|
|
private static readonly IMatcher<Node> MultilineCodeBlockNodeMatcher = new RegexMatcher<Node>(
|
|
|
|
|
// Language identifier is one word immediately after opening backticks, followed immediately by newline
|
|
|
|
|
// Blank lines at the beginning and end of content are trimmed
|
|
|
|
|
private static readonly IMatcher<Node> MultiLineCodeBlockNodeMatcher = new RegexMatcher<Node>(
|
|
|
|
|
new Regex("```(?:(\\w*)\\n)?(.+?)```", DefaultRegexOptions | RegexOptions.Singleline),
|
|
|
|
|
m => new MultilineCodeBlockNode(m.Value, m.Groups[1].Value, m.Groups[2].Value.Trim()));
|
|
|
|
|
m => new MultiLineCodeBlockNode(m.Groups[1].Value, m.Groups[2].Value.Trim('\r', '\n')));
|
|
|
|
|
|
|
|
|
|
/* Mentions */
|
|
|
|
|
|
|
|
|
|
// Capture @everyone
|
|
|
|
|
private static readonly IMatcher<Node> EveryoneMentionNodeMatcher = new StringMatcher<Node>(
|
|
|
|
|
"@everyone",
|
|
|
|
|
s => new MentionNode(s, "everyone", MentionType.Meta));
|
|
|
|
|
p => new MentionNode("everyone", MentionType.Meta));
|
|
|
|
|
|
|
|
|
|
// Capture @here
|
|
|
|
|
private static readonly IMatcher<Node> HereMentionNodeMatcher = new StringMatcher<Node>(
|
|
|
|
|
"@here",
|
|
|
|
|
s => new MentionNode(s, "here", MentionType.Meta));
|
|
|
|
|
p => new MentionNode("here", MentionType.Meta));
|
|
|
|
|
|
|
|
|
|
// Capture <@123456> or <@!123456>
|
|
|
|
|
private static readonly IMatcher<Node> UserMentionNodeMatcher = new RegexMatcher<Node>(
|
|
|
|
|
new Regex("<@!?(\\d+)>", DefaultRegexOptions),
|
|
|
|
|
m => new MentionNode(m.Value, m.Groups[1].Value, MentionType.User));
|
|
|
|
|
m => new MentionNode(m.Groups[1].Value, MentionType.User));
|
|
|
|
|
|
|
|
|
|
// Capture <#123456>
|
|
|
|
|
private static readonly IMatcher<Node> ChannelMentionNodeMatcher = new RegexMatcher<Node>(
|
|
|
|
|
new Regex("<#(\\d+)>", DefaultRegexOptions),
|
|
|
|
|
m => new MentionNode(m.Value, m.Groups[1].Value, MentionType.Channel));
|
|
|
|
|
m => new MentionNode(m.Groups[1].Value, MentionType.Channel));
|
|
|
|
|
|
|
|
|
|
// Capture <@&123456>
|
|
|
|
|
private static readonly IMatcher<Node> RoleMentionNodeMatcher = new RegexMatcher<Node>(
|
|
|
|
|
new Regex("<@&(\\d+)>", DefaultRegexOptions),
|
|
|
|
|
m => new MentionNode(m.Value, m.Groups[1].Value, MentionType.Role));
|
|
|
|
|
m => new MentionNode(m.Groups[1].Value, MentionType.Role));
|
|
|
|
|
|
|
|
|
|
/* Emojis */
|
|
|
|
|
|
|
|
|
@ -108,29 +108,29 @@ namespace DiscordChatExporter.Core.Markdown
|
|
|
|
|
// (this does not match all emojis in Discord but it's reasonably accurate enough)
|
|
|
|
|
private static readonly IMatcher<Node> StandardEmojiNodeMatcher = new RegexMatcher<Node>(
|
|
|
|
|
new Regex("((?:[\\uD83C][\\uDDE6-\\uDDFF]){2}|\\p{So}|\\p{Cs}{2}|\\d\\p{Me})", DefaultRegexOptions),
|
|
|
|
|
m => new EmojiNode(m.Value, m.Groups[1].Value));
|
|
|
|
|
m => new EmojiNode(m.Groups[1].Value));
|
|
|
|
|
|
|
|
|
|
// Capture <:lul:123456> or <a:lul:123456>
|
|
|
|
|
private static readonly IMatcher<Node> CustomEmojiNodeMatcher = new RegexMatcher<Node>(
|
|
|
|
|
new Regex("<(a)?:(.+?):(\\d+?)>", DefaultRegexOptions),
|
|
|
|
|
m => new EmojiNode(m.Value, m.Groups[3].Value, m.Groups[2].Value, !m.Groups[1].Value.IsNullOrWhiteSpace()));
|
|
|
|
|
m => new EmojiNode(m.Groups[3].Value, m.Groups[2].Value, !m.Groups[1].Value.IsNullOrWhiteSpace()));
|
|
|
|
|
|
|
|
|
|
/* Links */
|
|
|
|
|
|
|
|
|
|
// Capture [title](link)
|
|
|
|
|
private static readonly IMatcher<Node> TitledLinkNodeMatcher = new RegexMatcher<Node>(
|
|
|
|
|
new Regex("\\[(.+?)\\]\\((.+?)\\)", DefaultRegexOptions),
|
|
|
|
|
m => new LinkNode(m.Value, m.Groups[2].Value, m.Groups[1].Value));
|
|
|
|
|
m => new LinkNode(m.Groups[2].Value, m.Groups[1].Value));
|
|
|
|
|
|
|
|
|
|
// Capture any non-whitespace character after http:// or https:// until the last punctuation character or whitespace
|
|
|
|
|
private static readonly IMatcher<Node> AutoLinkNodeMatcher = new RegexMatcher<Node>(
|
|
|
|
|
new Regex("(https?://\\S*[^\\.,:;\"\'\\s])", DefaultRegexOptions),
|
|
|
|
|
m => new LinkNode(m.Value, m.Groups[1].Value));
|
|
|
|
|
m => new LinkNode(m.Groups[1].Value));
|
|
|
|
|
|
|
|
|
|
// Same as auto link but also surrounded by angular brackets
|
|
|
|
|
private static readonly IMatcher<Node> HiddenLinkNodeMatcher = new RegexMatcher<Node>(
|
|
|
|
|
new Regex("<(https?://\\S*[^\\.,:;\"\'\\s])>", DefaultRegexOptions),
|
|
|
|
|
m => new LinkNode(m.Value, m.Groups[1].Value));
|
|
|
|
|
m => new LinkNode(m.Groups[1].Value));
|
|
|
|
|
|
|
|
|
|
/* Text */
|
|
|
|
|
|
|
|
|
@ -138,25 +138,25 @@ namespace DiscordChatExporter.Core.Markdown
|
|
|
|
|
// This escapes it from matching for formatting
|
|
|
|
|
private static readonly IMatcher<Node> ShrugTextNodeMatcher = new StringMatcher<Node>(
|
|
|
|
|
@"¯\_(ツ)_/¯",
|
|
|
|
|
s => new TextNode(s));
|
|
|
|
|
p => new TextNode(p.ToString()));
|
|
|
|
|
|
|
|
|
|
// Capture some specific emojis that don't get rendered
|
|
|
|
|
// This escapes it from matching for emoji
|
|
|
|
|
private static readonly IMatcher<Node> IgnoredEmojiTextNodeMatcher = new RegexMatcher<Node>(
|
|
|
|
|
new Regex("(\\u26A7|\\u2640|\\u2642|\\u2695|\\u267E|\\u00A9|\\u00AE|\\u2122)", DefaultRegexOptions),
|
|
|
|
|
m => new TextNode(m.Value, m.Groups[1].Value));
|
|
|
|
|
m => new TextNode(m.Groups[1].Value));
|
|
|
|
|
|
|
|
|
|
// Capture any "symbol/other" character or surrogate pair preceded by a backslash
|
|
|
|
|
// This escapes it from matching for emoji
|
|
|
|
|
private static readonly IMatcher<Node> EscapedSymbolTextNodeMatcher = new RegexMatcher<Node>(
|
|
|
|
|
new Regex("\\\\(\\p{So}|\\p{Cs}{2})", DefaultRegexOptions),
|
|
|
|
|
m => new TextNode(m.Value, m.Groups[1].Value));
|
|
|
|
|
m => new TextNode(m.Groups[1].Value));
|
|
|
|
|
|
|
|
|
|
// Capture any non-whitespace, non latin alphanumeric character preceded by a backslash
|
|
|
|
|
// This escapes it from matching for formatting or other tokens
|
|
|
|
|
private static readonly IMatcher<Node> EscapedCharacterTextNodeMatcher = new RegexMatcher<Node>(
|
|
|
|
|
new Regex("\\\\([^a-zA-Z0-9\\s])", DefaultRegexOptions),
|
|
|
|
|
m => new TextNode(m.Value, m.Groups[1].Value));
|
|
|
|
|
m => new TextNode(m.Groups[1].Value));
|
|
|
|
|
|
|
|
|
|
// Combine all matchers into one
|
|
|
|
|
// Matchers that have similar patterns are ordered from most specific to least specific
|
|
|
|
@ -178,7 +178,7 @@ namespace DiscordChatExporter.Core.Markdown
|
|
|
|
|
SpoilerFormattedNodeMatcher,
|
|
|
|
|
|
|
|
|
|
// Code blocks
|
|
|
|
|
MultilineCodeBlockNodeMatcher,
|
|
|
|
|
MultiLineCodeBlockNodeMatcher,
|
|
|
|
|
InlineCodeBlockNodeMatcher,
|
|
|
|
|
|
|
|
|
|
// Mentions
|
|
|
|
@ -197,9 +197,27 @@ namespace DiscordChatExporter.Core.Markdown
|
|
|
|
|
StandardEmojiNodeMatcher,
|
|
|
|
|
CustomEmojiNodeMatcher);
|
|
|
|
|
|
|
|
|
|
private static IReadOnlyList<Node> Parse(string input, IMatcher<Node> matcher) =>
|
|
|
|
|
matcher.MatchAll(input, s => new TextNode(s)).Select(r => r.Value).ToArray();
|
|
|
|
|
private static readonly IMatcher<Node> MinimalAggregateNodeMatcher = new AggregateMatcher<Node>(
|
|
|
|
|
// Mentions
|
|
|
|
|
EveryoneMentionNodeMatcher,
|
|
|
|
|
HereMentionNodeMatcher,
|
|
|
|
|
UserMentionNodeMatcher,
|
|
|
|
|
ChannelMentionNodeMatcher,
|
|
|
|
|
RoleMentionNodeMatcher,
|
|
|
|
|
|
|
|
|
|
// Emoji
|
|
|
|
|
StandardEmojiNodeMatcher,
|
|
|
|
|
CustomEmojiNodeMatcher);
|
|
|
|
|
|
|
|
|
|
private static IReadOnlyList<Node> Parse(StringPart stringPart, IMatcher<Node> matcher) =>
|
|
|
|
|
matcher.MatchAll(stringPart, p => new TextNode(p.ToString())).Select(r => r.Value).ToArray();
|
|
|
|
|
|
|
|
|
|
private static IReadOnlyList<Node> Parse(StringPart stringPart) => Parse(stringPart, AggregateNodeMatcher);
|
|
|
|
|
|
|
|
|
|
private static IReadOnlyList<Node> ParseMinimal(StringPart stringPart) => Parse(stringPart, MinimalAggregateNodeMatcher);
|
|
|
|
|
|
|
|
|
|
public static IReadOnlyList<Node> Parse(string input) => Parse(new StringPart(input));
|
|
|
|
|
|
|
|
|
|
public static IReadOnlyList<Node> Parse(string input) => Parse(input, AggregateNodeMatcher);
|
|
|
|
|
public static IReadOnlyList<Node> ParseMinimal(string input) => ParseMinimal(new StringPart(input));
|
|
|
|
|
}
|
|
|
|
|
}
|