Skip to content

Commit e7418d4

Browse files
committed
stage
1 parent 33d65a5 commit e7418d4

File tree

4 files changed

+34
-25
lines changed

4 files changed

+34
-25
lines changed

src/Elastic.Documentation.Navigation/Isolated/DocumentationSetNavigation.cs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -393,10 +393,7 @@ int depth
393393
)
394394
{
395395
// tocRef.Path is now the FULL path (e.g., "guides/api" or "setup/advanced") after LoadAndResolve
396-
// Extract just the last segment (relative part) for the URL path prefix
397396
var fullTocPath = tocRef.Path;
398-
var lastSlashIndex = fullTocPath.LastIndexOf('/');
399-
var relativeTocPath = lastSlashIndex >= 0 ? fullTocPath[(lastSlashIndex + 1)..] : fullTocPath;
400397

401398
var tocDirectory = context.ReadFileSystem.DirectoryInfo.New(
402399
context.ReadFileSystem.Path.Combine(context.DocumentationSourceDirectory.FullName, fullTocPath)
@@ -405,9 +402,10 @@ int depth
405402
// TODO: Add validation for TOCs with children in parent YAML
406403
// This is a known limitation - TOCs should not have children defined in parent YAML
407404

408-
// Create a scoped path prefix for this TOC
409-
var parentPrefix = homeProvider.PathPrefix.TrimEnd('/');
410-
var scopedPathPrefix = string.IsNullOrEmpty(parentPrefix) ? $"/{relativeTocPath}" : $"{parentPrefix}/{relativeTocPath}";
405+
// According to url-building.md line 19-21: "We are not actually changing the PathPrefix,
406+
// we create the scope to be able to rehome during Assembler builds."
407+
// So TOC uses the SAME PathPrefix as its parent - it only creates a scope for rehoming
408+
var scopedPathPrefix = homeProvider.PathPrefix;
411409

412410
// Create the TOC navigation with empty children initially
413411
// We use null parent temporarily - we'll set it properly at the end using the public setter
@@ -425,20 +423,25 @@ int depth
425423
NavigationIndex = index
426424
};
427425

428-
// Convert children - pass tocNavigation as parent and as HomeProvider (TOC creates new scope)
426+
// Create a scoped HomeProvider for TOC children
427+
// According to url-building.md: "In isolated builds the NavigationRoot is always the DocumentationSetNavigation"
428+
// So we use the parent's NavigationRoot, not the TOC itself
429+
var tocHomeProvider = new NavigationHomeProvider(scopedPathPrefix, homeProvider.NavigationRoot);
430+
431+
// Convert children - pass tocNavigation as parent and tocHomeProvider as HomeProvider (TOC creates new scope)
429432
var children = new List<INavigationItem>();
430433
var childIndex = 0;
431434

432435
// LoadAndResolve has already resolved children from the toc.yml file and prepended full paths.
433-
// Children have full paths (e.g., "guides/api/reference.md"), so they should use tocNavigation as their HomeProvider
436+
// Children have full paths (e.g., "guides/api/reference.md"), so they should use the TOC's scoped HomeProvider
434437
foreach (var child in tocRef.Children)
435438
{
436439
var childNav = ConvertToNavigationItem(
437440
child,
438441
childIndex++,
439442
context,
440443
tocNavigation,
441-
tocNavigation, // TOC implements INavigationHomeProvider
444+
tocHomeProvider, // Use the scoped HomeProvider with correct NavigationRoot
442445
depth + 1
443446
);
444447

@@ -476,14 +479,11 @@ int depth
476479
NavigationIndex = index
477480
};
478481

479-
// Update children to point to the final TOC navigation
480-
// This includes both Parent and HomeProvider (via INavigationHomeAccessor)
482+
// Update children's Parent to point to the final TOC navigation
483+
// Note: We don't update HomeProvider here because children already have the correct tocHomeProvider
484+
// which provides the scoped PathPrefix and correct NavigationRoot
481485
foreach (var child in children)
482-
{
483486
child.Parent = finalTocNavigation;
484-
if (child is INavigationHomeAccessor accessor)
485-
accessor.HomeProvider = finalTocNavigation; // TOC implements INavigationHomeProvider
486-
}
487487

488488
return finalTocNavigation;
489489
}

