Skip to content

Commit b39a5bb

Browse files
wxiaoguangGiteaBot
andauthored
Make wiki title supports dashes and improve wiki name related features (#24143)
Close #7570 1. Clearly define the wiki path behaviors, see `services/wiki/wiki_path.go` and tests 2. Keep compatibility with old contents 3. Allow to use dashes in titles, eg: "2000-01-02 Meeting record" 4. Add a "Pages" link in the dropdown, otherwise users can't go to the Pages page easily. 5. Add a "View original git file" link in the Pages list, even if some file names are broken, users still have a chance to edit or remove it, without cloning the wiki repo to local. 6. Fix 500 error when the name contains prefix spaces. This PR also introduces the ability to support sub-directories, but it can't be done at the moment due to there are a lot of legacy wiki data, which use "%2F" in file names. ![image](https://user-images.githubusercontent.com/2114189/232239004-3359d7b9-7bf3-4ff3-8446-bfb0e79645dd.png) ![image](https://user-images.githubusercontent.com/2114189/232239020-74b92c72-bf73-4377-a319-1c85609f82b1.png) Co-authored-by: Giteabot <teabot@gitea.io>
1 parent 738f2af commit b39a5bb

File tree

13 files changed

+400
-256
lines changed

13 files changed

+400
-256
lines changed

modules/git/repo_commit.go

+3
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ func (repo *Repository) GetCommitByPath(relpath string) (*Commit, error) {
8484
if err != nil {
8585
return nil, err
8686
}
87+
if len(commits) == 0 {
88+
return nil, ErrNotExist{ID: relpath}
89+
}
8790
return commits[0], nil
8891
}
8992

options/locale/locale_en-US.ini

+1
Original file line numberDiff line numberDiff line change
@@ -1791,6 +1791,7 @@ wiki.reserved_page = The wiki page name "%s" is reserved.
17911791
wiki.pages = Pages
17921792
wiki.last_updated = Last updated %s
17931793
wiki.page_name_desc = Enter a name for this Wiki page. Some special names are: 'Home', '_Sidebar' and '_Footer'.
1794+
wiki.original_git_entry_tooltip = View original Git file instead of using friendly link.
17941795

17951796
activity = Activity
17961797
activity.period.filter_label = Period:

routers/api/v1/repo/wiki.go

+19-21
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ func NewWikiPage(ctx *context.APIContext) {
5858
return
5959
}
6060

61-
wikiName := wiki_service.NormalizeWikiName(form.Title)
61+
wikiName := wiki_service.UserTitleToWebPath("", form.Title)
6262

6363
if len(form.Message) == 0 {
64-
form.Message = fmt.Sprintf("Add '%s'", form.Title)
64+
form.Message = fmt.Sprintf("Add %q", form.Title)
6565
}
6666

6767
content, err := base64.StdEncoding.DecodeString(form.ContentBase64)
@@ -85,7 +85,7 @@ func NewWikiPage(ctx *context.APIContext) {
8585
wikiPage := getWikiPage(ctx, wikiName)
8686

8787
if !ctx.Written() {
88-
notification.NotifyNewWikiPage(ctx, ctx.Doer, ctx.Repo.Repository, wikiName, form.Message)
88+
notification.NotifyNewWikiPage(ctx, ctx.Doer, ctx.Repo.Repository, string(wikiName), form.Message)
8989
ctx.JSON(http.StatusCreated, wikiPage)
9090
}
9191
}
@@ -127,15 +127,15 @@ func EditWikiPage(ctx *context.APIContext) {
127127

128128
form := web.GetForm(ctx).(*api.CreateWikiPageOptions)
129129

130-
oldWikiName := wiki_service.NormalizeWikiName(ctx.Params(":pageName"))
131-
newWikiName := wiki_service.NormalizeWikiName(form.Title)
130+
oldWikiName := wiki_service.WebPathFromRequest(ctx.Params(":pageName"))
131+
newWikiName := wiki_service.UserTitleToWebPath("", form.Title)
132132

133133
if len(newWikiName) == 0 {
134134
newWikiName = oldWikiName
135135
}
136136

137137
if len(form.Message) == 0 {
138-
form.Message = fmt.Sprintf("Update '%s'", newWikiName)
138+
form.Message = fmt.Sprintf("Update %q", newWikiName)
139139
}
140140

141141
content, err := base64.StdEncoding.DecodeString(form.ContentBase64)
@@ -153,14 +153,12 @@ func EditWikiPage(ctx *context.APIContext) {
153153
wikiPage := getWikiPage(ctx, newWikiName)
154154

155155
if !ctx.Written() {
156-
notification.NotifyEditWikiPage(ctx, ctx.Doer, ctx.Repo.Repository, newWikiName, form.Message)
156+
notification.NotifyEditWikiPage(ctx, ctx.Doer, ctx.Repo.Repository, string(newWikiName), form.Message)
157157
ctx.JSON(http.StatusOK, wikiPage)
158158
}
159159
}
160160

161-
func getWikiPage(ctx *context.APIContext, title string) *api.WikiPage {
162-
title = wiki_service.NormalizeWikiName(title)
163-
161+
func getWikiPage(ctx *context.APIContext, wikiName wiki_service.WebPath) *api.WikiPage {
164162
wikiRepo, commit := findWikiRepoCommit(ctx)
165163
if wikiRepo != nil {
166164
defer wikiRepo.Close()
@@ -170,7 +168,7 @@ func getWikiPage(ctx *context.APIContext, title string) *api.WikiPage {
170168
}
171169

172170
// lookup filename in wiki - get filecontent, real filename
173-
content, pageFilename := wikiContentsByName(ctx, commit, title, false)
171+
content, pageFilename := wikiContentsByName(ctx, commit, wikiName, false)
174172
if ctx.Written() {
175173
return nil
176174
}
@@ -196,7 +194,7 @@ func getWikiPage(ctx *context.APIContext, title string) *api.WikiPage {
196194
}
197195

198196
return &api.WikiPage{
199-
WikiPageMetaData: convert.ToWikiPageMetaData(title, lastCommit, ctx.Repo.Repository),
197+
WikiPageMetaData: convert.ToWikiPageMetaData(wikiName, lastCommit, ctx.Repo.Repository),
200198
ContentBase64: content,
201199
CommitCount: commitsCount,
202200
Sidebar: sidebarContent,
@@ -233,7 +231,7 @@ func DeleteWikiPage(ctx *context.APIContext) {
233231
// "404":
234232
// "$ref": "#/responses/notFound"
235233

236-
wikiName := wiki_service.NormalizeWikiName(ctx.Params(":pageName"))
234+
wikiName := wiki_service.WebPathFromRequest(ctx.Params(":pageName"))
237235

238236
if err := wiki_service.DeleteWikiPage(ctx, ctx.Doer, ctx.Repo.Repository, wikiName); err != nil {
239237
if err.Error() == "file does not exist" {
@@ -244,7 +242,7 @@ func DeleteWikiPage(ctx *context.APIContext) {
244242
return
245243
}
246244

247-
notification.NotifyDeleteWikiPage(ctx, ctx.Doer, ctx.Repo.Repository, wikiName)
245+
notification.NotifyDeleteWikiPage(ctx, ctx.Doer, ctx.Repo.Repository, string(wikiName))
248246

249247
ctx.Status(http.StatusNoContent)
250248
}
@@ -316,7 +314,7 @@ func ListWikiPages(ctx *context.APIContext) {
316314
ctx.Error(http.StatusInternalServerError, "GetCommit", err)
317315
return
318316
}
319-
wikiName, err := wiki_service.FilenameToName(entry.Name())
317+
wikiName, err := wiki_service.GitPathToWebPath(entry.Name())
320318
if err != nil {
321319
if repo_model.IsErrWikiInvalidFileName(err) {
322320
continue
@@ -361,7 +359,7 @@ func GetWikiPage(ctx *context.APIContext) {
361359
// "$ref": "#/responses/notFound"
362360

363361
// get requested pagename
364-
pageName := wiki_service.NormalizeWikiName(ctx.Params(":pageName"))
362+
pageName := wiki_service.WebPathFromRequest(ctx.Params(":pageName"))
365363

366364
wikiPage := getWikiPage(ctx, pageName)
367365
if !ctx.Written() {
@@ -411,7 +409,7 @@ func ListPageRevisions(ctx *context.APIContext) {
411409
}
412410

413411
// get requested pagename
414-
pageName := wiki_service.NormalizeWikiName(ctx.Params(":pageName"))
412+
pageName := wiki_service.WebPathFromRequest(ctx.Params(":pageName"))
415413
if len(pageName) == 0 {
416414
pageName = "Home"
417415
}
@@ -502,9 +500,9 @@ func wikiContentsByEntry(ctx *context.APIContext, entry *git.TreeEntry) string {
502500

503501
// wikiContentsByName returns the contents of a wiki page, along with a boolean
504502
// indicating whether the page exists. Writes to ctx if an error occurs.
505-
func wikiContentsByName(ctx *context.APIContext, commit *git.Commit, wikiName string, isSidebarOrFooter bool) (string, string) {
506-
pageFilename := wiki_service.NameToFilename(wikiName)
507-
entry, err := findEntryForFile(commit, pageFilename)
503+
func wikiContentsByName(ctx *context.APIContext, commit *git.Commit, wikiName wiki_service.WebPath, isSidebarOrFooter bool) (string, string) {
504+
gitFilename := wiki_service.WebPathToGitPath(wikiName)
505+
entry, err := findEntryForFile(commit, gitFilename)
508506
if err != nil {
509507
if git.IsErrNotExist(err) {
510508
if !isSidebarOrFooter {
@@ -515,5 +513,5 @@ func wikiContentsByName(ctx *context.APIContext, commit *git.Commit, wikiName st
515513
}
516514
return "", ""
517515
}
518-
return wikiContentsByEntry(ctx, entry), pageFilename
516+
return wikiContentsByEntry(ctx, entry), gitFilename
519517
}

0 commit comments

Comments
 (0)