Skip to content

Commit b424f49

Browse files
committed
ensure global layout is seeded uniformly
1 parent 2eb6d53 commit b424f49

File tree

12 files changed

+45
-82
lines changed

12 files changed

+45
-82
lines changed

src/Elastic.ApiExplorer/ApiViewModel.cs

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,43 @@
33
// See the LICENSE file in the project root for more information
44

55
using Elastic.Documentation;
6+
using Elastic.Documentation.Configuration;
7+
using Elastic.Documentation.Configuration.Assembler;
8+
using Elastic.Documentation.Configuration.Builder;
9+
using Elastic.Documentation.Site;
610
using Elastic.Documentation.Site.FileProviders;
711
using Elastic.Documentation.Site.Navigation;
812
using Microsoft.AspNetCore.Html;
913

1014
namespace Elastic.ApiExplorer;
1115

12-
public abstract class ApiViewModel
16+
public abstract class ApiViewModel(ApiRenderContext context)
1317
{
14-
public required string NavigationHtml { get; init; }
15-
public required StaticFileContentHashProvider StaticFileContentHashProvider { get; init; }
16-
public required INavigationItem CurrentNavigationItem { get; init; }
17-
public required IMarkdownStringRenderer MarkdownRenderer { get; init; }
18+
public string NavigationHtml { get; } = context.NavigationHtml;
19+
public StaticFileContentHashProvider StaticFileContentHashProvider { get; } = context.StaticFileContentHashProvider;
20+
public INavigationItem CurrentNavigationItem { get; } = context.CurrentNavigation;
21+
public IMarkdownStringRenderer MarkdownRenderer { get; } = context.MarkdownRenderer;
22+
public BuildContext BuildContext { get; } = context.BuildContext;
1823

1924

2025
public HtmlString RenderMarkdown(string? markdown) =>
21-
string.IsNullOrEmpty(markdown) ? new(string.Empty) : new(MarkdownRenderer.Render(markdown, null));
26+
new(string.IsNullOrEmpty(markdown) ? string.Empty : MarkdownRenderer.Render(markdown, null));
27+
28+
29+
public GlobalLayoutViewModel CreateGlobalLayoutModel() =>
30+
new()
31+
{
32+
DocSetName = "Api Explorer",
33+
Description = "",
34+
CurrentNavigationItem = CurrentNavigationItem,
35+
Previous = null,
36+
Next = null,
37+
NavigationHtml = NavigationHtml,
38+
UrlPathPrefix = BuildContext.UrlPathPrefix,
39+
AllowIndexing = BuildContext.AllowIndexing,
40+
CanonicalBaseUrl = BuildContext.CanonicalBaseUrl,
41+
GoogleTagManager = new GoogleTagManagerConfiguration(),
42+
Features = new FeatureFlags([]),
43+
StaticFileContentHashProvider = StaticFileContentHashProvider
44+
};
2245
}

src/Elastic.ApiExplorer/Endpoints/ApiEndpoint.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,14 @@ public ApiEndpoint(string route, IOpenApiPathItem openApiPath)
1818
}
1919

2020
public string Route { get; }
21+
2122
public IOpenApiPathItem OpenApiPath { get; }
2223

