Skip to content

Commit 44c5b7f

Browse files
committed
fix: 修复Wiki API的三个关键问题
1. 修复Revisions API返回格式问题(Issue #2 Problem 3) - 使用convert.ToWikiCommitList()正确包装响应 - 确保前端可以访问.commits属性 2. 修复GET API内容缺失和404错误(Issue #2 Problems 1 & 2) - 同时尝试查找unescaped和escaped两种文件格式 - 与Service层的prepareGitPath()逻辑保持一致 - 修复了大部分非Home页面返回404的问题 3. 修复PATCH API标题处理错误(Issue #1) - 在转换title前先比较是否与当前标题相同 - 避免不必要的路径转换和意外重命名 - 确保PATCH请求正确更新指定的页面
1 parent 58dce9b commit 44c5b7f

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

routers/api/v1/repo/wiki.go

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,14 @@ func EditWikiPage(ctx *context.APIContext) {
143143
if form.Title == "" {
144144
newWikiName = oldWikiName
145145
} else {
146-
newWikiName = wiki_service.UserTitleToWebPath("", form.Title)
146+
// Check if the new title is the same as the current title to avoid unnecessary conversion
147+
_, currentTitle := wiki_service.WebPathToUserTitle(oldWikiName)
148+
if strings.TrimSpace(form.Title) == currentTitle {
149+
// Title unchanged, keep the original WebPath to avoid encoding inconsistencies
150+
newWikiName = oldWikiName
151+
} else {
152+
newWikiName = wiki_service.UserTitleToWebPath("", form.Title)
153+
}
147154
}
148155

149156
if len(form.Message) == 0 {
@@ -448,11 +455,8 @@ func ListPageRevisions(ctx *context.APIContext) {
448455
return
449456
}
450457

451-
// Convert commits to API format
452-
result := make([]*api.WikiCommit, len(commitsHistory))
453-
for i := range commitsHistory {
454-
result[i] = convert.ToWikiCommit(commitsHistory[i])
455-
}
458+
// Convert commits to API format and wrap in WikiCommitList
459+
result := convert.ToWikiCommitList(commitsHistory, commitsCount)
456460

457461
ctx.SetTotalCountHeader(commitsCount)
458462
ctx.JSON(http.StatusOK, result)
@@ -519,8 +523,18 @@ func wikiContentsByEntry(ctx *context.APIContext, entry *git.TreeEntry) string {
519523
// wikiContentsByName returns the contents of a wiki page, along with a boolean
520524
// indicating whether the page exists. Writes to ctx if an error occurs.
521525
func wikiContentsByName(ctx *context.APIContext, commit *git.Commit, wikiName wiki_service.WebPath, isSidebarOrFooter bool) (string, string) {
522-
gitFilename := wiki_service.WebPathToGitPath(wikiName)
523-
entry, err := findEntryForFile(commit, gitFilename)
526+
// Try both unescaped and escaped versions of the filename for compatibility
527+
unescaped := string(wikiName) + ".md"
528+
gitPath := wiki_service.WebPathToGitPath(wikiName)
529+
530+
// First try the unescaped version
531+
entry, err := findEntryForFile(commit, unescaped)
532+
if err == nil && entry != nil {
533+
return wikiContentsByEntry(ctx, entry), unescaped
534+
}
535+
536+
// If not found, try the escaped gitPath version
537+
entry, err = findEntryForFile(commit, gitPath)
524538
if err != nil {
525539
if git.IsErrNotExist(err) {
526540
if !isSidebarOrFooter {
@@ -531,5 +545,5 @@ func wikiContentsByName(ctx *context.APIContext, commit *git.Commit, wikiName wi
531545
}
532546
return "", ""
533547
}
534-
return wikiContentsByEntry(ctx, entry), gitFilename
548+
return wikiContentsByEntry(ctx, entry), gitPath
535549
}

0 commit comments

Comments
 (0)