Files
ZelWiki/ZelWiki.Library/Navigation.cs
2025-02-23 18:47:21 +08:00

57 lines
1.5 KiB
C#

using System.Text;
using System.Text.RegularExpressions;
namespace ZelWiki.Library
{
/// <summary>
/// 导航
/// </summary>
public class Navigation
{
public static string Clean(string? str)
{
if (str == null)
{
return string.Empty;
}
str = str.Trim().Trim([':']).Trim();
if (str.Contains("::"))
{
throw new Exception("导航不能包含命名空间");
}
str = str.Replace("&quot;", "\"")
.Replace("&amp;", "&")
.Replace("&lt;", "<")
.Replace("&gt;", ">")
.Replace("&nbsp;", " ");
str = str.Replace('\\', '/');
str = str.Replace("::", "_").Trim();
var sb = new StringBuilder();
foreach (var c in str)
{
if (char.IsWhiteSpace(c) || c == '.')
{
sb.Append('_');
}
else if (char.IsLetterOrDigit(c) || c == '_' || c == '/' || c == '-')
{
sb.Append(c);
}
}
var result = sb.ToString();
result = Regex.Replace(result, @"[_]{2,}", "_");
result = Regex.Replace(result, @"[/]{2,}", "/");
return result.ToLower();
}
}
}