|
3 | 3 | // See the LICENSE file in the project root for more information
|
4 | 4 |
|
5 | 5 | using System.Collections.Concurrent;
|
| 6 | +using Elastic.Documentation.Diagnostics; |
6 | 7 | using Elastic.Documentation.Site.Navigation;
|
7 | 8 | using Elastic.Markdown.IO.Navigation;
|
8 | 9 | using Microsoft.Extensions.Logging;
|
9 | 10 |
|
10 | 11 | namespace Documentation.Assembler.Navigation;
|
11 | 12 |
|
12 |
| -public class GlobalNavigationHtmlWriter(ILoggerFactory logFactory, GlobalNavigation globalNavigation) : INavigationHtmlWriter |
| 13 | +public class GlobalNavigationHtmlWriter(ILoggerFactory logFactory, GlobalNavigation globalNavigation, DiagnosticsCollector collector) : INavigationHtmlWriter |
13 | 14 | {
|
14 | 15 | private readonly ILogger<Program> _logger = logFactory.CreateLogger<Program>();
|
15 | 16 |
|
16 | 17 | private readonly ConcurrentDictionary<(string, int), string> _renderedNavigationCache = [];
|
17 | 18 |
|
18 | 19 | public async Task<NavigationRenderResult> RenderNavigation(IRootNavigationItem<INavigationModel, INavigationItem> currentRootNavigation, int maxLevel, Cancel ctx = default)
|
19 | 20 | {
|
20 |
| - INodeNavigationItem<INavigationModel, INavigationItem> lastParentBeforeRoot = currentRootNavigation; |
21 |
| - INodeNavigationItem<INavigationModel, INavigationItem> parent = currentRootNavigation; |
22 |
| - while (parent.Parent is not null) |
23 |
| - { |
24 |
| - lastParentBeforeRoot = parent; |
25 |
| - parent = parent.Parent; |
26 |
| - } |
27 |
| - if (_renderedNavigationCache.TryGetValue((lastParentBeforeRoot.Id, maxLevel), out var html)) |
| 21 | + if (currentRootNavigation.Parent is not null && currentRootNavigation.Parent.Depth != 0) |
| 22 | + collector.EmitGlobalError($"Passed root is not actually a top level navigation item {currentRootNavigation.NavigationTitle} ({currentRootNavigation.Id}) in {currentRootNavigation.Url}"); |
| 23 | + |
| 24 | + if (_renderedNavigationCache.TryGetValue((currentRootNavigation.Id, maxLevel), out var html)) |
28 | 25 | {
|
29 | 26 | return new NavigationRenderResult
|
30 | 27 | {
|
31 | 28 | Html = html,
|
32 |
| - Id = lastParentBeforeRoot.Id |
| 29 | + Id = currentRootNavigation.Id |
33 | 30 | };
|
34 | 31 | }
|
35 | 32 |
|
36 |
| - _logger.LogInformation("Rendering navigation for {NavigationTitle} ({Id})", lastParentBeforeRoot.NavigationTitle, lastParentBeforeRoot.Id); |
| 33 | + _logger.LogInformation("Rendering navigation for {NavigationTitle} ({Id})", currentRootNavigation.NavigationTitle, currentRootNavigation.Id); |
37 | 34 |
|
38 |
| - if (lastParentBeforeRoot is not DocumentationGroup group) |
| 35 | + if (currentRootNavigation is not DocumentationGroup group) |
39 | 36 | return NavigationRenderResult.Empty;
|
40 | 37 |
|
41 | 38 | var model = CreateNavigationModel(group, maxLevel);
|
42 | 39 | html = await ((INavigationHtmlWriter)this).Render(model, ctx);
|
43 |
| - _renderedNavigationCache[(lastParentBeforeRoot.Id, maxLevel)] = html; |
| 40 | + _renderedNavigationCache[(currentRootNavigation.Id, maxLevel)] = html; |
44 | 41 | return new NavigationRenderResult
|
45 | 42 | {
|
46 | 43 | Html = html,
|
47 |
| - Id = lastParentBeforeRoot.Id |
| 44 | + Id = currentRootNavigation.Id |
48 | 45 | };
|
49 | 46 | }
|
50 | 47 |
|
|
0 commit comments