src/Elastic.Documentation.Navigation/Isolated/TableOfContentsNavigation.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ public class TableOfContentsNavigation : IRootNavigationItem<IDocumentationFile,
1818
, INavigationHomeAccessor
1919
, INavigationHomeProvider
2020
{
21-
private INavigationHomeProvider? _homeProvider;
22-
2321
public TableOfContentsNavigation(
2422
IDirectoryInfo tableOfContentsDirectory,
2523
int depth,
@@ -33,7 +31,6 @@ Dictionary<Uri, INodeNavigationItem<IDocumentationFile, INavigationItem>> tocNod
3331
{
3432
TableOfContentsDirectory = tableOfContentsDirectory;
3533
NavigationItems = navigationItems;
36-
Index = this.FindIndex<IDocumentationFile>(new NotFoundModel($"{parentPath}/index.md"));
3734
Parent = parent;
3835
Hidden = false;
3936
IsUsingNavigationDropdown = false;
@@ -43,20 +40,31 @@ Dictionary<Uri, INodeNavigationItem<IDocumentationFile, INavigationItem>> tocNod
4340
ParentPath = parentPath;
4441
_pathPrefix = pathPrefix;
4542

43+
// Initialize _homeProvider to this - it will be updated in assembler builds if needed
44+
_homeProvider = this;
45+
4646
// Create an identifier for this TOC
4747
Identifier = new Uri($"{git.RepositoryName}://{parentPath}");
4848
_ = tocNodes.TryAdd(Identifier, this);
49+
50+
// FindIndex must be called after _homeProvider is set
51+
Index = this.FindIndex<IDocumentationFile>(new NotFoundModel($"{parentPath}/index.md"));
4952
}
5053

5154
private readonly string _pathPrefix;
5255

56+
/// <summary>
57+
/// Internal HomeProvider - defaults to this, but can be updated in assembler builds.
58+
/// </summary>
59+
private INavigationHomeProvider _homeProvider { get; set; }
60+
5361
/// <summary>
5462
/// The composed path prefix for this TOC, which is the parent's prefix + this TOC's parent path.
5563
/// This is used by children to build their URLs.
5664
/// Implements INavigationHomeProvider.PathPrefix
5765
/// When HomeProvider is set (during assembler), this returns the external provider's PathPrefix.
5866
/// </summary>
59-
public string PathPrefix => _homeProvider?.PathPrefix ?? _pathPrefix;
67+
public string PathPrefix => _homeProvider == this ? _pathPrefix : _homeProvider.PathPrefix;
6068

6169
/// <inheritdoc />
6270
public string Url => Index.Url;
@@ -69,7 +77,7 @@ Dictionary<Uri, INodeNavigationItem<IDocumentationFile, INavigationItem>> tocNod
6977
/// In assembler builds, this can be overridden via HomeProvider.
7078
/// This satisfies both INavigationItem.NavigationRoot and INavigationHomeProvider.NavigationRoot.
7179
/// </summary>
72-
public IRootNavigationItem<INavigationModel, INavigationItem> NavigationRoot => _homeProvider?.NavigationRoot ?? this;
80+
public IRootNavigationItem<INavigationModel, INavigationItem> NavigationRoot => _homeProvider == this ? this : _homeProvider.NavigationRoot;
7381

7482
/// <inheritdoc />
7583
public INodeNavigationItem<INavigationModel, INavigationItem>? Parent { get; set; }
@@ -81,7 +89,7 @@ Dictionary<Uri, INodeNavigationItem<IDocumentationFile, INavigationItem>> tocNod
8189
/// </summary>
8290
public INavigationHomeProvider HomeProvider
8391
{
84-
get => _homeProvider ?? this;
92+
get => _homeProvider;
8593
set => _homeProvider = value;
8694
}
8795

tests/Navigation.Tests/Isolation/ConstructorTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public void ConstructorCreatesFileNavigationLeafFromFileRef()
8585
fileNav.Url.Should().Be("/getting-started");
8686
fileNav.Hidden.Should().BeFalse();
8787
fileNav.NavigationRoot.Should().BeSameAs(navigation);
88-
fileNav.Parent.Should().BeNull();
88+
fileNav.Parent.Should().BeSameAs(navigation); // Top-level files have DocumentationSetNavigation as parent
8989
}
9090

9191
[Fact]

tests/Navigation.Tests/Isolation/NavigationStructureTests.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -339,12 +339,13 @@ void VisitNavigationItems(INavigationItem item)
339339
var gettingStarted = setupFolder.NavigationItems.First();
340340
gettingStarted.NavigationRoot.Should().BeSameAs(navigation);
341341

342-
// Items in advanced TOC should point to the TableOfContentsNavigation
342+
// According to url-building.md: "In isolated builds the NavigationRoot is always the DocumentationSetNavigation"
343+
// Items in advanced TOC should point to DocumentationSetNavigation, not the TOC
343344
var advancedToc = setupFolder.NavigationItems.ElementAt(1).Should().BeOfType<TableOfContentsNavigation>().Subject;
344-
advancedToc.NavigationRoot.Should().BeSameAs(advancedToc, "TableOfContentsNavigation should be its own root");
345+
advancedToc.NavigationRoot.Should().BeSameAs(advancedToc, "TableOfContentsNavigation is its own root when not rehomed");
345346

346347
var advancedIndex = advancedToc.NavigationItems.First();
347-
advancedIndex.NavigationRoot.Should().BeSameAs(advancedToc, "Items within TOC should point to the TOC as their root");
348+
advancedIndex.NavigationRoot.Should().BeSameAs(navigation, "Items within TOC should point to DocumentationSetNavigation in isolated builds");
348349

349350
// Items in file with children should point to DocumentationSetNavigation
350351
var guideFile = navigation.NavigationItems.ElementAt(2).Should().BeOfType<VirtualFileNavigation<TestDocumentationFile>>().Subject;

0 commit comments

Comments
 (0)