35 lines
897 B
C#
35 lines
897 B
C#
using System.Text;
|
||
using System.Text.RegularExpressions;
|
||
|
||
namespace SimpleWiki.Services;
|
||
|
||
public static class SlugHelper
|
||
{
|
||
public static string Generate(string title)
|
||
{
|
||
if (string.IsNullOrWhiteSpace(title))
|
||
{
|
||
return string.Empty;
|
||
}
|
||
|
||
var normalized = title.Trim().ToLowerInvariant();
|
||
normalized = normalized.Replace('ё', 'е');
|
||
|
||
var sb = new StringBuilder();
|
||
foreach (var ch in normalized)
|
||
{
|
||
if (char.IsLetterOrDigit(ch))
|
||
{
|
||
sb.Append(ch);
|
||
}
|
||
else if (char.IsWhiteSpace(ch) || ch == '-' || ch == '_')
|
||
{
|
||
sb.Append('-');
|
||
}
|
||
}
|
||
|
||
var slug = Regex.Replace(sb.ToString(), "-+", "-").Trim('-');
|
||
return string.IsNullOrWhiteSpace(slug) ? $"article-{DateTime.UtcNow:yyyyMMddHHmmss}" : slug;
|
||
}
|
||
}
|