From 1fb97eeeeb6969014cb816291e5551a98490ce42 Mon Sep 17 00:00:00 2001 From: Jonathan Miller Date: Tue, 26 May 2026 13:22:21 -0500 Subject: [PATCH] =?UTF-8?q?feat:=20smart=20wiki=20filenames=20=E2=80=94=20?= =?UTF-8?q?sanitize=20special=20characters=20to=20hyphens?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit New wiki page titles are now sanitized before creating the git file: - Spaces and special characters replaced with hyphens - Consecutive hyphens collapsed to single hyphen - Leading/trailing hyphens trimmed Examples: - "My Page Name" -> "My-Page-Name" - "API & Docs (v2)" -> "API-Docs-v2" - "100% Complete!!" -> "100-Complete" Only affects NEW pages. Existing wiki pages with legacy filenames (spaces, URL encoding) continue to work — the read path is unchanged. Co-Authored-By: Claude Opus 4.6 (1M context) --- services/wiki/wiki_path.go | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/services/wiki/wiki_path.go b/services/wiki/wiki_path.go index 91363322e1..4961cdb75c 100644 --- a/services/wiki/wiki_path.go +++ b/services/wiki/wiki_path.go @@ -6,6 +6,7 @@ package wiki import ( "net/url" "path" + "regexp" "strings" repo_model "git.mokoconsulting.tech/MokoConsulting/MokoGitea/models/repo" @@ -148,10 +149,24 @@ func WebPathFromRequest(s string) WebPath { return WebPath(s) } +var multiHyphenRe = regexp.MustCompile(`-{2,}`) +var nonAlphanumRe = regexp.MustCompile(`[^a-zA-Z0-9\-]`) + +// sanitizeWikiTitle converts a user-provided title into a clean, URL-friendly slug. +// Spaces and special characters become hyphens, consecutive hyphens collapse to one. +func sanitizeWikiTitle(title string) string { + title = strings.TrimSpace(title) + title = strings.ReplaceAll(title, " ", "-") + title = nonAlphanumRe.ReplaceAllString(title, "-") + title = multiHyphenRe.ReplaceAllString(title, "-") + title = strings.Trim(title, "-") + return title +} + func UserTitleToWebPath(base, title string) WebPath { // TODO: no support for subdirectory, because the old wiki code's behavior is always using %2F, instead of subdirectory. // So we do not add the support for writing slashes in title at the moment. - title = strings.TrimSpace(title) + title = sanitizeWikiTitle(title) title = util.PathJoinRelX(base, escapeSegToWeb(title, false)) if title == "" || title == "." { title = "unnamed" -- 2.52.0