|
2 | 2 | // Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
|
3 | 3 | // See the LICENSE file in the project root for more information
|
4 | 4 |
|
| 5 | +using System.Text; |
| 6 | +using Elastic.Markdown.IO.Configuration; |
| 7 | + |
5 | 8 | namespace Elastic.Markdown.Helpers;
|
6 | 9 |
|
7 |
| -public class Htmx |
| 10 | +public static class Htmx |
| 11 | +{ |
| 12 | + public static string GetHxSelectOob(FeatureFlags features, string? pathPrefix, string currentUrl, string targetUrl) |
| 13 | + { |
| 14 | + HashSet<string> selectTargets = |
| 15 | + [ |
| 16 | + "#primary-nav", "#secondary-nav", "#markdown-content", "#toc-nav", "#prev-next-nav", "#breadcrumbs" |
| 17 | + ]; |
| 18 | + if (!HasSameTopLevelGroup(pathPrefix, currentUrl, targetUrl) && features.IsPrimaryNavEnabled) |
| 19 | + _ = selectTargets.Add("#pages-nav"); |
| 20 | + return string.Join(',', selectTargets); |
| 21 | + } |
| 22 | + |
| 23 | + public static bool HasSameTopLevelGroup(string? pathPrefix, string currentUrl, string targetUrl) |
| 24 | + { |
| 25 | + if (string.IsNullOrEmpty(targetUrl) || string.IsNullOrEmpty(currentUrl)) |
| 26 | + return false; |
| 27 | + var startIndex = pathPrefix?.Length ?? 0; |
| 28 | + |
| 29 | + if (currentUrl.Length < startIndex) |
| 30 | + throw new InvalidUrlException("Current URL is not a valid URL", currentUrl, startIndex); |
| 31 | + |
| 32 | + if (targetUrl.Length < startIndex) |
| 33 | + throw new InvalidUrlException("Target URL is not a valid URL", targetUrl, startIndex); |
| 34 | + |
| 35 | + var currentSegments = GetSegments(currentUrl[startIndex..].Trim('/')); |
| 36 | + var targetSegments = GetSegments(targetUrl[startIndex..].Trim('/')); |
| 37 | + return currentSegments.Length >= 1 && targetSegments.Length >= 1 && currentSegments[0] == targetSegments[0]; |
| 38 | + } |
| 39 | + |
| 40 | + public static string GetPreload() => "true"; |
| 41 | + |
| 42 | + public static string GetHxSwap() => "none"; |
| 43 | + public static string GetHxPushUrl() => "true"; |
| 44 | + public static string GetHxIndicator() => "#htmx-indicator"; |
| 45 | + |
| 46 | + private static string[] GetSegments(string url) => url.Split('/'); |
| 47 | + |
| 48 | + public static string GetHxAttributes(FeatureFlags features, string? pathPrefix, string currentUrl, string targetUrl) |
| 49 | + { |
| 50 | + |
| 51 | + var attributes = new StringBuilder(); |
| 52 | + _ = attributes.Append($" hx-get={targetUrl}"); |
| 53 | + _ = attributes.Append($" hx-select-oob={GetHxSelectOob(features, pathPrefix, currentUrl, targetUrl)}"); |
| 54 | + _ = attributes.Append($" hx-swap={GetHxSwap()}"); |
| 55 | + _ = attributes.Append($" hx-push-url={GetHxPushUrl()}"); |
| 56 | + _ = attributes.Append($" hx-indicator={GetHxIndicator()}"); |
| 57 | + _ = attributes.Append($" preload={GetPreload()}"); |
| 58 | + return attributes.ToString(); |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | + |
| 63 | +internal sealed class InvalidUrlException : ArgumentException |
8 | 64 | {
|
9 |
| - public static string GetHxSelectOob() => "#markdown-content,#toc-nav,#prev-next-nav,#breadcrumbs"; |
| 65 | + public InvalidUrlException(string message, string url, int startIndex) |
| 66 | + : base($"{message} (Url: {url}, StartIndex: {startIndex})") |
| 67 | + { |
| 68 | + Data["Url"] = url; |
| 69 | + Data["StartIndex"] = startIndex; |
| 70 | + } |
10 | 71 | }
|
0 commit comments