Skip to content

Commit 910d13f

Browse files
authored
Fix products rendering (#1968)
1 parent 41e9595 commit 910d13f

File tree

5 files changed

+101
-28
lines changed

5 files changed

+101
-28
lines changed

src/Elastic.Markdown/Exporters/LlmMarkdownExporter.cs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.IO.Compression;
77
using System.Text;
88
using Elastic.Documentation.Configuration;
9+
using Elastic.Documentation.Configuration.Products;
910
using Elastic.Markdown.Helpers;
1011
using Markdig.Syntax;
1112

@@ -113,21 +114,11 @@ private string CreateLlmContentWithMetadata(MarkdownExportFileContext context, s
113114
if (!string.IsNullOrEmpty(sourceFile.Url))
114115
_ = metadata.AppendLine($"url: {context.BuildContext.CanonicalBaseUrl?.Scheme}://{context.BuildContext.CanonicalBaseUrl?.Host}{sourceFile.Url}");
115116

116-
var configProducts = context.BuildContext.ProductsConfiguration.Products.Select(p =>
117-
{
118-
if (context.BuildContext.ProductsConfiguration.Products.TryGetValue(p.Value.Id, out var product))
119-
return product;
120-
throw new ArgumentException($"Invalid product id: {p.Value.Id}");
121-
});
122-
var frontMatterProducts = sourceFile.YamlFrontMatter?.Products ?? [];
123-
var allProducts = frontMatterProducts
124-
.Union(configProducts)
125-
.Distinct()
126-
.ToList();
127-
if (allProducts.Count > 0)
117+
var pageProducts = GetPageProducts(sourceFile.YamlFrontMatter?.Products);
118+
if (pageProducts.Count > 0)
128119
{
129120
_ = metadata.AppendLine("products:");
130-
foreach (var item in allProducts.Select(p => p.DisplayName).Order())
121+
foreach (var item in pageProducts.Select(p => p.DisplayName).Order())
131122
_ = metadata.AppendLine($" - {item}");
132123
}
133124

@@ -138,6 +129,9 @@ private string CreateLlmContentWithMetadata(MarkdownExportFileContext context, s
138129

139130
return metadata.ToString();
140131
}
132+
133+
private static List<Product> GetPageProducts(IReadOnlyCollection<Product>? frontMatterProducts) =>
134+
frontMatterProducts?.ToList() ?? [];
141135
}
142136

143137
public static class LlmMarkdownExporterExtensions

src/Elastic.Markdown/HtmlWriter.cs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Text.Json;
77
using Elastic.Documentation;
88
using Elastic.Documentation.Configuration.LegacyUrlMappings;
9+
using Elastic.Documentation.Configuration.Products;
910
using Elastic.Documentation.Configuration.Versions;
1011
using Elastic.Documentation.Site.FileProviders;
1112
using Elastic.Documentation.Site.Navigation;
@@ -86,19 +87,7 @@ private async Task<RenderResult> RenderLayout(MarkdownFile markdown, MarkdownDoc
8687
var siteName = DocumentationSet.Tree.Index.Title ?? "Elastic Documentation";
8788
var legacyPages = LegacyUrlMapper.MapLegacyUrl(markdown.YamlFrontMatter?.MappedPages);
8889

89-
var configProducts = DocumentationSet.Context.ProductsConfiguration.Products.Select(p =>
90-
{
91-
if (DocumentationSet.Context.ProductsConfiguration.Products.TryGetValue(p.Value.Id, out var product))
92-
return product;
93-
throw new ArgumentException($"Invalid product id: {p.Value.Id}");
94-
});
95-
96-
var frontMatterProducts = markdown.YamlFrontMatter?.Products ?? [];
97-
98-
var allProducts = frontMatterProducts
99-
.Union(configProducts)
100-
.Distinct()
101-
.ToHashSet();
90+
var pageProducts = GetPageProducts(markdown.YamlFrontMatter?.Products);
10291

10392
string? allVersionsUrl = null;
10493

@@ -153,7 +142,7 @@ private async Task<RenderResult> RenderLayout(MarkdownFile markdown, MarkdownDoc
153142
AllVersionsUrl = allVersionsUrl,
154143
LegacyPages = legacyPages?.ToArray(),
155144
VersionDropdownItems = VersionDropDownItemViewModel.FromLegacyPageMappings(legacyPages?.ToArray()),
156-
Products = allProducts,
145+
Products = pageProducts,
157146
VersionsConfig = DocumentationSet.Context.VersionsConfiguration,
158147
StructuredBreadcrumbsJson = structuredBreadcrumbsJsonString
159148
});
@@ -229,6 +218,9 @@ public async Task<MarkdownDocument> WriteAsync(IDirectoryInfo outBaseDir, IFileI
229218
return document;
230219
}
231220

221+
private static HashSet<Product> GetPageProducts(IReadOnlyCollection<Product>? frontMatterProducts) =>
222+
frontMatterProducts?.ToHashSet() ?? [];
223+
232224
}
233225

