mirror of
https://github.com/khoaliber/LetterFeed.git
synced 2026-03-02 13:18:27 +00:00
19 lines
498 B
Python
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
|