Files
LetterFeed/backend/app/core/slug.py
2025-07-24 13:20:22 +02:00

19 lines
498 B
Python

import re
def sanitize_slug(slug: str | None) -> str | None:
"""Sanitize a string to be used as a URL slug.
- Converts to lowercase
- Replaces spaces and underscores with hyphens
- Removes characters that are not alphanumeric or hyphens
- Removes leading/trailing hyphens
"""
if not slug:
return None
slug = slug.lower()
slug = re.sub(r"[\s_]+", "-", slug)
slug = re.sub(r"[^a-z0-9-]", "", slug)
slug = slug.strip("-")
return slug or None