");
- var contentFormatted = FormatMessageContentHtml(message.Content);
+ var contentFormatted = FormatMessageContentHtml(message);
await writer.WriteAsync(contentFormatted);
// Edited timestamp
@@ -215,8 +216,39 @@ namespace DiscordChatExporter.Services
return $"{size:0.#} {units[unit]}";
}
- private static string FormatMessageContentHtml(string content)
+ public static string FormatMessageContentText(Message message)
{
+ var content = message.Content;
+
+ // New lines
+ content = content.Replace("\n", Environment.NewLine);
+
+ // User mentions (<@id>)
+ content = Regex.Replace(content, "<@(\\d*)>",
+ m =>
+ {
+ var mentionedUser = message.MentionedUsers.First(u => u.Id == m.Groups[1].Value);
+ return $"@{mentionedUser}";
+ });
+
+ // Role mentions (<@&id>)
+ content = Regex.Replace(content, "<@&(\\d*)>",
+ m =>
+ {
+ var mentionedRole = message.MentionedRoles.First(r => r.Id == m.Groups[1].Value);
+ return $"@{mentionedRole.Name}";
+ });
+
+ // Custom emojis (<:name:id>)
+ content = Regex.Replace(content, "<(:.*?:)\\d*>", "$1");
+
+ return content;
+ }
+
+ private static string FormatMessageContentHtml(Message message)
+ {
+ var content = message.Content;
+
// Encode HTML
content = HtmlEncode(content);
@@ -248,9 +280,35 @@ namespace DiscordChatExporter.Services
// New lines
content = content.Replace("\n", "
");
+ // Meta mentions (@everyone)
+ content = content.Replace("@everyone", "
@everyone");
+
+ // Meta mentions (@here)
+ content = content.Replace("@here", "
@here");
+
+ // User mentions (<@id>)
+ content = Regex.Replace(content, "<@(\\d*)>",
+ m =>
+ {
+ var mentionedUser = message.MentionedUsers.First(u => u.Id == m.Groups[1].Value);
+ return $"
" +
+ $"@{HtmlEncode(mentionedUser.Name)}" +
+ "";
+ });
+
+ // Role mentions (<@&id>)
+ content = Regex.Replace(content, "<@&(\\d*)>",
+ m =>
+ {
+ var mentionedRole = message.MentionedRoles.First(r => r.Id == m.Groups[1].Value);
+ return "
" +
+ $"@{HtmlEncode(mentionedRole.Name)}" +
+ "";
+ });
+
// Custom emojis (<:name:id>)
- content = Regex.Replace(content, "<:.*?:(\\d+)>",
- "
");
+ content = Regex.Replace(content, "<(:.*?:)(\\d*)>",
+ "
");
return content;
}