Skip to content

Commit cffd2d7

Browse files
Copilotreakaleek
andcommitted
Fix heading order issue when snippets are included
- Modified GetAnchors method to preserve line number from include directive - Included snippet headings now appear at the correct position in TOC - Changed from assigning line 0 to using actual include block line number - This ensures right-side navigation respects document order Co-authored-by: reakaleek <[email protected]>
1 parent 20716b1 commit cffd2d7

File tree

2 files changed

+86
-4
lines changed

2 files changed

+86
-4
lines changed

src/Elastic.Markdown/IO/MarkdownFile.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -253,12 +253,16 @@ public static List<PageTocItem> GetAnchors(
253253
|| file is not SnippetFile snippet)
254254
return null;
255255

256-
return snippet.GetAnchors(set, parser, frontMatter);
256+
var anchors = snippet.GetAnchors(set, parser, frontMatter);
257+
return new { Block = i, Anchors = anchors };
257258
})
258259
.Where(i => i is not null)
259260
.ToArray();
260261

261-
var includedTocs = includes.SelectMany(i => i!.TableOfContentItems).ToArray();
262+
var includedTocs = includes
263+
.SelectMany(i => i!.Anchors!.TableOfContentItems
264+
.Select(item => new { TocItem = item, i.Block.Line }))
265+
.ToArray();
262266

263267
// Collect headings from standard markdown
264268
var headingTocs = document
@@ -308,7 +312,7 @@ public static List<PageTocItem> GetAnchors(
308312

309313
var toc = headingTocs
310314
.Concat(stepperTocs)
311-
.Concat(includedTocs.Select(item => new { TocItem = item, Line = 0 }))
315+
.Concat(includedTocs)
312316
.OrderBy(item => item.Line)
313317
.Select(item => item.TocItem)
314318
.Select(toc => subs.Count == 0
@@ -318,7 +322,7 @@ public static List<PageTocItem> GetAnchors(
318322
: toc)
319323
.ToList();
320324

321-
var includedAnchors = includes.SelectMany(i => i!.Anchors).ToArray();
325+
var includedAnchors = includes.SelectMany(i => i!.Anchors!.Anchors).ToArray();
322326
anchors =
323327
[
324328
..document.Descendants<DirectiveBlock>()
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Licensed to Elasticsearch B.V under one or more agreements.
2+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information
4+
5+
using System.IO.Abstractions.TestingHelpers;
6+
using Elastic.Markdown.IO;
7+
using Elastic.Markdown.Myst.Directives.Include;
8+
using Elastic.Markdown.Tests.Directives;
9+
using FluentAssertions;
10+
11+
namespace Elastic.Markdown.Tests.FileInclusion;
12+
13+
public class IncludeHeadingOrderTests(ITestOutputHelper output) : DirectiveTest<IncludeBlock>(output,
14+
"""
15+
## Check status, stop, and restart SLM
16+
### Get SLM status
17+
### Stop SLM
18+
### Start SLM
19+
## Check status, stop, and restart ILM
20+
21+
:::{include} _snippets/ilm-status.md
22+
:::
23+
"""
24+
)
25+
{
26+
protected override void AddToFileSystem(MockFileSystem fileSystem)
27+
{
28+
// language=markdown
29+
var inclusion = """
30+
### Get ILM status
31+
### Stop ILM
32+
### Start ILM
33+
""";
34+
fileSystem.AddFile(@"docs/_snippets/ilm-status.md", inclusion);
35+
}
36+
37+
[Fact]
38+
public void ParsesBlock() => Block.Should().NotBeNull();
39+
40+
[Fact]
41+
public void IncludesSnippetAfterMainContent() =>
42+
Html.Should().Contain("Get SLM status").And.Contain("Get ILM status");
43+
44+
[Fact]
45+
public void TableOfContentsRespectsOrder()
46+
{
47+
// Get the table of contents from the file
48+
var toc = File.PageTableOfContent;
49+
var headings = toc.Select(kvp => kvp.Value.Heading).ToList();
50+
51+
// The headings should appear in document order:
52+
// 1. Check status, stop, and restart SLM
53+
// 2. Get SLM status
54+
// 3. Stop SLM
55+
// 4. Start SLM
56+
// 5. Check status, stop, and restart ILM
57+
// 6. Get ILM status (from included snippet)
58+
// 7. Stop ILM (from included snippet)
59+
// 8. Start ILM (from included snippet)
60+
61+
headings.Should().HaveCount(8);
62+
63+
// Check the order is correct
64+
var expectedOrder = new[]
65+
{
66+
"Check status, stop, and restart SLM",
67+
"Get SLM status",
68+
"Stop SLM",
69+
"Start SLM",
70+
"Check status, stop, and restart ILM",
71+
"Get ILM status",
72+
"Stop ILM",
73+
"Start ILM"
74+
};
75+
76+
headings.Should().ContainInOrder(expectedOrder);
77+
}
78+
}

0 commit comments

Comments
 (0)