50 lines
1.3 KiB
C#
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);
|
|
}
|
|
}
|