Skip to content

Commit

Permalink
UrlTool update
Browse files Browse the repository at this point in the history
  • Loading branch information
HermanSchoenfeld committed Oct 4, 2024
1 parent 4620ab1 commit 4a34686
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/Hydrogen/Network/UrlTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Text;
Expand All @@ -32,6 +33,12 @@ public static string StripAnchorTag(string url) {
return url;
}

public static IEnumerable<string> CalculateBreadcrumbFromPath(string urlPath) {
var urlParts = Tools.Url.StripAnchorTag(urlPath.TrimStart("/")).Split('/').Reverse().Reverse().ToArray();
for (var i = urlParts.Length; i > 0; i--) {
yield return Tools.Url.Combine(urlParts.Take(i));
}
}

public static string ToHtml4DOMObjectID(string text, string prefixIfRequired = "obj_") {
var slug = ToUrlSlug(text);
Expand Down Expand Up @@ -121,7 +128,6 @@ public static Dictionary<string, string> ParseQueryString(string queryString) {
public static bool IsVideoSharingUrl(string url)
=> IsYouTubeUrl(url) || IsVimeoUrl(url);


public static bool IsYouTubeUrl(string url)
=> TryParseYouTubeUrl(url, out _);

Expand All @@ -141,7 +147,6 @@ public static bool TryParseYouTubeUrl(string url, bool allowIdOnly, out string v
return videoID != null;
}


public static bool IsVimeoUrl(string url)
=> TryParseVimeoUrl(url, out _);

Expand Down Expand Up @@ -217,7 +222,6 @@ private static IEnumerable<KeyValuePair<string, string>> RemoveEmptyValueQueryPa
return queryParams.Where(x => !string.IsNullOrWhiteSpace(x.Value));
}


public static string Combine(string url1, string url2) {
Guard.ArgumentNotNullOrWhitespace(url1, nameof(url1));
if (string.IsNullOrWhiteSpace(url2))
Expand All @@ -244,7 +248,7 @@ public static string ToQueryString(Dictionary<string, string> data) {
return sb.ToString();
}

public static bool TryParse(string url, out string protocol, out int port, out string host, out string path, out string queryString) {
public static bool TryParse(string url, out string protocol, out int? port, out string host, out string path, out string queryString) {
if (!Uri.TryCreate(url, UriKind.RelativeOrAbsolute, out var uri)) {
protocol = host = path = queryString = null;
port = -1;
Expand All @@ -255,15 +259,16 @@ public static bool TryParse(string url, out string protocol, out int port, out s
port = 0;
return false;
}

protocol = uri.Scheme;
port = uri.Port;
port = uri.Authority.Contains(":") ? uri.Port : null;
host = uri.Host;
path = uri.AbsolutePath;
queryString = uri.Query;
return true;
}

public static (string protocol, int port, string host, string path, string queryString) Parse(string url) {
public static (string protocol, int? port, string host, string path, string queryString) Parse(string url) {
if (!TryParse(url, out var protocol, out var port, out var host, out var path, out var queryString))
throw new InvalidOperationException($"Unable to parse '{url}'");
return (protocol, port, host, path, queryString);
Expand Down

0 comments on commit 4a34686

Please sign in to comment.