mirror of
https://github.com/khoaliber/LetterFeed.git
synced 2026-03-02 21:19:13 +00:00
feat: custom newsletter slug
This commit is contained in:
18
backend/app/core/slug.py
Normal file
18
backend/app/core/slug.py
Normal file
@@ -0,0 +1,18 @@
|
||||
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
|
||||
Reference in New Issue
Block a user