Skip to content

Commit

Permalink
Don't render toc if there is nothing
Browse files Browse the repository at this point in the history
  • Loading branch information
linkdotnet committed Oct 15, 2024
1 parent 24dcf55 commit cb03125
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,26 +1,36 @@
<div class="accordion" id="tocAccordion">
<div class="accordion-item">
<h2 class="accordion-header" id="toc-header">
<button class="accordion-button collapsed d-flex align-items-center gap-2" type="button" data-bs-toggle="collapse" data-bs-target="#toc-accordion-body" aria-expanded="false" aria-controls="toc-accordion-body">
<i class="list"></i><div class="vr"></div><span>Table of Contents</span>
</button>
</h2>
<div id="toc-accordion-body" class="accordion-collapse collapse" aria-labelledby="toc-header">
<nav class="nav flex-column p-2">
@foreach (var item in MarkdownConverter.GenerateToc(Content))
{
@TocLink(item)
}
</nav>
@if (toc?.Count > 0)
{
<div class="accordion" id="tocAccordion">
<div class="accordion-item">
<h2 class="accordion-header" id="toc-header">
<button class="accordion-button collapsed d-flex align-items-center gap-2" type="button" data-bs-toggle="collapse" data-bs-target="#toc-accordion-body" aria-expanded="false" aria-controls="toc-accordion-body">
<i class="list"></i>
<div class="vr"></div><span>Table of Contents</span>
</button>
</h2>
<div id="toc-accordion-body" class="accordion-collapse collapse" aria-labelledby="toc-header">
<nav class="nav flex-column p-2">
@foreach (var item in MarkdownConverter.GenerateToc(Content))
{
@TocLink(item)
}
</nav>
</div>
</div>
</div>
</div>
}

@code {
[Parameter, EditorRequired] public required string CurrentUri { get; set; }

[Parameter, EditorRequired] public required string Content { get; set; }

private IReadOnlyCollection<TocItem>? toc;

protected override void OnParametersSet()
{
toc = MarkdownConverter.GenerateToc(Content);
}

private string GetAnchorUrl(string anchor)
{
if (CurrentUri.Contains('#'))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using AngleSharp.Dom;
using Blazorise;
using LinkDotNet.Blog.TestUtilities;
using LinkDotNet.Blog.Web.Features.ShowBlogPost.Components;

Expand Down Expand Up @@ -68,4 +69,14 @@ public void ShouldCreateCorrectToc(string markdown, string expectedToc)
var link = cut.Find("nav a");
link.TextContent.ShouldBe(expectedToc);
}

[Fact]
public void EmptyTocShouldNotBeDisplayed()
{
var cut = Render<TableOfContents>(p => p
.Add(x => x.Content, string.Empty)
.Add(x => x.CurrentUri, "https://localhost"));

cut.Markup.ShouldBeEmpty();
}
}

0 comments on commit cb03125

Please sign in to comment.