-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathSettingsViewModel.cs
More file actions
88 lines (70 loc) · 3.32 KB
/
SettingsViewModel.cs
File metadata and controls
88 lines (70 loc) · 3.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
// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information
using Elastic.Documentation.AppliesTo;
using Elastic.Documentation.Configuration.Versions;
using Elastic.Markdown.Helpers;
using Elastic.Markdown.Myst.Components;
using Elastic.Markdown.Myst.Roles.AppliesTo;
using RazorSlices;
namespace Elastic.Markdown.Myst.Directives.Settings;
public class SettingsViewModel
{
public required YamlSettings SettingsCollection { get; init; }
public required Func<string, string> RenderMarkdown { get; init; }
public required VersionsConfiguration VersionsConfig { get; init; }
/// <summary>Markdown heading level for each group section (1–6).</summary>
public required int GroupHeadingLevel { get; init; }
/// <summary>
/// When set, only settings visible for this deployment type are rendered.
/// Accepted values: <c>ech</c>, <c>ece</c>, <c>eck</c>, <c>self</c>.
/// </summary>
public string? ActiveDeploymentFilter { get; init; }
public bool IsGroupVisible(SettingsGrouping group) =>
ActiveDeploymentFilter is null ||
DeploymentFilter.AnyVisible(group.Settings, ActiveDeploymentFilter, null);
public bool IsSettingVisible(Setting setting, ApplicableTo? inheritedAppliesTo) =>
ActiveDeploymentFilter is null ||
setting.IsVisibleForDeployment(ActiveDeploymentFilter, inheritedAppliesTo);
public string RenderAppliesToInline(ApplicableTo? appliesTo) =>
RenderAppliesToPlacement(appliesTo, ApplicabilityBadgePlacement.Combined);
public string RenderStackRowBadges(ApplicableTo? appliesTo) =>
RenderAppliesToPlacement(appliesTo, ApplicabilityBadgePlacement.StackRow);
public string RenderSupportedOnBadges(ApplicableTo? appliesTo) =>
RenderAppliesToPlacement(appliesTo, ApplicabilityBadgePlacement.SupportedOnRow);
private string RenderAppliesToPlacement(ApplicableTo? appliesTo, ApplicabilityBadgePlacement placement)
{
if (appliesTo is null || appliesTo == ApplicableTo.All)
return string.Empty;
var viewModel = new ApplicableToViewModel
{
AppliesTo = appliesTo,
Inline = true,
VersionsConfig = VersionsConfig,
BadgePlacement = placement
};
return ApplicableToRole.Create(viewModel).RenderAsync().AsTask().GetAwaiter().GetResult();
}
/// <summary>Stable HTML id / in-page TOC slug for a settings YAML group heading.</summary>
public static string GroupHeadingSlug(SettingsGrouping group) =>
string.IsNullOrWhiteSpace(group.Id)
? (group.Name ?? string.Empty).Slugify()
: group.Id;
public static string ComposeSettingName(string? parentName, string? settingName)
{
if (string.IsNullOrWhiteSpace(settingName))
return parentName ?? string.Empty;
if (string.IsNullOrWhiteSpace(parentName))
return settingName;
if (settingName.StartsWith(parentName, StringComparison.Ordinal))
return settingName;
if (settingName.StartsWith('[') || settingName.StartsWith('.'))
return parentName + settingName;
return $"{parentName}.{settingName}";
}
/// <summary>Stable HTML fragment for a setting: YAML <c>id</c> when present, otherwise slugified composed name.</summary>
public static string SettingFragmentId(Setting setting, string composedDisplayName) =>
string.IsNullOrWhiteSpace(setting.Id)
? composedDisplayName.Replace('.', '-').Slugify()
: setting.Id;
}