Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/Elastic.Documentation.Site/Htmx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@
// 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 System.Reflection;
using System.Text;
using Elastic.Documentation.Extensions;

namespace Elastic.Documentation.Site;

public static class Htmx
{
private static readonly string Version =
Assembly.GetExecutingAssembly().GetCustomAttributes<AssemblyInformationalVersionAttribute>()
.FirstOrDefault()?.InformationalVersion ?? "0.0.0";
public static readonly string VersionHash = ShortId.Create(Version);

public static string GetHxSelectOob(bool hasSameTopLevelGroup) => hasSameTopLevelGroup ? "#content-container,#toc-nav" : "#main-container";
public const string Preload = "mousedown";
public const string HxSwap = "none";
Expand All @@ -24,8 +31,9 @@ public static string GetHxAttributes(
string? hxIndicator = HxIndicator
)
{
var hxGetUrl = targetUrl.Contains('?') ? $"{targetUrl}&v={VersionHash}" : $"{targetUrl}?v={VersionHash}";
var attributes = new StringBuilder();
_ = attributes.Append($" hx-get={targetUrl}");
_ = attributes.Append($" hx-get={hxGetUrl}");
_ = attributes.Append($" hx-select-oob={hxSwapOob ?? GetHxSelectOob(hasSameTopLevelGroup)}");
_ = attributes.Append($" hx-swap={hxSwap}");
_ = attributes.Append($" hx-push-url={hxPushUrl}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// 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 System.Reflection;
using Elastic.Documentation.Extensions;
using Elastic.Documentation.Site;
using Elastic.Documentation.Site.Navigation;
using Elastic.Markdown.IO;
Expand All @@ -25,7 +27,10 @@ protected override void Write(HtmlRenderer renderer, LinkInline link)
return;
}

var url = link.GetDynamicUrl != null ? link.GetDynamicUrl() : link.Url;
var url = link.GetDynamicUrl?.Invoke() ?? link.Url;
var hxGetUrl = url;
if (hxGetUrl is not null)
hxGetUrl = hxGetUrl.Contains('?') ? $"{hxGetUrl}&v={Htmx.VersionHash}" : $"{hxGetUrl}?v={Htmx.VersionHash}";

var isCrossLink = (link.GetData("isCrossLink") as bool?) == true;
var isHttpLink = url?.StartsWith("http") ?? false;
Expand All @@ -41,7 +46,7 @@ protected override void Write(HtmlRenderer renderer, LinkInline link)
var targetRootNavigation = link.GetData($"Target{nameof(MarkdownFile.NavigationRoot)}") as INodeNavigationItem<INavigationModel, INavigationItem>;
var hasSameTopLevelGroup = !isCrossLink && (currentRootNavigation?.Id == targetRootNavigation?.Id);
_ = renderer.Write(" hx-get=\"");
_ = renderer.WriteEscapeUrl(url);
_ = renderer.WriteEscapeUrl(hxGetUrl);
_ = renderer.Write('"');
_ = renderer.Write($" hx-select-oob=\"{Htmx.GetHxSelectOob(hasSameTopLevelGroup)}\"");
_ = renderer.Write($" hx-swap=\"{Htmx.HxSwap}\"");
Expand Down
Loading