Files
wehub-resource-sync a06f331eb8
CI / benchmark (push) Has been skipped
install-script / posix-syntax (push) Successful in 6m1s
CI / build-onnx (push) Failing after 6m43s
init-smoke / dry-run (push) Failing after 15m57s
security / govulncheck (push) Has been cancelled
security / trivy-fs (push) Has been cancelled
CI / test (1.26, ubuntu-latest) (push) Has been cancelled
Scorecard supply-chain security / Scorecard analysis (push) Has been cancelled
CI / test (1.26, macos-latest) (push) Has been cancelled
CI / build-windows (push) Has been cancelled
CI / lint (push) Has been cancelled
install-script / powershell-syntax (push) Has been cancelled
install-script / install (macos-14) (push) Has been cancelled
install-script / install (ubuntu-latest) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:33:42 +08:00

40 lines
966 B
Go

package wiki
import (
"path/filepath"
"regexp"
"strings"
)
var slugRE = regexp.MustCompile(`[^a-z0-9]+`)
// slugify lowercases s and collapses runs of non-alphanumerics into a
// single dash. Returns "" only for the empty input.
func slugify(s string) string {
s = strings.ToLower(strings.TrimSpace(s))
s = slugRE.ReplaceAllString(s, "-")
return strings.Trim(s, "-")
}
// RepoSlugFromPath derives a filesystem-safe slug for the per-repo
// directory under wiki/. Uses the basename of the absolute path
// (after resolving symlinks where possible) and falls back to "repo"
// when the basename is empty or just "/".
func RepoSlugFromPath(repoPath string) string {
if repoPath == "" {
return "repo"
}
abs, err := filepath.Abs(repoPath)
if err == nil {
repoPath = abs
}
base := filepath.Base(filepath.Clean(repoPath))
if base == "/" || base == "." || base == "" {
return "repo"
}
if s := slugify(base); s != "" {
return s
}
return "repo"
}