-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathIndex.cshtml
More file actions
123 lines (121 loc) · 5.32 KB
/
Index.cshtml
File metadata and controls
123 lines (121 loc) · 5.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
@using System.Text.Json
@using Elastic.Documentation
@using Elastic.Documentation.Configuration
@using Elastic.Documentation.Extensions
@using Elastic.Documentation.Site
@using Elastic.Markdown
@using Elastic.Markdown.Myst.Components
@using Markdig
@using ViewModelSerializerContext = Elastic.Markdown.Page.ViewModelSerializerContext
@inherits RazorSliceHttpResult<Elastic.Markdown.Page.IndexViewModel>
@implements IUsesLayout<Elastic.Codex._MarkdownLayout, MarkdownLayoutViewModel>
@functions {
static string GetRootPath(string? siteRootPath, string? urlPathPrefix)
{
if (!string.IsNullOrEmpty(siteRootPath))
return siteRootPath;
var prefix = urlPathPrefix?.Trim('/') ?? "";
return string.IsNullOrEmpty(prefix) ? "/" : $"/{prefix}/";
}
static bool IsDocsetRootUrl(string? navUrl, string? docsetUrl, string? urlPathPrefix)
{
if (string.IsNullOrEmpty(docsetUrl))
return false;
var docsetNorm = docsetUrl.TrimEnd('/');
var navNorm = navUrl?.TrimEnd('/') ?? "";
if (string.IsNullOrEmpty(navNorm) || navNorm == "/")
return string.Equals((urlPathPrefix ?? "").TrimEnd('/'), docsetNorm, StringComparison.OrdinalIgnoreCase);
if (navNorm.StartsWith("/"))
return string.Equals(navNorm, docsetNorm, StringComparison.OrdinalIgnoreCase);
var navFull = $"{urlPathPrefix?.TrimEnd('/')}/{navNorm.TrimStart('/')}".TrimEnd('/');
return string.Equals(navFull, docsetNorm, StringComparison.OrdinalIgnoreCase);
}
public MarkdownLayoutViewModel LayoutModel => new()
{
DocsBuilderVersion = ShortId.Create(BuildContext.Version),
Layout = Model.CurrentDocument.YamlFrontMatter?.Layout,
RenderHamburgerIcon = Model.CurrentDocument.YamlFrontMatter?.Layout != MarkdownPageLayout.LandingPage,
DocSetName = Model.DocSetName,
Title = $"{Model.Title} | {Model.SiteName}",
Description = Model.Description,
PageTocItems = Model.PageTocItems.Where(i => i is
{
Level: 2 or 3
})
.ToList(),
CurrentNavigationItem = Model.CurrentNavigationItem,
Previous = Model.PreviousDocument,
Next = Model.NextDocument,
NavigationHtml = Model.NavigationHtml,
NavV2Sections = Model.NavV2Sections,
ActiveSectionId = Model.ActiveSectionId,
UrlPathPrefix = Model.UrlPathPrefix,
GithubEditUrl = Model.GithubEditUrl,
MarkdownUrl = Model.MarkdownUrl,
BuildType = Model.BuildType,
HideEditThisPage = Model.Features.DisableGitHubEditLink && Model.BuildType != BuildType.Isolated,
AllowIndexing = Model.AllowIndexing,
CanonicalBaseUrl = Model.CanonicalBaseUrl,
GoogleTagManager = Model.GoogleTagManager,
Optimizely = Model.Optimizely,
Features = Model.Features,
StaticFileContentHashProvider = Model.StaticFileContentHashProvider,
ReportIssueUrl = Model.ReportIssueUrl,
Breadcrumbs = Model.Breadcrumbs,
Htmx = new DefaultHtmxAttributeProvider(GetRootPath(Model.SiteRootPath, Model.UrlPathPrefix)),
CurrentVersion = Model.CurrentDocument.YamlFrontMatter?.Layout == MarkdownPageLayout.LandingPage ? Model.VersionsConfig.VersioningSystems[0].Current : Model.CurrentVersion,
AllVersionsUrl = Model.AllVersionsUrl,
VersioningSystem = Model.VersioningSystem,
VersionDropdownSerializedModel = JsonSerializer.Serialize(Model.VersionDropdownItems,
ViewModelSerializerContext.Default.VersionDropDownItemViewModelArray),
CodexBreadcrumbs = Model.CodexBreadcrumbs is { } cb
? [.. cb, .. Model.Breadcrumbs.Skip(1).Where(b => !IsDocsetRootUrl(b.Url, cb[^1].Url, Model.UrlPathPrefix)).Select(b => new CodexBreadcrumb(b.NavigationTitle ?? "", b.Url)), .. (Model.Breadcrumbs.Length > 1 && !IsDocsetRootUrl(Model.CurrentNavigationItem.Url, cb[^1].Url, Model.UrlPathPrefix) ? new[] { new CodexBreadcrumb(Model.CurrentNavigationItem.NavigationTitle ?? "", Model.CurrentNavigationItem.Url) } : Array.Empty<CodexBreadcrumb>())]
: null,
// Header properties for isolated mode
HeaderTitle = Model.SiteHeaderTitle ?? Model.DocSetName,
HeaderVersion = null,
GitBranch = Model.GitBranch,
GitCommitShort = Model.GitCommitShort,
GitRepository = Model.GitRepository,
GitHubDocsUrl = Model.GitHubDocsUrl,
GitHubRef = Model.GitHubRef,
};
protected override Task ExecuteSectionAsync(string name)
{
if (name == GlobalSections.Head)
{
@if (Model.CurrentVersion is not null)
{
<meta class="elastic" name="product_version" content="@(new HtmlString(Model.CurrentVersion))"/>
<meta name="DC.identifier" content="@(new HtmlString(Model.CurrentVersion))"/>
}
<link rel="alternate" type="text/markdown" href="@(Model.MarkdownUrl)" title="Markdown export"/>
<script type="application/ld+json">
@(new HtmlString(Model.StructuredBreadcrumbsJson))
</script>
}
if (name == GlobalSections.Head && Model.Products is { Count: > 0 })
{
var products = string.Join(",", Model.Products.Select(p => p.DisplayName));
<meta class="elastic" name="product_name" content="@(products)"/>
<meta name="DC.subject" content="@(products)"/>
}
return Task.CompletedTask;
}
}
<section id="elastic-docs-v3">
@* This way it's correctly rendered as <h1>text</h1> instead of <h1><p>text</p></h1> *@
@(new HtmlString(Markdown.ToHtml("# " + Model.TitleRaw)))
@if (Model.AppliesTo is not null)
{
<p class="applies applies-block">
@await RenderPartialAsync(ApplicableToComponent.Create(new ApplicableToViewModel
{
AppliesTo = Model.AppliesTo,
Inline = false,
VersionsConfig = Model.VersionsConfig
}))
</p>
}
@(new HtmlString(Model.MarkdownHtml))
</section>