| | | 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 | | { |
| | 40 | 25 | | if (string.IsNullOrWhiteSpace(title)) |
| | 3 | 26 | | return "untitled"; |
| | | 27 | | |
| | | 28 | | // Convert to lowercase |
| | 37 | 29 | | var slug = title.ToLowerInvariant(); |
| | | 30 | | |
| | | 31 | | // Replace spaces with hyphens |
| | 37 | 32 | | slug = slug.Replace(" ", "-"); |
| | | 33 | | |
| | | 34 | | // Remove invalid characters (keep only a-z, 0-9, hyphens) |
| | 37 | 35 | | slug = InvalidCharsRegex.Replace(slug, string.Empty); |
| | | 36 | | |
| | | 37 | | // Replace multiple consecutive hyphens with single hyphen |
| | 37 | 38 | | slug = MultipleHyphensRegex.Replace(slug, "-"); |
| | | 39 | | |
| | | 40 | | // Trim hyphens from start and end |
| | 37 | 41 | | slug = slug.Trim('-'); |
| | | 42 | | |
| | 37 | 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 | | { |
| | 37 | 51 | | if (string.IsNullOrWhiteSpace(slug)) |
| | 3 | 52 | | return false; |
| | | 53 | | |
| | | 54 | | // Must be lowercase alphanumeric and hyphens only |
| | 34 | 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)) |
| | 2 | 64 | | return baseSlug; |
| | | 65 | | |
| | 7 | 66 | | var counter = 2; |
| | | 67 | | string uniqueSlug; |
| | | 68 | | do |
| | | 69 | | { |
| | 111 | 70 | | uniqueSlug = $"{baseSlug}-{counter++}"; |
| | | 71 | | } |
| | 111 | 72 | | while (existingSlugs.Contains(uniqueSlug)); |
| | | 73 | | |
| | 7 | 74 | | return uniqueSlug; |
| | | 75 | | } |
| | | 76 | | |
| | | 77 | | /// <summary> |
| | | 78 | | /// Generates a unique slug that is not in <paramref name="existingSiblingSlugs"/> and not in |
| | | 79 | | /// <paramref name="reservedSlugs"/>. If <paramref name="baseSlug"/> itself is reserved or |
| | | 80 | | /// already taken, appends <c>-2</c>, <c>-3</c>, … until a free value is found. |
| | | 81 | | /// </summary> |
| | | 82 | | public static string GenerateUniqueSiblingSlug( |
| | | 83 | | string baseSlug, |
| | | 84 | | HashSet<string> existingSiblingSlugs, |
| | | 85 | | IReadOnlyCollection<string> reservedSlugs) |
| | | 86 | | { |
| | | 87 | | // Normalise to a valid slug shape first |
| | 7 | 88 | | var slug = IsValidSlug(baseSlug) ? baseSlug : GenerateSlug(baseSlug); |
| | | 89 | | |
| | 7 | 90 | | var reserved = new HashSet<string>(reservedSlugs, StringComparer.OrdinalIgnoreCase); |
| | | 91 | | |
| | 7 | 92 | | if (!existingSiblingSlugs.Contains(slug) && !reserved.Contains(slug)) |
| | 2 | 93 | | return slug; |
| | | 94 | | |
| | 5 | 95 | | var counter = 2; |
| | | 96 | | string candidate; |
| | | 97 | | do |
| | | 98 | | { |
| | 9 | 99 | | candidate = $"{slug}-{counter++}"; |
| | | 100 | | } |
| | 9 | 101 | | while (existingSiblingSlugs.Contains(candidate) || reserved.Contains(candidate)); |
| | | 102 | | |
| | 5 | 103 | | return candidate; |
| | | 104 | | } |
| | | 105 | | } |