From dd32f71ddc730d11c2b8b92ed28ea672fbb2e619 Mon Sep 17 00:00:00 2001 From: Steven Giesel Date: Sun, 30 Jun 2024 23:24:01 +0200 Subject: [PATCH] Fix links in header --- .../Features/MarkdownConverter.cs | 15 ++++++++------- .../Components/TableOfContentsTests.cs | 13 +++++++++++++ 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/LinkDotNet.Blog.Web/Features/MarkdownConverter.cs b/src/LinkDotNet.Blog.Web/Features/MarkdownConverter.cs index 9246f2bc..24704eac 100644 --- a/src/LinkDotNet.Blog.Web/Features/MarkdownConverter.cs +++ b/src/LinkDotNet.Blog.Web/Features/MarkdownConverter.cs @@ -3,6 +3,7 @@ using System.Text; using Markdig; using Markdig.Extensions.AutoIdentifiers; +using Markdig.Helpers; using Markdig.Renderers.Html; using Markdig.Syntax; using Markdig.Syntax.Inlines; @@ -51,14 +52,14 @@ private static string InlineToString(ContainerInline inline) var current = inline.FirstChild; while (current is not null) { - if (current is CodeInline cd) + var text = current switch { - sb.Append(cd.Content); - } - else - { - sb.Append(current); - } + CodeInline cd => cd.Content, + LinkInline link => link.FirstChild?.ToString(), + _ => current.ToString() + }; + + sb.Append(text); current = current.NextSibling; } diff --git a/tests/LinkDotNet.Blog.UnitTests/Web/Features/ShowBlogPost/Components/TableOfContentsTests.cs b/tests/LinkDotNet.Blog.UnitTests/Web/Features/ShowBlogPost/Components/TableOfContentsTests.cs index dc913700..ea8fed2b 100644 --- a/tests/LinkDotNet.Blog.UnitTests/Web/Features/ShowBlogPost/Components/TableOfContentsTests.cs +++ b/tests/LinkDotNet.Blog.UnitTests/Web/Features/ShowBlogPost/Components/TableOfContentsTests.cs @@ -65,4 +65,17 @@ public void ShouldCreateCorrectTocWithCodeInHeadings() var link = cut.Find("nav a"); link.TextContent.Should().Be("This is Header 1"); } + + [Fact] + public void ShouldCreateCorrectTocWithLinkInHeadings() + { + const string content = "# [This is a link](https://link.com)"; + + var cut = Render(p => p + .Add(x => x.Content, content) + .Add(x => x.CurrentUri, "https://localhost")); + + var link = cut.Find("nav a"); + link.TextContent.Should().Be("This is a link"); + } }