2324
public async Task RenderAsync(FileSystemStream stream, ApiRenderContext context, Cancel ctx = default)
2425
{
25-
var viewModel = new IndexViewModel
26+
var viewModel = new IndexViewModel(context)
2627
{
27-
ApiEndpoint = this,
28-
StaticFileContentHashProvider = context.StaticFileContentHashProvider,
29-
NavigationHtml = context.NavigationHtml,
30-
CurrentNavigationItem = context.CurrentNavigation,
31-
MarkdownRenderer = context.MarkdownRenderer
32-
28+
ApiEndpoint = this
3329
};
3430
var slice = EndpointView.Create(viewModel);
3531
await slice.RenderAsync(stream, cancellationToken: ctx);

src/Elastic.ApiExplorer/Endpoints/EndpointView.cshtml

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,7 @@
1-
@using Elastic.Documentation.Configuration.Assembler
2-
@using Elastic.Documentation.Configuration.Builder
31
@inherits RazorSliceHttpResult<IndexViewModel>
42
@implements IUsesLayout<Elastic.ApiExplorer._Layout, GlobalLayoutViewModel>
53
@functions {
6-
public GlobalLayoutViewModel LayoutModel => new()
7-
{
8-
DocSetName = "Api Explorer",
9-
Description = "",
10-
CurrentNavigationItem = Model.CurrentNavigationItem,
11-
Previous = null,
12-
Next = null,
13-
NavigationHtml = Model.NavigationHtml,
14-
UrlPathPrefix = null,
15-
AllowIndexing = false,
16-
CanonicalBaseUrl = null,
17-
GoogleTagManager = new GoogleTagManagerConfiguration(),
18-
Features = new FeatureFlags([]),
19-
StaticFileContentHashProvider = Model.StaticFileContentHashProvider
20-
};
4+
public GlobalLayoutViewModel LayoutModel => Model.CreateGlobalLayoutModel();
215
}
226
<section id="elastic-docs-v3">
237
<h1>@Model.CurrentNavigationItem.Url</h1>

src/Elastic.ApiExplorer/Endpoints/IndexViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace Elastic.ApiExplorer.Endpoints;
99

10-
public class IndexViewModel : ApiViewModel
10+
public class IndexViewModel(ApiRenderContext context) : ApiViewModel(context)
1111
{
1212
public required ApiEndpoint ApiEndpoint { get; init; }
1313
}

src/Elastic.ApiExplorer/Landing/LandingNavigationItem.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,10 @@ public class ApiLanding : IApiGroupingModel
1414
{
1515
public async Task RenderAsync(FileSystemStream stream, ApiRenderContext context, Cancel ctx = default)
1616
{
17-
var viewModel = new LandingViewModel
17+
var viewModel = new LandingViewModel(context)
1818
{
1919
Landing = this,
20-
StaticFileContentHashProvider = context.StaticFileContentHashProvider,
21-
NavigationHtml = context.NavigationHtml,
22-
ApiInfo = context.Model.Info,
23-
CurrentNavigationItem = context.CurrentNavigation,
24-
MarkdownRenderer = context.MarkdownRenderer
20+
ApiInfo = context.Model.Info
2521
};
2622
var slice = LandingView.Create(viewModel);
2723
await slice.RenderAsync(stream, cancellationToken: ctx);

src/Elastic.ApiExplorer/Landing/LandingView.cshtml

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,7 @@
1-
@using Elastic.Documentation.Configuration.Assembler
2-
@using Elastic.Documentation.Configuration.Builder
31
@inherits RazorSliceHttpResult<Elastic.ApiExplorer.Landing.LandingViewModel>
42
@implements IUsesLayout<Elastic.ApiExplorer._Layout, GlobalLayoutViewModel>
53
@functions {
6-
public GlobalLayoutViewModel LayoutModel => new()
7-
{
8-
DocSetName = "Api Explorer",
9-
Description = "",
10-
CurrentNavigationItem = Model.CurrentNavigationItem,
11-
Previous = null,
12-
Next = null,
13-
NavigationHtml = Model.NavigationHtml,
14-
UrlPathPrefix = null,
15-
AllowIndexing = false,
16-
CanonicalBaseUrl = null,
17-
GoogleTagManager = new GoogleTagManagerConfiguration(),
18-
Features = new FeatureFlags([]),
19-
StaticFileContentHashProvider = Model.StaticFileContentHashProvider
20-
};
4+
public GlobalLayoutViewModel LayoutModel => Model.CreateGlobalLayoutModel();
215
}
226
<section id="elastic-docs-v3">
237
<h1>@Model.ApiInfo.Title</h1>

src/Elastic.ApiExplorer/Landing/LandingViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace Elastic.ApiExplorer.Landing;
88

9-
public class LandingViewModel : ApiViewModel
9+
public class LandingViewModel(ApiRenderContext context) : ApiViewModel(context)
1010
{
1111
public required ApiLanding Landing { get; init; }
1212
public required OpenApiInfo ApiInfo { get; init; }

src/Elastic.ApiExplorer/Operations/OperationNavigationItem.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,9 @@ public record ApiOperation(OperationType OperationType, OpenApiOperation Operati
1616
{
1717
public async Task RenderAsync(FileSystemStream stream, ApiRenderContext context, Cancel ctx = default)
1818
{
19-
var viewModel = new OperationViewModel
19+
var viewModel = new OperationViewModel(context)
2020
{
21-
Operation = this,
22-
StaticFileContentHashProvider = context.StaticFileContentHashProvider,
23-
NavigationHtml = context.NavigationHtml,
24-
CurrentNavigationItem = context.CurrentNavigation,
25-
MarkdownRenderer = context.MarkdownRenderer
21+
Operation = this
2622
};
2723
var slice = OperationView.Create(viewModel);
2824
await slice.RenderAsync(stream, cancellationToken: ctx);

src/Elastic.ApiExplorer/Operations/OperationView.cshtml

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,10 @@
11
@using Elastic.ApiExplorer.Landing
22
@using Elastic.ApiExplorer.Operations
3-
@using Elastic.Documentation.Configuration.Assembler
4-
@using Elastic.Documentation.Configuration.Builder
53
@using Microsoft.OpenApi.Models
64
@inherits RazorSliceHttpResult<Elastic.ApiExplorer.Operations.OperationViewModel>
75
@implements IUsesLayout<Elastic.ApiExplorer._Layout, GlobalLayoutViewModel>
86
@functions {
9-
public GlobalLayoutViewModel LayoutModel => new()
10-
{
11-
DocSetName = "Api Explorer",
12-
Description = "",
13-
CurrentNavigationItem = Model.CurrentNavigationItem,
14-
Previous = null,
15-
Next = null,
16-
NavigationHtml = Model.NavigationHtml,
17-
UrlPathPrefix = null,
18-
AllowIndexing = false,
19-
CanonicalBaseUrl = null,
20-
GoogleTagManager = new GoogleTagManagerConfiguration(),
21-
Features = new FeatureFlags([]),
22-
StaticFileContentHashProvider = Model.StaticFileContentHashProvider
23-
};
7+
public GlobalLayoutViewModel LayoutModel => Model.CreateGlobalLayoutModel();
248
}
259
@{
2610
var parent = Model.CurrentNavigationItem.Parent as EndpointNavigationItem;

src/Elastic.ApiExplorer/Operations/OperationViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Elastic.ApiExplorer.Operations;
66

7-
public class OperationViewModel : ApiViewModel
7+
public class OperationViewModel(ApiRenderContext context) : ApiViewModel(context)
88
{
99
public required ApiOperation Operation { get; init; }
1010

0 commit comments

Comments
 (0)