Skip to content

Commit ee10395

Browse files
committed
Refactor feature flags
1 parent 549d490 commit ee10395

File tree

13 files changed

+47
-35
lines changed

13 files changed

+47
-35
lines changed

docs/_docset.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ subs:
1010
ece: "Elastic Cloud Enterprise"
1111
eck: "Elastic Cloud on Kubernetes"
1212

13-
# features:
14-
# primary-nav: true
13+
features:
14+
primary-nav: false
1515

1616
toc:
1717
- file: index.md

src/Elastic.Markdown/Helpers/Htmx.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ namespace Elastic.Markdown.Helpers;
99

1010
public static class Htmx
1111
{
12-
public static string GetHxSelectOob(ConfigurationFile configuration, string? pathPrefix, string currentUrl, string targetUrl)
12+
public static string GetHxSelectOob(FeatureFlags features, string? pathPrefix, string currentUrl, string targetUrl)
1313
{
1414
HashSet<string> selectTargets =
1515
[
1616
"#primary-nav", "#secondary-nav", "#markdown-content", "#toc-nav", "#prev-next-nav", "#breadcrumbs"
1717
];
18-
if (!HasSameTopLevelGroup(pathPrefix, currentUrl, targetUrl) && configuration.Features.ContainsKey("primary-nav"))
18+
if (!HasSameTopLevelGroup(pathPrefix, currentUrl, targetUrl) && features.IsPrimaryNavEnabled)
1919
_ = selectTargets.Add("#pages-nav");
2020
return string.Join(',', selectTargets);
2121
}
@@ -36,12 +36,12 @@ public static bool HasSameTopLevelGroup(string? pathPrefix, string currentUrl, s
3636

3737
private static string[] GetSegments(string url) => url.Split('/');
3838

39-
public static string GetHxAttributes(ConfigurationFile configuration, string? pathPrefix, string currentUrl, string targetUrl)
39+
public static string GetHxAttributes(FeatureFlags features, string? pathPrefix, string currentUrl, string targetUrl)
4040
{
4141

4242
var attributes = new StringBuilder();
4343
_ = attributes.Append($" hx-get={targetUrl}");
44-
_ = attributes.Append($" hx-select-oob={GetHxSelectOob(configuration, pathPrefix, currentUrl, targetUrl)}");
44+
_ = attributes.Append($" hx-select-oob={GetHxSelectOob(features, pathPrefix, currentUrl, targetUrl)}");
4545
_ = attributes.Append($" hx-swap={GetHxSwap()}");
4646
_ = attributes.Append($" hx-push-url={GetHxPushUrl()}");
4747
_ = attributes.Append($" hx-indicator={GetHxIndicator()}");

src/Elastic.Markdown/IO/Configuration/ConfigurationFile.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ public record ConfigurationFile : DocumentationFile
3131
private readonly Dictionary<string, string> _substitutions = new(StringComparer.OrdinalIgnoreCase);
3232
public IReadOnlyDictionary<string, string> Substitutions => _substitutions;
3333

34-
private readonly Dictionary<string, string> _features = new(StringComparer.OrdinalIgnoreCase);
35-
public IReadOnlyDictionary<string, string> Features => _features;
34+
private readonly Dictionary<string, bool> _features = new(StringComparer.OrdinalIgnoreCase);
35+
private FeatureFlags? _featureFlags;
36+
public FeatureFlags Features => _featureFlags ??= new FeatureFlags(_features);
3637

3738
public ConfigurationFile(IFileInfo sourceFile, IDirectoryInfo rootPath, BuildContext context, int depth = 0, string parentPath = "")
3839
: base(sourceFile, rootPath)
@@ -83,7 +84,7 @@ public ConfigurationFile(IFileInfo sourceFile, IDirectoryInfo rootPath, BuildCon
8384
TableOfContents = entries;
8485
break;
8586
case "features":
86-
_features = reader.ReadDictionary(entry.Entry);
87+
_features = reader.ReadDictionary(entry.Entry).ToDictionary(k => k.Key, v => bool.Parse(v.Value), StringComparer.OrdinalIgnoreCase);
8788
break;
8889
case "external_hosts":
8990
reader.EmitWarning($"{entry.Key} has been deprecated and will be removed", entry.Key);
@@ -103,6 +104,8 @@ public ConfigurationFile(IFileInfo sourceFile, IDirectoryInfo rootPath, BuildCon
103104
Globs = [.. ImplicitFolders.Select(f => Glob.Parse($"{f}/*.md"))];
104105
}
105106

107+
public bool IsFeatureEnabled(string feature) => _features.TryGetValue(feature, out var enabled) && enabled;
108+
106109
private List<ITocItem> ReadChildren(YamlStreamReader reader, KeyValuePair<YamlNode, YamlNode> entry, string parentPath)
107110
{
108111
var entries = new List<ITocItem>();
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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+
namespace Elastic.Markdown.IO.Configuration;
6+
7+
public class FeatureFlags(Dictionary<string, bool> featureFlags)
8+
{
9+
public bool IsPrimaryNavEnabled => IsEnabled("primary-nav");
10+
private bool IsEnabled(string key) => featureFlags.TryGetValue(key, out var value) && value;
11+
}

src/Elastic.Markdown/Myst/Renderers/HtmxLinkInlineRenderer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ protected override void Write(HtmlRenderer renderer, LinkInline link)
3131
_ = renderer.Write(" hx-get=\"");
3232
_ = renderer.WriteEscapeUrl(link.GetDynamicUrl != null ? link.GetDynamicUrl() ?? link.Url : link.Url);
3333
_ = renderer.Write('"');
34-
_ = renderer.Write($" hx-select-oob=\"{Htmx.GetHxSelectOob(build.Configuration, build.UrlPathPrefix, currentUrl, link.Url)}\"");
34+
_ = renderer.Write($" hx-select-oob=\"{Htmx.GetHxSelectOob(build.Configuration.Features, build.UrlPathPrefix, currentUrl, link.Url)}\"");
3535
_ = renderer.Write(" hx-swap=\"none\"");
3636
_ = renderer.Write(" hx-push-url=\"true\"");
3737
_ = renderer.Write(" hx-indicator=\"#htmx-indicator\"");

src/Elastic.Markdown/Slices/HtmlWriter.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ private async Task<string> RenderNavigation(ConfigurationFile configuration, str
2727
Tree = group ?? DocumentationSet.Tree,
2828
CurrentDocument = markdown,
2929
IsRoot = topLevelGroupId == DocumentationSet.Tree.Id,
30-
Configuration = configuration
30+
Features = configuration.Features
3131
});
3232
return await slice.RenderAsync(cancellationToken: ctx);
3333
}
@@ -56,7 +56,7 @@ public async Task<string> RenderLayout(MarkdownFile markdown, MarkdownDocument d
5656

5757
string? navigationHtml;
5858

59-
if (DocumentationSet.Configuration.Features.ContainsKey("primary-nav"))
59+
if (DocumentationSet.Configuration.Features.IsPrimaryNavEnabled)
6060
{
6161
var topLevelGroupId = GetTopLevelGroupId(markdown);
6262
if (!_renderedNavigationCache.TryGetValue(topLevelGroupId, out var value))
@@ -101,7 +101,7 @@ public async Task<string> RenderLayout(MarkdownFile markdown, MarkdownDocument d
101101
Applies = markdown.YamlFrontMatter?.AppliesTo,
102102
GithubEditUrl = editUrl,
103103
AllowIndexing = DocumentationSet.Build.AllowIndexing && !markdown.Hidden,
104-
Configuration = DocumentationSet.Configuration
104+
Features = DocumentationSet.Configuration.Features
105105
});
106106
return await slice.RenderAsync(cancellationToken: ctx);
107107
}

src/Elastic.Markdown/Slices/Index.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
UrlPathPrefix = Model.UrlPathPrefix,
1616
GithubEditUrl = Model.GithubEditUrl,
1717
AllowIndexing = Model.AllowIndexing,
18-
Configuration = Model.Configuration,
18+
Features = Model.Features
1919
};
2020
}
2121
<section id="elastic-docs-v3">

src/Elastic.Markdown/Slices/Layout/_Breadcrumbs.cshtml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
itemprop="item"
77
href="@Model.Link("/")"
88
hx-get="@Model.Link("/")"
9-
hx-select-oob="@Htmx.GetHxSelectOob(Model.Configuration, Model.UrlPathPrefix, Model.Link("/"), Model.CurrentDocument.Url)"
9+
hx-select-oob="@Htmx.GetHxSelectOob(Model.Features, Model.UrlPathPrefix, Model.Link("/"), Model.CurrentDocument.Url)"
1010
hx-swap="none"
1111
hx-push-url="true"
1212
hx-indicator="#htmx-indicator"
@@ -24,7 +24,7 @@
2424
itemprop="item"
2525
href="@item.Url"
2626
hx-get="@item.Url"
27-
hx-select-oob="@Htmx.GetHxSelectOob(Model.Configuration, Model.UrlPathPrefix, item.Url, Model.CurrentDocument.Url)"
27+
hx-select-oob="@Htmx.GetHxSelectOob(Model.Features, Model.UrlPathPrefix, item.Url, Model.CurrentDocument.Url)"
2828
hx-swap="none"
2929
hx-push-url="true"
3030
hx-indicator="#htmx-indicator"

src/Elastic.Markdown/Slices/Layout/_Header.cshtml

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
@{
55
string GetHxAttributes(string url)
66
{
7-
return Htmx.GetHxAttributes(Model.Configuration, Model.UrlPathPrefix, Model.CurrentDocument.Url, url);
7+
return Htmx.GetHxAttributes(Model.Features, Model.UrlPathPrefix, Model.CurrentDocument.Url, url);
88
}
99

1010
var primaryNavViewModel = new PrimaryNavViewModel
@@ -31,7 +31,7 @@
3131
Description = "Unify app and infrastructure visibility to proactively resolve issues.",
3232
Url = Model.Link("/solutions/observability"),
3333
HtmxAttributes = Htmx.GetHxAttributes(
34-
Model.Configuration,
34+
Model.Features,
3535
Model.CurrentDocument.UrlPathPrefix,
3636
Model.CurrentDocument.Url,
3737
Model.Link("/solutions/security")
@@ -113,7 +113,7 @@
113113
<img src="@Model.Static("logo-elastic-horizontal-color.svg")" alt="Elastic" height="40" width="116">
114114
</a>
115115
</div>
116-
@if (Model.Configuration.Features.ContainsKey("primary-nav"))
116+
@if (Model.Features.IsPrimaryNavEnabled)
117117
{
118118
@await RenderPartialAsync(_PrimaryNav.Create(primaryNavViewModel))
119119
}
@@ -141,14 +141,12 @@
141141
</div>
142142
</div>
143143
</header>
144-
@* <div class="h-1"> *@
145-
@* </div> *@
146-
@if (Model.Configuration.Features.ContainsKey("primary-nav"))
144+
@if (Model.Features.IsPrimaryNavEnabled)
147145
{
148146
<nav id="secondary-nav" class="bg-grey-10 border-b-1 border-grey-20 font-sans font-semibold px-6 pt-6">
149147
<div class="container flex mx-auto justify-between">
150148
<ul class="flex gap-6">
151-
@if (!Model.Configuration.Features.ContainsKey("primary-nav"))
149+
@if (!Model.Features.IsPrimaryNavEnabled)
152150
{
153151
@foreach (var navItem in Model.TopLevelNavigationItems)
154152
{
@@ -159,7 +157,7 @@
159157
<a
160158
href="@navItem.Index.Url"
161159
hx-get="@navItem.Index.Url"
162-
hx-select-oob="@Htmx.GetHxSelectOob(Model.Configuration, Model.CurrentDocument.UrlPathPrefix, Model.CurrentDocument.Url, navItem.Index.Url)"
160+
hx-select-oob="@Htmx.GetHxSelectOob(Model.Features, Model.CurrentDocument.UrlPathPrefix, Model.CurrentDocument.Url, navItem.Index.Url)"
163161
hx-swap="none"
164162
hx-push-url="true"
165163
hx-indicator="#htmx-indicator"

src/Elastic.Markdown/Slices/Layout/_PrevNextNav.cshtml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<a
99
href="@Model.Previous.Url"
1010
hx-get="@Model.Previous.Url"
11-
hx-select-oob="@Htmx.GetHxSelectOob(Model.Configuration, Model.CurrentDocument.UrlPathPrefix, Model.CurrentDocument.Url, Model.Previous.Url)"
11+
hx-select-oob="@Htmx.GetHxSelectOob(Model.Features, Model.CurrentDocument.UrlPathPrefix, Model.CurrentDocument.Url, Model.Previous.Url)"
1212
hx-swap="none"
1313
hx-push-url="true"
1414
hx-indicator="#htmx-indicator"
@@ -31,7 +31,7 @@
3131
<a
3232
href="@Model.Next.Url"
3333
hx-get="@Model.Next.Url"
34-
hx-select-oob="@Htmx.GetHxSelectOob(Model.Configuration, Model.CurrentDocument.UrlPathPrefix, Model.CurrentDocument.Url, Model.Next.Url)"
34+
hx-select-oob="@Htmx.GetHxSelectOob(Model.Features, Model.CurrentDocument.UrlPathPrefix, Model.CurrentDocument.Url, Model.Next.Url)"
3535
hx-swap="none"
3636
hx-push-url="true"
3737
hx-indicator="#htmx-indicator"

0 commit comments

Comments
 (0)