From 982e3b38c35e27ca2ec66b9032d99e6233f9e6d1 Mon Sep 17 00:00:00 2001 From: "D. Moonfire" Date: Sat, 24 Dec 2022 00:38:24 -0600 Subject: [PATCH] feat(markdown): implemented expanding single link list items --- .../MakeSingleLinkListItems.cs | 103 ++++++++++++++++++ .../MfGames.Nitride.Markdown.csproj | 1 + .../MakeSingleLinkListItemsTests.cs | 100 +++++++++++++++++ .../MarkdownTestContext.cs | 22 ++++ 4 files changed, 226 insertions(+) create mode 100644 src/MfGames.Nitride.Markdown/MakeSingleLinkListItems.cs create mode 100644 tests/MfGames.Nitride.Markdown.Tests/MakeSingleLinkListItemsTests.cs create mode 100644 tests/MfGames.Nitride.Markdown.Tests/MarkdownTestContext.cs diff --git a/src/MfGames.Nitride.Markdown/MakeSingleLinkListItems.cs b/src/MfGames.Nitride.Markdown/MakeSingleLinkListItems.cs new file mode 100644 index 0000000..85ab750 --- /dev/null +++ b/src/MfGames.Nitride.Markdown/MakeSingleLinkListItems.cs @@ -0,0 +1,103 @@ +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; + +/// +/// 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. +/// +public class MakeSingleLinkListItems : IOperation +{ + private readonly ISlugConverter slugs; + + public MakeSingleLinkListItems(ISlugConverter slugs) + { + this.slugs = slugs; + } + + /// + public IEnumerable Run(IEnumerable input) + { + return input + .SelectManyEntity( + x => x + .Select(this.MakeSingleLinkLists)); + } + + /// + /// 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. + /// + private Entity MakeSingleLinkLists(Entity entity) + { + string content = entity.GetText()!; + + string output = Regex.Replace( + content, + @"- \[\[(?