mfgames-cil/src/MfGames.Nitride.Markdown/MakeSingleLinkListItems.cs
D. Moonfire ca9f4867e6
All checks were successful
deploy / deploy (push) Successful in 43m31s
fix: tweaking input for make single list to handle star Markdown
2024-04-20 21:15:36 -05:00

102 lines
3.1 KiB
C#

using System.Text.RegularExpressions;
using MfGames.Gallium;
using MfGames.Nitride.Contents;
using MfGames.Nitride.Slugs;
namespace MfGames.Nitride.Markdown;
/// <summary>
/// An operation that turns list items that starts with a link into a single
/// link for the entire list item, even if there are additional links in the
/// list.
/// </summary>
public class MakeSingleLinkListItems : IOperation
{
private readonly ISlugConverter slugs;
public MakeSingleLinkListItems(ISlugConverter slugs)
{
this.slugs = slugs;
}
/// <inheritdoc />
public IEnumerable<Entity> Run(
IEnumerable<Entity> input,
CancellationToken cancellationToken = default
)
{
return input.SelectManyEntity<IsMarkdown>(x => x.Select(this.MakeSingleLinkLists));
}
/// <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 MakeSingleLinkLists(Entity entity)
{
string content = entity.GetTextContentString()!;
string output = Regex.Replace(
content,
@"(?<item>-|\*) \[\[(?<label>[^\]]+?)\]\](?<post>[^\n]+)\n",
match =>
{
string item = match.Groups["item"].ToString();
string wiki = match.Groups["label"].ToString();
string path = string.Format("/{0}/", this.slugs.ToSlug(wiki.Split('|').First()));
string label = wiki.Split('|').Last();
string after = match.Groups["post"].ToString();
string post = this.RemoveLinks(after);
string value = $"{item} [{label}{post}]({path})\n";
return value;
}
);
output = Regex.Replace(
output,
@"(?<item>-|\*) \[(?<label>[^\]]+?)\]\((?<path>[^\)]+?)\)(?<post>[^\n]+)\n",
match =>
{
string item = match.Groups["item"].ToString();
string label = match.Groups["label"].ToString();
string path = match.Groups["path"].ToString();
string after = match.Groups["post"].ToString();
string post = this.RemoveLinks(after);
string value = $"{item} [{label}{post}]({path})\n";
return value;
}
);
return entity.SetTextContent(output);
}
/// <summary>
/// Removes a normal link and replaces it with the text.
/// </summary>
private string RemoveLinks(string input)
{
string output = Regex.Replace(
input,
@"\[\[(?<label>[^\]]+)\]\]",
match =>
{
string link1 = match.Groups["label"].ToString();
string label1 = link1.Split('|').Last();
return label1;
}
);
output = Regex.Replace(
output,
@"\[(?<label>[^\]]+)\]\([^)]+\)",
match => match.Groups["label"].ToString()
);
return output;
}
}