From a4ccf6501123157bdf84b5b2be85dcd6b77fd1ce Mon Sep 17 00:00:00 2001 From: Aly Sewelam Date: Wed, 26 Nov 2025 17:25:30 +0200 Subject: [PATCH] use config option for default wiki format paths - listing of wiki pages now depends on option and defaults to markdown only --- modules/setting/repository.go | 1 + routers/api/v1/repo/wiki.go | 1 + services/wiki/wiki.go | 42 ++++++++++++++++++++++++++++------- services/wiki/wiki_path.go | 13 ++++++++++- 4 files changed, 48 insertions(+), 9 deletions(-) diff --git a/modules/setting/repository.go b/modules/setting/repository.go index d952420e71..8c499774f4 100644 --- a/modules/setting/repository.go +++ b/modules/setting/repository.go @@ -286,6 +286,7 @@ func loadRepositoryFrom(rootCfg ConfigProvider) { Repository.GoGetCloneURLProtocol = sec.Key("GO_GET_CLONE_URL_PROTOCOL").MustString("https") Repository.MaxCreationLimit = sec.Key("MAX_CREATION_LIMIT").MustInt(-1) Repository.DefaultBranch = sec.Key("DEFAULT_BRANCH").MustString(Repository.DefaultBranch) + Repository.DefaultWikiFormat = sec.Key("WIKI_FORMAT").MustString(Repository.DefaultWikiFormat) RepoRootPath = sec.Key("ROOT").MustString(filepath.Join(AppDataPath, "gitea-repositories")) if !filepath.IsAbs(RepoRootPath) { RepoRootPath = filepath.Join(AppWorkPath, RepoRootPath) diff --git a/routers/api/v1/repo/wiki.go b/routers/api/v1/repo/wiki.go index 8e24ffa465..357b37e3d8 100644 --- a/routers/api/v1/repo/wiki.go +++ b/routers/api/v1/repo/wiki.go @@ -8,6 +8,7 @@ import ( "fmt" "net/http" "net/url" + "strings" repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/modules/git" diff --git a/services/wiki/wiki.go b/services/wiki/wiki.go index 18fd8ba057..fdba8d870f 100644 --- a/services/wiki/wiki.go +++ b/services/wiki/wiki.go @@ -21,6 +21,7 @@ import ( "code.gitea.io/gitea/modules/graceful" "code.gitea.io/gitea/modules/log" repo_module "code.gitea.io/gitea/modules/repository" + "code.gitea.io/gitea/modules/setting" asymkey_service "code.gitea.io/gitea/services/asymkey" repo_service "code.gitea.io/gitea/services/repository" ) @@ -54,13 +55,26 @@ func InitWiki(ctx context.Context, repo *repo_model.Repository) error { // prepareGitPath try to find a suitable file path with file name by the given raw wiki name. // return: existence, prepared file path with name, error -func prepareGitPath(gitRepo *git.Repository, defaultWikiBranch string, wikiPath WebPath) (bool, string, error) { +func prepareGitPath(gitRepo *git.Repository, defaultWikiBranch string, wikiPath WebPath, defaultWikiFormat string) (bool, string, error) { unescapedMd := string(wikiPath) + ".md" unescapedOrg := string(wikiPath) + ".org" gitPath := WebPathToGitPath(wikiPath) - // Look for .md, .org, and escaped file - filesInIndex, err := gitRepo.LsTree(defaultWikiBranch, unescapedMd, unescapedOrg, gitPath) + // Build list of files to look for based on defaultWikiFormat + var filesToCheck []string + checkMarkdown := defaultWikiFormat == "markdown" || defaultWikiFormat == "both" + checkOrg := defaultWikiFormat == "org" || defaultWikiFormat == "both" + + if checkMarkdown { + filesToCheck = append(filesToCheck, unescapedMd) + } + if checkOrg { + filesToCheck = append(filesToCheck, unescapedOrg) + } + filesToCheck = append(filesToCheck, gitPath) + + // Look for files based on format setting + filesInIndex, err := gitRepo.LsTree(defaultWikiBranch, filesToCheck...) if err != nil { if strings.Contains(err.Error(), "Not a valid object name") { return false, gitPath, nil // branch doesn't exist @@ -74,9 +88,13 @@ func prepareGitPath(gitRepo *git.Repository, defaultWikiBranch string, wikiPath switch filename { // if we find unescaped file (.md or .org) return it case unescapedMd: - return true, unescapedMd, nil + if checkMarkdown { + return true, unescapedMd, nil + } case unescapedOrg: - return true, unescapedOrg, nil + if checkOrg { + return true, unescapedOrg, nil + } case gitPath: foundEscaped = true } @@ -142,7 +160,11 @@ func updateWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model } } - isWikiExist, newWikiPath, err := prepareGitPath(gitRepo, repo.DefaultWikiBranch, newWikiName) + defaultWikiFormat := repo.DefaultWikiFormat + if defaultWikiFormat == "" { + defaultWikiFormat = setting.Repository.DefaultWikiFormat + } + isWikiExist, newWikiPath, err := prepareGitPath(gitRepo, repo.DefaultWikiBranch, newWikiName, defaultWikiFormat) if err != nil { return err } @@ -158,7 +180,7 @@ func updateWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model isOldWikiExist := true oldWikiPath := newWikiPath if oldWikiName != newWikiName { - isOldWikiExist, oldWikiPath, err = prepareGitPath(gitRepo, repo.DefaultWikiBranch, oldWikiName) + isOldWikiExist, oldWikiPath, err = prepareGitPath(gitRepo, repo.DefaultWikiBranch, oldWikiName, defaultWikiFormat) if err != nil { return err } @@ -294,7 +316,11 @@ func DeleteWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model return fmt.Errorf("unable to read HEAD tree to index in: %s %w", basePath, err) } - found, wikiPath, err := prepareGitPath(gitRepo, repo.DefaultWikiBranch, wikiName) + defaultWikiFormat := repo.DefaultWikiFormat + if defaultWikiFormat == "" { + defaultWikiFormat = setting.Repository.DefaultWikiFormat + } + found, wikiPath, err := prepareGitPath(gitRepo, repo.DefaultWikiBranch, wikiName, defaultWikiFormat) if err != nil { return err } diff --git a/services/wiki/wiki_path.go b/services/wiki/wiki_path.go index 16b1da86ab..2fd057640c 100644 --- a/services/wiki/wiki_path.go +++ b/services/wiki/wiki_path.go @@ -10,6 +10,7 @@ import ( repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/services/convert" @@ -101,6 +102,9 @@ func WebPathToGitPath(s WebPath) string { return util.PathJoinRelX(ret) } + // Get default wiki format from global setting + defaultWikiFormat := setting.Repository.DefaultWikiFormat + a := strings.Split(string(s), "/") for i := range a { shouldAddDashMarker := hasDashMarker(a[i]) @@ -109,7 +113,14 @@ func WebPathToGitPath(s WebPath) string { a[i] = strings.ReplaceAll(a[i], "%20", " ") // space is safe to be kept in git path a[i] = strings.ReplaceAll(a[i], "+", " ") } - return strings.Join(a, "/") + ".md" + basePath := strings.Join(a, "/") + + // Determine extension based on format setting + if defaultWikiFormat == "org" { + return basePath + ".org" + } + // For "both" or "markdown", default to .md + return basePath + ".md" } func GitPathToWebPath(s string) (wp WebPath, err error) {