| | | 1 | | using System.Text.RegularExpressions; |
| | | 2 | | |
| | | 3 | | namespace Chronicis.Shared.Utilities; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Utility class for generating URL-safe slugs from article titles. |
| | | 7 | | /// </summary> |
| | | 8 | | public static class SlugGenerator |
| | | 9 | | { |
| | 1 | 10 | | private static readonly Regex InvalidCharsRegex = new(@"[^a-z0-9-]", RegexOptions.Compiled); |
| | 1 | 11 | | private static readonly Regex MultipleHyphensRegex = new(@"-{2,}", RegexOptions.Compiled); |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Generates a URL-safe slug from a title. |
| | | 15 | | /// Rules: |
| | | 16 | | /// - Converts to lowercase |
| | | 17 | | /// - Replaces spaces with hyphens |
| | | 18 | | /// - Removes all characters except a-z, 0-9, and hyphens |
| | | 19 | | /// - Collapses multiple consecutive hyphens into single hyphen |
| | | 20 | | /// - Trims hyphens from start and end |
| | | 21 | | /// - Returns "untitled" if result is empty |
| | | 22 | | /// </summary> |
| | | 23 | | public static string GenerateSlug(string title) |
| | | 24 | | { |
| | 9 | 25 | | if (string.IsNullOrWhiteSpace(title)) |
| | 0 | 26 | | return "untitled"; |
| | | 27 | | |
| | | 28 | | // Convert to lowercase |
| | 9 | 29 | | var slug = title.ToLowerInvariant(); |
| | | 30 | | |
| | | 31 | | // Replace spaces with hyphens |
| | 9 | 32 | | slug = slug.Replace(" ", "-"); |
| | | 33 | | |
| | | 34 | | // Remove invalid characters (keep only a-z, 0-9, hyphens) |
| | 9 | 35 | | slug = InvalidCharsRegex.Replace(slug, string.Empty); |
| | | 36 | | |
| | | 37 | | // Replace multiple consecutive hyphens with single hyphen |
| | 9 | 38 | | slug = MultipleHyphensRegex.Replace(slug, "-"); |
| | | 39 | | |
| | | 40 | | // Trim hyphens from start and end |
| | 9 | 41 | | slug = slug.Trim('-'); |
| | | 42 | | |
| | 9 | 43 | | return string.IsNullOrEmpty(slug) ? "untitled" : slug; |
| | | 44 | | } |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// Validates if a slug is URL-safe. |
| | | 48 | | /// </summary> |
| | | 49 | | public static bool IsValidSlug(string slug) |
| | | 50 | | { |
| | 0 | 51 | | if (string.IsNullOrWhiteSpace(slug)) |
| | 0 | 52 | | return false; |
| | | 53 | | |
| | | 54 | | // Must be lowercase alphanumeric and hyphens only |
| | 0 | 55 | | return Regex.IsMatch(slug, @"^[a-z0-9-]+$"); |
| | | 56 | | } |
| | | 57 | | |
| | | 58 | | /// <summary> |
| | | 59 | | /// Generates a unique slug by appending a counter if needed. |
| | | 60 | | /// </summary> |
| | | 61 | | public static string GenerateUniqueSlug(string baseSlug, HashSet<string> existingSlugs) |
| | | 62 | | { |
| | 9 | 63 | | if (!existingSlugs.Contains(baseSlug)) |
| | 7 | 64 | | return baseSlug; |
| | | 65 | | |
| | 2 | 66 | | var counter = 2; |
| | | 67 | | string uniqueSlug; |
| | | 68 | | do |
| | | 69 | | { |
| | 2 | 70 | | uniqueSlug = $"{baseSlug}-{counter++}"; |
| | | 71 | | } |
| | 2 | 72 | | while (existingSlugs.Contains(uniqueSlug)); |
| | | 73 | | |
| | 2 | 74 | | return uniqueSlug; |
| | | 75 | | } |
| | | 76 | | } |