mirror of
https://github.com/Tyrrrz/DiscordChatExporter.git
synced 2026-04-20 13:10:29 +00:00
@@ -2,4 +2,7 @@
|
||||
|
||||
namespace DiscordChatExporter.Core.Markdown;
|
||||
|
||||
internal record FormattingNode(FormattingKind Kind, IReadOnlyList<MarkdownNode> Children) : MarkdownNode;
|
||||
internal record FormattingNode(
|
||||
FormattingKind Kind,
|
||||
IReadOnlyList<MarkdownNode> Children
|
||||
) : MarkdownNode, IContainerNode;
|
||||
8
DiscordChatExporter.Core/Markdown/IContainerNode.cs
Normal file
8
DiscordChatExporter.Core/Markdown/IContainerNode.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace DiscordChatExporter.Core.Markdown;
|
||||
|
||||
internal interface IContainerNode
|
||||
{
|
||||
IReadOnlyList<MarkdownNode> Children { get; }
|
||||
}
|
||||
@@ -2,9 +2,10 @@
|
||||
|
||||
namespace DiscordChatExporter.Core.Markdown;
|
||||
|
||||
// Named links can contain child nodes (e.g. [**bold URL**](https://test.com))
|
||||
internal record LinkNode(
|
||||
string Url,
|
||||
IReadOnlyList<MarkdownNode> Children) : MarkdownNode
|
||||
IReadOnlyList<MarkdownNode> Children) : MarkdownNode, IContainerNode
|
||||
{
|
||||
public LinkNode(string url)
|
||||
: this(url, new[] { new TextNode(url) })
|
||||
|
||||
@@ -385,12 +385,32 @@ internal static partial class MarkdownParser
|
||||
private static IReadOnlyList<MarkdownNode> Parse(StringSegment segment) =>
|
||||
Parse(segment, AggregateNodeMatcher);
|
||||
|
||||
public static IReadOnlyList<MarkdownNode> Parse(string markdown) =>
|
||||
Parse(new StringSegment(markdown));
|
||||
|
||||
private static IReadOnlyList<MarkdownNode> ParseMinimal(StringSegment segment) =>
|
||||
Parse(segment, MinimalAggregateNodeMatcher);
|
||||
|
||||
public static IReadOnlyList<MarkdownNode> Parse(string input) =>
|
||||
Parse(new StringSegment(input));
|
||||
public static IReadOnlyList<MarkdownNode> ParseMinimal(string markdown) =>
|
||||
ParseMinimal(new StringSegment(markdown));
|
||||
|
||||
public static IReadOnlyList<MarkdownNode> ParseMinimal(string input) =>
|
||||
ParseMinimal(new StringSegment(input));
|
||||
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