234226
public record RenderResult

src/infra/docs-lambda-index-publisher/lambda.DockerFile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,3 @@ RUN arch=$TARGETARCH \
3232
&& echo $TARGETOS-$arch > /tmp/rid
3333

3434
RUN dotnet publish src/infra/docs-lambda-index-publisher -r linux-x64 -c Release
35-
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// Licensed to Elasticsearch B.V under one or more agreements.
2+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information
4+
5+
module ``AuthoringTests``.``frontmatter``.``products frontmatter``
6+
7+
open Swensen.Unquote
8+
open Xunit
9+
open authoring
10+
open JetBrains.Annotations
11+
12+
let frontMatter ([<LanguageInjection("yaml")>]m: string) =
13+
Setup.Document $"""---
14+
{m}
15+
---
16+
# Test Page
17+
18+
This is a test page with products frontmatter.
19+
"""
20+
21+
type ``products frontmatter in HTML`` () =
22+
static let markdownWithProducts = frontMatter """
23+
products:
24+
- id: elasticsearch
25+
- id: ecctl
26+
"""
27+
28+
[<Fact>]
29+
let ``includes products meta tags when products are specified`` () =
30+
markdownWithProducts |> converts "index.md" |> containsHtml """
31+
<meta class="elastic" name="product_name" content="Elasticsearch,Elastic Cloud Control ECCTL"/>
32+
<meta name="DC.subject" content="Elasticsearch,Elastic Cloud Control ECCTL"/>
33+
"""
34+
35+
[<Fact>]
36+
let ``does not include products meta tags when no products are specified`` () =
37+
let markdownWithoutProducts = Setup.Document """
38+
# Test Page
39+
40+
This is a test page without products frontmatter.
41+
"""
42+
// When there are no products, no product-related meta tags should be rendered at all
43+
let results = markdownWithoutProducts.Value
44+
let defaultFile = results.MarkdownResults |> Seq.find (fun r -> r.File.RelativePath = "index.md")
45+
let html = defaultFile.Html
46+
47+
// Verify that product meta tags are NOT present in the HTML
48+
test <@ not (html.Contains("product_name")) @>
49+
test <@ not (html.Contains("DC.subject")) @>
50+
51+
type ``products frontmatter in LLM Markdown`` () =
52+
static let markdownWithProducts = frontMatter """
53+
products:
54+
- id: elasticsearch
55+
- id: ecctl
56+
"""
57+
58+
static let markdownWithoutProducts = Setup.Document """
59+
# Test Page
60+
61+
This is a test page without products frontmatter.
62+
"""
63+
64+
[<Fact>]
65+
let ``includes products in frontmatter when products are specified`` () =
66+
// Test that the products frontmatter is correctly processed by checking the file
67+
let results = markdownWithProducts.Value
68+
let defaultFile = results.MarkdownResults |> Seq.find (fun r -> r.File.RelativePath = "index.md")
69+
70+
// Test that the file has the correct products
71+
test <@ defaultFile.File.YamlFrontMatter <> null @>
72+
test <@ defaultFile.File.YamlFrontMatter.Products <> null @>
73+
test <@ defaultFile.File.YamlFrontMatter.Products.Count = 2 @>
74+
75+
// Test that the products are correctly identified
76+
let productIds = defaultFile.File.YamlFrontMatter.Products |> Seq.map (fun p -> p.Id) |> Set.ofSeq
77+
test <@ productIds.Contains("elasticsearch") @>
78+
test <@ productIds.Contains("ecctl") @>
79+
80+
[<Fact>]
81+
let ``does not include products in frontmatter when no products are specified`` () =
82+
// Test that pages without products frontmatter don't have products
83+
let results = markdownWithoutProducts.Value
84+
let defaultFile = results.MarkdownResults |> Seq.find (fun r -> r.File.RelativePath = "index.md")
85+
86+
// Test that the file has no products
87+
test <@ defaultFile.File.YamlFrontMatter = null || defaultFile.File.YamlFrontMatter.Products = null || defaultFile.File.YamlFrontMatter.Products.Count = 0 @>

tests/authoring/authoring.fsproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,6 @@
5555
<Compile Include="Directives\IncludeBlocks.fs" />
5656
<Compile Include="Linters\WhiteSpaceNormalizers.fs" />
5757
<Compile Include="LlmMarkdown\LlmMarkdownOutput.fs" />
58+
<Compile Include="FrontMatter\ProductsFrontMatter.fs" />
5859
</ItemGroup>
5960
</Project>

0 commit comments

Comments
 (0)