1
0
Files
OAiP-Presnyakov_Ilya-Labora…/Services/SlugHelper.cs
Debug_pro fb13e5a20a upload
2026-06-07 18:19:53 +03:00

35 lines
897 B
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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;
}
}