diff options
| author | gramanas <grm@eyesin.space> | 2026-09-12 11:24:17 +0300 |
|---|---|---|
| committer | gramanas <grm@eyesin.space> | 2026-09-12 11:24:17 +0300 |
| commit | 3eb04b1a2bdf9e53231fe862cfd76327371a9741 (patch) | |
| tree | b38b2d82a47233fd8e0bb18c59e4a8f3dd2412d7 /internal/slug/slug.go | |
| download | blogspace-3eb04b1a2bdf9e53231fe862cfd76327371a9741.tar.gz blogspace-3eb04b1a2bdf9e53231fe862cfd76327371a9741.tar.bz2 blogspace-3eb04b1a2bdf9e53231fe862cfd76327371a9741.zip | |
Initial multi-tenant blog host
Go + Postgres application serving a management dashboard on the base
domain and one public blog per subdomain. Markdown posts organised in
pages, form-based theme customisation, image uploads stored in Postgres,
JWT cookie sessions with CSRF, superadmin user management, RSS feeds.
Docker/compose deployment and a Makefile-driven dev environment with
seed data.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Sd8UPWrvyYCLj97JexNw3A
Diffstat (limited to 'internal/slug/slug.go')
| -rw-r--r-- | internal/slug/slug.go | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/internal/slug/slug.go b/internal/slug/slug.go new file mode 100644 index 0000000..a3c99a4 --- /dev/null +++ b/internal/slug/slug.go @@ -0,0 +1,54 @@ +// Package slug turns titles into URL-safe identifiers. +package slug + +import ( + "fmt" + "regexp" + "strings" + "unicode" + + "golang.org/x/text/runes" + "golang.org/x/text/transform" + "golang.org/x/text/unicode/norm" +) + +var ( + nonAlnum = regexp.MustCompile(`[^a-z0-9]+`) + validRe = regexp.MustCompile(`^[a-z0-9](?:-?[a-z0-9])*$`) + stripMarks = transform.Chain(norm.NFD, runes.Remove(runes.In(unicode.Mn)), norm.NFC) +) + +// greek maps Greek letters to Latin so Greek titles get readable slugs. +var greek = strings.NewReplacer( + "α", "a", "β", "v", "γ", "g", "δ", "d", "ε", "e", "ζ", "z", "η", "i", "θ", "th", + "ι", "i", "κ", "k", "λ", "l", "μ", "m", "ν", "n", "ξ", "x", "ο", "o", "π", "p", + "ρ", "r", "σ", "s", "ς", "s", "τ", "t", "υ", "y", "φ", "f", "χ", "ch", "ψ", "ps", "ω", "o", +) + +// Make returns a slug for s; falls back to "untitled" when nothing usable remains. +func Make(s string) string { + if t, _, err := transform.String(stripMarks, s); err == nil { + s = t + } + s = greek.Replace(strings.ToLower(s)) + s = nonAlnum.ReplaceAllString(s, "-") + s = strings.Trim(s, "-") + if len(s) > 80 { + s = strings.Trim(s[:80], "-") + } + if s == "" { + return "untitled" + } + return s +} + +// WithSuffix returns "base-n" (or base when n < 2), used to resolve collisions. +func WithSuffix(base string, n int) string { + if n < 2 { + return base + } + return fmt.Sprintf("%s-%d", base, n) +} + +// Valid reports whether s is an acceptable slug as typed by a user. +func Valid(s string) bool { return validRe.MatchString(s) && len(s) <= 80 } |
