Parse emojis in markdown and render them using Twemoji

Closes #140
This commit is contained in:
Alexey Golub
2019-03-03 18:36:12 +02:00
parent a564906719
commit 28175e2110
4 changed files with 68 additions and 19 deletions

View File

@@ -1,10 +1,12 @@
using Tyrrrz.Extensions;
using System.Collections.Generic;
using System.Linq;
using Tyrrrz.Extensions;
namespace DiscordChatExporter.Core.Models
{
// https://discordapp.com/developers/docs/resources/emoji#emoji-object
public class Emoji
public partial class Emoji
{
public string Id { get; }
@@ -28,8 +30,7 @@ namespace DiscordChatExporter.Core.Models
}
// Standard unicode emoji (via twemoji)
var codePoint = char.ConvertToUtf32(Name, 0).ToString("x");
return $"https://twemoji.maxcdn.com/2/72x72/{codePoint}.png";
return $"https://twemoji.maxcdn.com/2/72x72/{GetTwemojiName(Name)}.png";
}
}
@@ -40,4 +41,16 @@ namespace DiscordChatExporter.Core.Models
IsAnimated = isAnimated;
}
}
public partial class Emoji
{
private static IEnumerable<int> GetCodePoints(string emoji)
{
for (var i = 0; i < emoji.Length; i += char.IsHighSurrogate(emoji[i]) ? 2 : 1)
yield return char.ConvertToUtf32(emoji, i);
}
private static string GetTwemojiName(string emoji)
=> GetCodePoints(emoji).Select(i => i.ToString("x")).JoinToString("-");
}
}

View File

@@ -201,12 +201,14 @@ namespace DiscordChatExporter.Core.Services
else if (node is EmojiNode emojiNode)
{
buffer.Append($"<img class=\"emoji\" title=\"{emojiNode.Name}\" src=\"https://cdn.discordapp.com/emojis/{emojiNode.Id}.png\" />");
var emoji = new Emoji(emojiNode.Id, emojiNode.Name, emojiNode.IsAnimated);
buffer.Append($"<img class=\"emoji\" title=\"{emoji.Name}\" src=\"{emoji.ImageUrl}\" />");
}
else if (node is LinkNode linkNode)
{
buffer.Append($"<a href=\"{Uri.EscapeUriString(linkNode.Url)}\">{linkNode.Title.HtmlEncode()}</a>");
var escapedUrl = Uri.EscapeUriString(linkNode.Url);
buffer.Append($"<a href=\"{escapedUrl}\">{linkNode.Title.HtmlEncode()}</a>");
}
}