Skip to content

Commit c186837

Browse files
authored
Fix TableOfContentsTree locating index (#1588)
* Fix TableOfContentsTree locating index * simplify if check
1 parent f08a1ec commit c186837

File tree

3 files changed

+42
-7
lines changed

3 files changed

+42
-7
lines changed

src/Elastic.Markdown/IO/DocumentationSet.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ public DocumentationSet(
189189

190190
Tree = new TableOfContentsTree(Source, Context, lookups, treeCollector, ref fileIndex);
191191

192+
var navigationIndex = 0;
193+
UpdateNavigationIndex(Tree.NavigationItems, ref navigationIndex);
192194
var markdownFiles = Files.OfType<MarkdownFile>().ToArray();
193195

194196
var excludedChildren = markdownFiles.Where(f => !f.PartOfNavigation).ToArray();
@@ -210,6 +212,28 @@ public DocumentationSet(
210212
ValidateRedirectsExists();
211213
}
212214

215+
private void UpdateNavigationIndex(IReadOnlyCollection<INavigationItem> navigationItems, ref int navigationIndex)
216+
{
217+
foreach (var item in navigationItems)
218+
{
219+
switch (item)
220+
{
221+
case FileNavigationItem fileNavigationItem:
222+
var fileIndex = Interlocked.Increment(ref navigationIndex);
223+
fileNavigationItem.NavigationIndex = fileIndex;
224+
break;
225+
case DocumentationGroup documentationGroup:
226+
var groupIndex = Interlocked.Increment(ref navigationIndex);
227+
documentationGroup.NavigationIndex = groupIndex;
228+
UpdateNavigationIndex(documentationGroup.NavigationItems, ref navigationIndex);
229+
break;
230+
default:
231+
Context.EmitError(Context.ConfigurationPath, $"Unhandled navigation item type: {item.GetType()}");
232+
break;
233+
}
234+
}
235+
}
236+
213237
public FrozenDictionary<int, INavigationItem> NavigationIndexedByOrder { get; }
214238

215239
private static IReadOnlyCollection<INavigationItem> CreateNavigationLookup(INavigationItem item)

src/Elastic.Markdown/IO/Navigation/DocumentationGroup.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,15 @@ out List<INavigationItem> navigationItems
9999
groups = [];
100100
navigationItems = [];
101101
files = [];
102+
var fileReferences = lookups.TableOfContents.OfType<FileReference>().ToArray();
102103
var indexFile = virtualIndexFile;
104+
FileReference? indexReference = null;
105+
if (indexFile is null)
106+
{
107+
indexReference =
108+
fileReferences.FirstOrDefault(f => f.RelativePath.EndsWith("index.md"))
109+
?? fileReferences.FirstOrDefault();
110+
}
103111

104112
var list = navigationItems;
105113

@@ -158,8 +166,10 @@ void AddToNavigationItems(INavigationItem item, ref int fileIndex)
158166
}
159167

160168
files.Add(md);
161-
if (file.RelativePath.EndsWith("index.md") && d is MarkdownFile i)
162-
indexFile ??= i;
169+
if (file.RelativePath.EndsWith("index.md"))
170+
indexFile ??= md;
171+
else if (indexReference == file)
172+
indexFile ??= md;
163173

164174
// Add the page to navigation items unless it's the index file
165175
// the index file can either be the discovered `index.md` or the parent group's

tests/docs-assembler.Tests/src/docs-assembler.Tests/GlobalNavigationTests.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,8 @@ public async Task ParsesReferences()
153153

154154
var navigation = new GlobalNavigation(assembleSources, navigationFile);
155155
var referenceNav = navigation.NavigationLookup[expectedRoot];
156-
navigation.NavigationItems.Should().HaveSameCount(navigation.NavigationLookup);
156+
navigation.NavigationItems.OfType<TableOfContentsTree>()
157+
.Should().HaveSameCount(navigation.NavigationLookup);
157158

158159
referenceNav.Should().NotBeNull();
159160
var navigationLookup = referenceNav.NavigationItems.OfType<TableOfContentsTree>().ToDictionary(i => i.Source, i => i);
@@ -314,11 +315,11 @@ public async Task UriResolving()
314315
var resolvedUri = uriResolver.Resolve(new Uri("docs-content://reference/apm/something.md"), "/reference/apm/something");
315316
resolvedUri.Should().Be("https://www.elastic.co/docs/reference/apm/something");
316317

317-
resolvedUri = uriResolver.Resolve(new Uri("apm-agent-ios://reference/instrumentation.md"), "/reference/instrumentation");
318-
resolvedUri.Should().Be("https://www.elastic.co/docs/reference/apm/agents/ios/instrumentation");
318+
resolvedUri = uriResolver.Resolve(new Uri("apm-agent-nodejs://reference/instrumentation.md"), "/reference/instrumentation");
319+
resolvedUri.Should().Be("https://www.elastic.co/docs/reference/apm/agents/nodejs/instrumentation");
319320

320-
resolvedUri = uriResolver.Resolve(new Uri("apm-agent-android://reference/a/file.md"), "/reference/a/file");
321-
resolvedUri.Should().Be("https://www.elastic.co/docs/reference/apm/agents/android/a/file");
321+
resolvedUri = uriResolver.Resolve(new Uri("apm-agent-dotnet://reference/a/file.md"), "/reference/a/file");
322+
resolvedUri.Should().Be("https://www.elastic.co/docs/reference/apm/agents/dotnet/a/file");
322323

323324
resolvedUri = uriResolver.Resolve(new Uri("elasticsearch-net://reference/b/file.md"), "/reference/b/file");
324325
resolvedUri.Should().Be("https://www.elastic.co/docs/reference/elasticsearch/clients/dotnet/b/file");

0 commit comments

Comments
 (0)