mirror of
https://github.com/Tyrrrz/DiscordChatExporter.git
synced 2026-03-17 20:32:34 +00:00
Include inline emoji in JSON export (#1311)
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
using DiscordChatExporter.Core.Discord;
|
||||
using DiscordChatExporter.Core.Utils;
|
||||
using DiscordChatExporter.Core.Discord.Data;
|
||||
|
||||
namespace DiscordChatExporter.Core.Markdown;
|
||||
|
||||
@@ -11,11 +11,17 @@ internal record EmojiNode(
|
||||
bool IsAnimated
|
||||
) : MarkdownNode
|
||||
{
|
||||
public bool IsCustomEmoji => Id is not null;
|
||||
|
||||
// Name of a custom emoji (e.g. LUL) or name of a standard emoji (e.g. slight_smile)
|
||||
public string Code => IsCustomEmoji ? Name : EmojiIndex.TryGetCode(Name) ?? Name;
|
||||
// This coupling is unsound from the domain-design perspective, but it helps us reuse
|
||||
// some code for now. We can refactor this later, if the coupling becomes a problem.
|
||||
private readonly Emoji _emoji = new(Id, Name, IsAnimated);
|
||||
|
||||
public EmojiNode(string name)
|
||||
: this(null, name, false) { }
|
||||
|
||||
public bool IsCustomEmoji => _emoji.IsCustomEmoji;
|
||||
|
||||
// Name of a custom emoji (e.g. LUL) or name of a standard emoji (e.g. slight_smile)
|
||||
public string Code => _emoji.Code;
|
||||
|
||||
public string ImageUrl => _emoji.ImageUrl;
|
||||
}
|
||||
|
||||
@@ -484,6 +484,37 @@ internal static partial class MarkdownParser
|
||||
|
||||
internal static partial class MarkdownParser
|
||||
{
|
||||
private static void Extract<TNode>(
|
||||
IEnumerable<MarkdownNode> nodes,
|
||||
ICollection<TNode> extractedNodes
|
||||
)
|
||||
where TNode : MarkdownNode
|
||||
{
|
||||
foreach (var node in nodes)
|
||||
{
|
||||
if (node is TNode extractedNode)
|
||||
extractedNodes.Add(extractedNode);
|
||||
|
||||
if (node is IContainerNode containerNode)
|
||||
Extract(containerNode.Children, extractedNodes);
|
||||
}
|
||||
}
|
||||
|
||||
public static IReadOnlyList<TNode> Extract<TNode>(string markdown)
|
||||
where TNode : MarkdownNode
|
||||
{
|
||||
var extractedNodes = new List<TNode>();
|
||||
Extract(Parse(markdown), extractedNodes);
|
||||
|
||||
return extractedNodes;
|
||||
}
|
||||
|
||||
public static IReadOnlyList<LinkNode> ExtractLinks(string markdown) =>
|
||||
Extract<LinkNode>(markdown);
|
||||
|
||||
public static IReadOnlyList<EmojiNode> ExtractEmojis(string markdown) =>
|
||||
Extract<EmojiNode>(markdown);
|
||||
|
||||
private static IReadOnlyList<MarkdownNode> Parse(
|
||||
MarkdownContext context,
|
||||
StringSegment segment
|
||||
@@ -499,24 +530,4 @@ internal static partial class MarkdownParser
|
||||
|
||||
public static IReadOnlyList<MarkdownNode> ParseMinimal(string markdown) =>
|
||||
ParseMinimal(new MarkdownContext(), new StringSegment(markdown));
|
||||
|
||||
private static void ExtractLinks(IEnumerable<MarkdownNode> nodes, ICollection<LinkNode> links)
|
||||
{
|
||||
foreach (var node in nodes)
|
||||
{
|
||||
if (node is LinkNode linkNode)
|
||||
links.Add(linkNode);
|
||||
|
||||
if (node is IContainerNode containerNode)
|
||||
ExtractLinks(containerNode.Children, links);
|
||||
}
|
||||
}
|
||||
|
||||
public static IReadOnlyList<LinkNode> ExtractLinks(string markdown)
|
||||
{
|
||||
var links = new List<LinkNode>();
|
||||
ExtractLinks(Parse(markdown), links);
|
||||
|
||||
return links;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user