diff --git a/src/Elastic.Markdown/IO/DocumentationSet.cs b/src/Elastic.Markdown/IO/DocumentationSet.cs index aeae90851..5dc9886c4 100644 --- a/src/Elastic.Markdown/IO/DocumentationSet.cs +++ b/src/Elastic.Markdown/IO/DocumentationSet.cs @@ -189,6 +189,8 @@ public DocumentationSet( Tree = new TableOfContentsTree(Source, Context, lookups, treeCollector, ref fileIndex); + var navigationIndex = 0; + UpdateNavigationIndex(Tree.NavigationItems, ref navigationIndex); var markdownFiles = Files.OfType().ToArray(); var excludedChildren = markdownFiles.Where(f => !f.PartOfNavigation).ToArray(); @@ -210,6 +212,28 @@ public DocumentationSet( ValidateRedirectsExists(); } + private void UpdateNavigationIndex(IReadOnlyCollection navigationItems, ref int navigationIndex) + { + foreach (var item in navigationItems) + { + switch (item) + { + case FileNavigationItem fileNavigationItem: + var fileIndex = Interlocked.Increment(ref navigationIndex); + fileNavigationItem.NavigationIndex = fileIndex; + break; + case DocumentationGroup documentationGroup: + var groupIndex = Interlocked.Increment(ref navigationIndex); + documentationGroup.NavigationIndex = groupIndex; + UpdateNavigationIndex(documentationGroup.NavigationItems, ref navigationIndex); + break; + default: + Context.EmitError(Context.ConfigurationPath, $"Unhandled navigation item type: {item.GetType()}"); + break; + } + } + } + public FrozenDictionary NavigationIndexedByOrder { get; } private static IReadOnlyCollection CreateNavigationLookup(INavigationItem item) diff --git a/src/Elastic.Markdown/IO/Navigation/DocumentationGroup.cs b/src/Elastic.Markdown/IO/Navigation/DocumentationGroup.cs index fd7554c85..038b857be 100644 --- a/src/Elastic.Markdown/IO/Navigation/DocumentationGroup.cs +++ b/src/Elastic.Markdown/IO/Navigation/DocumentationGroup.cs @@ -99,7 +99,15 @@ out List navigationItems groups = []; navigationItems = []; files = []; + var fileReferences = lookups.TableOfContents.OfType().ToArray(); var indexFile = virtualIndexFile; + FileReference? indexReference = null; + if (indexFile is null) + { + indexReference = + fileReferences.FirstOrDefault(f => f.RelativePath.EndsWith("index.md")) + ?? fileReferences.FirstOrDefault(); + } var list = navigationItems; @@ -158,8 +166,10 @@ void AddToNavigationItems(INavigationItem item, ref int fileIndex) } files.Add(md); - if (file.RelativePath.EndsWith("index.md") && d is MarkdownFile i) - indexFile ??= i; + if (file.RelativePath.EndsWith("index.md")) + indexFile ??= md; + else if (indexReference == file) + indexFile ??= md; // Add the page to navigation items unless it's the index file // the index file can either be the discovered `index.md` or the parent group's diff --git a/tests/docs-assembler.Tests/src/docs-assembler.Tests/GlobalNavigationTests.cs b/tests/docs-assembler.Tests/src/docs-assembler.Tests/GlobalNavigationTests.cs index 7ea6727d8..12f072c5e 100644 --- a/tests/docs-assembler.Tests/src/docs-assembler.Tests/GlobalNavigationTests.cs +++ b/tests/docs-assembler.Tests/src/docs-assembler.Tests/GlobalNavigationTests.cs @@ -153,7 +153,8 @@ public async Task ParsesReferences() var navigation = new GlobalNavigation(assembleSources, navigationFile); var referenceNav = navigation.NavigationLookup[expectedRoot]; - navigation.NavigationItems.Should().HaveSameCount(navigation.NavigationLookup); + navigation.NavigationItems.OfType() + .Should().HaveSameCount(navigation.NavigationLookup); referenceNav.Should().NotBeNull(); var navigationLookup = referenceNav.NavigationItems.OfType().ToDictionary(i => i.Source, i => i); @@ -314,11 +315,11 @@ public async Task UriResolving() var resolvedUri = uriResolver.Resolve(new Uri("docs-content://reference/apm/something.md"), "/reference/apm/something"); resolvedUri.Should().Be("https://www.elastic.co/docs/reference/apm/something"); - resolvedUri = uriResolver.Resolve(new Uri("apm-agent-ios://reference/instrumentation.md"), "/reference/instrumentation"); - resolvedUri.Should().Be("https://www.elastic.co/docs/reference/apm/agents/ios/instrumentation"); + resolvedUri = uriResolver.Resolve(new Uri("apm-agent-nodejs://reference/instrumentation.md"), "/reference/instrumentation"); + resolvedUri.Should().Be("https://www.elastic.co/docs/reference/apm/agents/nodejs/instrumentation"); - resolvedUri = uriResolver.Resolve(new Uri("apm-agent-android://reference/a/file.md"), "/reference/a/file"); - resolvedUri.Should().Be("https://www.elastic.co/docs/reference/apm/agents/android/a/file"); + resolvedUri = uriResolver.Resolve(new Uri("apm-agent-dotnet://reference/a/file.md"), "/reference/a/file"); + resolvedUri.Should().Be("https://www.elastic.co/docs/reference/apm/agents/dotnet/a/file"); resolvedUri = uriResolver.Resolve(new Uri("elasticsearch-net://reference/b/file.md"), "/reference/b/file"); resolvedUri.Should().Be("https://www.elastic.co/docs/reference/elasticsearch/clients/dotnet/b/file");