Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/Elastic.Markdown/IO/MarkdownFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,19 @@ public string? NavigationTitle

private bool _instructionsParsed;

public MarkdownFile[] YieldParents()
{
var parents = new List<MarkdownFile>();
var parent = Parent;
do
{
if (parent is { Index: not null } && parent.Index != this)
parents.Add(parent.Index);
parent = parent?.Parent;
} while (parent != null);
return parents.ToArray();
}

public async Task<MarkdownDocument> MinimalParse(Cancel ctx)
{
var document = await MarkdownParser.MinimalParseAsync(SourceFile, ctx);
Expand Down
11 changes: 1 addition & 10 deletions src/Elastic.Markdown/Slices/_ViewModels.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,7 @@ public MarkdownFile[] Parents
if (_parents is not null)
return _parents;

var parents = new List<MarkdownFile>();
var current = CurrentDocument.Parent;
do
{
if (current is { Index: not null } && current.Index != CurrentDocument)
parents.Add(current.Index);
current = current?.Parent;
} while (current != null);

_parents = [.. parents];
_parents = [.. CurrentDocument.YieldParents()];
return _parents;
}
}
Expand Down
25 changes: 25 additions & 0 deletions tests/Elastic.Markdown.Tests/SiteMap/BreadCrumbTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information

using Elastic.Markdown.IO;
using FluentAssertions;
using Xunit.Abstractions;

namespace Elastic.Markdown.Tests.SiteMap;

public class BreadCrumbTests(ITestOutputHelper output) : NavigationTestsBase(output)
{
[Fact]
public void ParsesATableOfContents()
{
var doc = Generator.DocumentationSet.Files.FirstOrDefault(f => f.RelativePath == "testing/nested/index.md") as MarkdownFile;

doc.Should().NotBeNull();

doc!.Parent.Should().NotBeNull();

doc.YieldParents().Should().HaveCount(2);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I think another critical path worth testing is if the page has 0 parents. Nevertheless, thank you for adding a test!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

++ will follow up at some point!


}
}
Loading