Skip to content

Commit 723a8a9

Browse files
committed
Add initial support for page level annotations for product support
1 parent 4bf4291 commit 723a8a9

File tree

4 files changed

+114
-1
lines changed

4 files changed

+114
-1
lines changed

src/Elastic.Markdown/Helpers/SemVersion.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,14 @@ public SemVersion(int major, int minor, int patch, string? prerelease, string? m
9292
Metadata = metadata ?? string.Empty;
9393
}
9494

95+
public static explicit operator SemVersion(string b)
96+
{
97+
var semVersion = TryParse(b, out var version) ? version : TryParse(b + ".0", out version) ? version : null;
98+
return semVersion ?? throw new ArgumentException($"'{b}' is not a valid semver2 version string.");
99+
}
100+
101+
public static implicit operator string(SemVersion d) => d.ToString();
102+
95103
/// <summary>
96104
///
97105
/// </summary>

src/Elastic.Markdown/Myst/FrontMatterParser.cs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
// Licensed to Elasticsearch B.V under one or more agreements.
22
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
33
// See the LICENSE file in the project root for more information
4+
5+
using Elastic.Markdown.Helpers;
6+
using YamlDotNet.Core;
7+
using YamlDotNet.Core.Events;
48
using YamlDotNet.Serialization;
59
using YamlDotNet.Serialization.NamingConventions;
610

@@ -20,6 +24,43 @@ public class YamlFrontMatter
2024

2125
[YamlMember(Alias = "sub")]
2226
public Dictionary<string, string>? Properties { get; set; }
27+
28+
29+
[YamlMember(Alias = "applies")]
30+
public DeploymentType? AppliesTo { get; set; }
31+
}
32+
33+
[YamlSerializable]
34+
public class DeploymentType
35+
{
36+
[YamlMember(Alias = "self")]
37+
public SelfManagedDeployment? SelfManaged { get; set; }
38+
39+
[YamlMember(Alias = "cloud")]
40+
public CloudManagedDeployment? Cloud { get; set; }
41+
}
42+
43+
[YamlSerializable]
44+
public class SelfManagedDeployment
45+
{
46+
[YamlMember(Alias = "stack")]
47+
public SemVersion? Stack { get; set; }
48+
49+
[YamlMember(Alias = "ece")]
50+
public SemVersion? Ece { get; set; }
51+
52+
[YamlMember(Alias = "eck")]
53+
public SemVersion? Eck { get; set; }
54+
}
55+
56+
[YamlSerializable]
57+
public class CloudManagedDeployment
58+
{
59+
[YamlMember(Alias = "ess")]
60+
public SemVersion? Ess { get; set; }
61+
62+
[YamlMember(Alias = "serverless")]
63+
public SemVersion? Serverless { get; set; }
2364
}
2465

2566
public static class FrontMatterParser
@@ -30,10 +71,29 @@ public static YamlFrontMatter Deserialize(string yaml)
3071

3172
var deserializer = new StaticDeserializerBuilder(new YamlFrontMatterStaticContext())
3273
.IgnoreUnmatchedProperties()
74+
.WithTypeConverter(new SemVersionConverter())
3375
.Build();
3476

3577
var frontMatter = deserializer.Deserialize<YamlFrontMatter>(input);
3678
return frontMatter;
3779

3880
}
3981
}
82+
83+
public class SemVersionConverter : IYamlTypeConverter
84+
{
85+
public bool Accepts(Type type) => type == typeof(SemVersion);
86+
87+
public object? ReadYaml(IParser parser, Type type, ObjectDeserializer rootDeserializer)
88+
{
89+
var value = parser.Consume<Scalar>();
90+
return (SemVersion)value.Value;
91+
}
92+
93+
public void WriteYaml(IEmitter emitter, object? value, Type type, ObjectSerializer serializer)
94+
{
95+
if (value == null)
96+
return;
97+
emitter.Emit(new Scalar(value.ToString()!));
98+
}
99+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
using Elastic.Markdown.Helpers;
6+
using Elastic.Markdown.Tests.Directives;
7+
using FluentAssertions;
8+
using Xunit.Abstractions;
9+
10+
namespace Elastic.Markdown.Tests.FrontMatter;
11+
12+
public class ProductConstraintTests(ITestOutputHelper output) : DirectiveTest(output,
13+
"""
14+
---
15+
title: Elastic Docs v3
16+
navigation_title: "Documentation Guide"
17+
applies:
18+
self:
19+
stack: 7.7
20+
cloud:
21+
serverless: 1.0.0
22+
---
23+
"""
24+
)
25+
{
26+
[Fact]
27+
public void ReadsTitle() => File.Title.Should().Be("Elastic Docs v3");
28+
29+
[Fact]
30+
public void ReadsNavigationTitle() => File.NavigationTitle.Should().Be("Documentation Guide");
31+
32+
[Fact]
33+
public void ReadsSubstitutions()
34+
{
35+
File.YamlFrontMatter.Should().NotBeNull();
36+
var appliesTo = File.YamlFrontMatter!.AppliesTo;
37+
appliesTo.Should().NotBeNull();
38+
appliesTo!.SelfManaged.Should().NotBeNull();
39+
appliesTo.Cloud.Should().NotBeNull();
40+
appliesTo.Cloud!.Serverless.Should().BeEquivalentTo(new SemVersion(1,0,0));
41+
}
42+
}
43+

tests/Elastic.Markdown.Tests/Directives/YamlFrontMatterTests.cs renamed to tests/Elastic.Markdown.Tests/FrontMatter/YamlFrontMatterTests.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
// Licensed to Elasticsearch B.V under one or more agreements.
22
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
33
// See the LICENSE file in the project root for more information
4+
5+
using Elastic.Markdown.Tests.Directives;
46
using FluentAssertions;
57
using Xunit.Abstractions;
68

7-
namespace Elastic.Markdown.Tests.Directives;
9+
namespace Elastic.Markdown.Tests.FrontMatter;
810

911
public class YamlFrontMatterTests(ITestOutputHelper output) : DirectiveTest(output,
1012
"""

0 commit comments

Comments
 (0)