Skip to content

Commit

Permalink
Find every HeadingBlock in a document (#349)
Browse files Browse the repository at this point in the history
* Find every HeadingBlock in a document

* Add test
  • Loading branch information
reakaleek authored Jan 27, 2025
1 parent 3c0da5f commit 20a79c9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Elastic.Markdown/IO/MarkdownFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ private void ReadDocumentInstructions(MarkdownDocument document)
}

var contents = document
.Where(block => block is HeadingBlock { Level: >= 2 })
.Cast<HeadingBlock>()
.Descendants<HeadingBlock>()
.Where(block => block is { Level: >= 2 })
.Select(h => (h.GetData("header") as string, h.GetData("anchor") as string))
.Select(h => new PageTocItem
{
Expand Down
25 changes: 25 additions & 0 deletions tests/Elastic.Markdown.Tests/Inline/AnchorLinkTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ protected override void AddToFileSystem(MockFileSystem fileSystem)
## New Requirements [#new-reqs]
:::{dropdown} Nested heading
##### Heading inside dropdown [#heading-inside-dropdown]
:::
These are new requirements
""";
fileSystem.AddFile(@"docs/testing/req.md", inclusion);
Expand Down Expand Up @@ -147,3 +154,21 @@ public void GeneratesHtml() =>
public void HasError() => Collector.Diagnostics.Should().HaveCount(1)
.And.Contain(d => d.Message.Contains("`sub-requirements2` does not exist"));
}


public class NestedHeadingTest(ITestOutputHelper output) : AnchorLinkTestBase(output,
"""
[Heading inside dropdown](testing/req.md#heading-inside-dropdown)
"""
)
{
[Fact]
public void GeneratesHtml() =>
// language=html
Html.Should().Contain(
"""<a href="testing/req.html#heading-inside-dropdown">Heading inside dropdown</a>"""
);

[Fact]
public void HasError() => Collector.Diagnostics.Should().HaveCount(0);
}

0 comments on commit 20a79c9

Please sign in to comment.