mfgames-cil/src/MfGames.Nitride.Markdown/RewriteMarkdownLink.cs
D. Moonfire 23a65c8674
All checks were successful
deploy / deploy (push) Successful in 41m4s
feat: added a Markdown transformer to convert links to index paths
2024-03-18 22:27:42 -05:00

50 lines
1.3 KiB
C#

using Markdig.Syntax.Inlines;
using MfGames.Gallium;
using MfGames.Markdown;
using MfGames.Nitride.Contents;
using MfGames.Nitride.Generators;
namespace MfGames.Nitride.Markdown;
/// <summary>
/// An operation that turns rewrites Markdown links with a variable callback.
/// </summary>
[WithProperties]
public partial class RewriteMarkdownLink : IOperation
{
private readonly RewriteLinkTransformer transformer;
public RewriteMarkdownLink()
{
this.transformer = new RewriteLinkTransformer();
}
public Action<LinkInline>? OnLinkCallback
{
get => this.transformer.OnLink;
set => this.transformer.OnLink = value;
}
/// <inheritdoc />
public IEnumerable<Entity> Run(
IEnumerable<Entity> input,
CancellationToken cancellationToken = default
)
{
return input.SelectManyEntity<IsMarkdown>(x => x.Select(this.Transform));
}
/// <summary>
/// This turns all links that start with a link into a single link while
/// removing all trailing links within the line. This is to simplify the
/// rendering of the link on page.
/// </summary>
private Entity Transform(Entity entity)
{
string input = entity.GetTextContentString()!;
string output = this.transformer.Transform(input)!;
return entity.SetTextContent(output);
}
}