104 lines
3.1 KiB
C#
104 lines
3.1 KiB
C#
|
using System.Collections.Generic;
|
||
|
using System.Linq;
|
||
|
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)
|
||
|
{
|
||
|
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.GetText()!;
|
||
|
|
||
|
string output = Regex.Replace(
|
||
|
content,
|
||
|
@"- \[\[(?<label>[^\]]+?)\]\](?<post>[^\n]+)\n",
|
||
|
match =>
|
||
|
{
|
||
|
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 = $"- [{label}{post}]({path})\n";
|
||
|
|
||
|
return value;
|
||
|
});
|
||
|
|
||
|
output = Regex.Replace(
|
||
|
output,
|
||
|
@"- \[(?<label>[^\]]+?)\]\((?<path>[^\)]+?)\)(?<post>[^\n]+)\n",
|
||
|
match =>
|
||
|
{
|
||
|
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 = $"- [{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 path1 = string.Format(
|
||
|
"/{0}/",
|
||
|
this.slugs.ToSlug(link1.Split('|').First()));
|
||
|
string label1 = link1.Split('|').Last();
|
||
|
|
||
|
return label1;
|
||
|
});
|
||
|
|
||
|
output = Regex.Replace(
|
||
|
output,
|
||
|
@"\[(?<label>[^\]]+)\]\([^)]+\)",
|
||
|
match => match.Groups["label"].ToString());
|
||
|
|
||
|
return output;
|
||
|
}
|
||
|
}
|