Skip to content

Commit f9a16ac

Browse files
committed
Add directory support for wiki
1 parent df98452 commit f9a16ac

File tree

3 files changed

+62
-18
lines changed

3 files changed

+62
-18
lines changed

routers/web/repo/wiki.go

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"io"
1212
"net/http"
1313
"net/url"
14+
"path"
1415
"path/filepath"
1516
"strings"
1617

@@ -72,6 +73,7 @@ func MustEnableWiki(ctx *context.Context) {
7273
// PageMeta wiki page meta information
7374
type PageMeta struct {
7475
Name string
76+
IsDir bool
7577
SubURL string
7678
GitEntryName string
7779
UpdatedUnix timeutil.TimeStamp
@@ -190,8 +192,23 @@ func renderViewPage(ctx *context.Context) (*git.Repository, *git.TreeEntry) {
190192
return nil, nil
191193
}
192194

195+
reqPath := util.PathJoinRelX(ctx.PathParamRaw("*"))
196+
p, err := url.PathUnescape(reqPath)
197+
if err != nil {
198+
ctx.ServerError("PathUnescape", err)
199+
return nil, nil
200+
}
201+
dirPath, pageName := path.Split(p)
202+
pageName = strings.TrimSuffix(pageName, ".md")
203+
treePath := wiki_service.WebDirPathToGitPath(wiki_service.WebPath(dirPath))
204+
tree, err := commit.SubTree(treePath)
205+
if err != nil {
206+
ctx.ServerError("SubTree", err)
207+
return nil, nil
208+
}
209+
193210
// Get page list.
194-
entries, err := commit.ListEntries()
211+
entries, err := tree.ListEntries()
195212
if err != nil {
196213
if wikiRepo != nil {
197214
wikiRepo.Close()
@@ -227,13 +244,12 @@ func renderViewPage(ctx *context.Context) (*git.Repository, *git.TreeEntry) {
227244
ctx.Data["Pages"] = pages
228245

229246
// get requested page name
230-
pageName := wiki_service.WebPathFromRequest(ctx.PathParamRaw("*"))
231247
if len(pageName) == 0 {
232248
pageName = "Home"
233249
}
234250

235-
_, displayName := wiki_service.WebPathToUserTitle(pageName)
236-
ctx.Data["PageURL"] = wiki_service.WebPathToURLPath(pageName)
251+
_, displayName := wiki_service.WebPathToUserTitle(wiki_service.WebPath(pageName))
252+
ctx.Data["PageURL"] = wiki_service.WebPathToURLPath(wiki_service.WebPath(pageName))
237253
ctx.Data["old_title"] = displayName
238254
ctx.Data["Title"] = displayName
239255
ctx.Data["title"] = displayName
@@ -242,7 +258,7 @@ func renderViewPage(ctx *context.Context) (*git.Repository, *git.TreeEntry) {
242258
isFooter := pageName == "_Footer"
243259

244260
// lookup filename in wiki - get gitTree entry , real filename
245-
entry, pageFilename, noEntry, isRaw := wikiEntryByName(ctx, commit, pageName)
261+
entry, pageFilename, noEntry, isRaw := wikiEntryByName(ctx, commit, wiki_service.WebPath(p))
246262
if noEntry {
247263
ctx.Redirect(ctx.Repo.RepoLink + "/wiki/?action=_pages")
248264
}
@@ -647,7 +663,8 @@ func WikiPages(ctx *context.Context) {
647663
return
648664
}
649665

650-
treePath := "" // To support list sub folders' pages in the future
666+
dirPath := util.PathJoinRelX(ctx.PathParamRaw("*"))
667+
treePath := wiki_service.WebDirPathToGitPath(wiki_service.WebPath(dirPath))
651668
tree, err := commit.SubTree(treePath)
652669
if err != nil {
653670
ctx.ServerError("SubTree", err)
@@ -669,22 +686,33 @@ func WikiPages(ctx *context.Context) {
669686

670687
pages := make([]PageMeta, 0, len(entries))
671688
for _, entry := range entries {
672-
if !entry.Entry.IsRegular() {
689+
if !entry.Entry.IsRegular() && !entry.Entry.IsDir() {
673690
continue
674691
}
675-
wikiName, err := wiki_service.GitPathToWebPath(entry.Entry.Name())
676-
if err != nil {
677-
if repo_model.IsErrWikiInvalidFileName(err) {
678-
continue
692+
var wikiName wiki_service.WebPath
693+
if entry.Entry.IsDir() {
694+
wikiName, err = wiki_service.GitDirPathToWebPath(entry.Entry.Name())
695+
if err != nil {
696+
ctx.ServerError("GitDirPathToWebPath", err)
697+
return
698+
}
699+
} else {
700+
wikiName, err = wiki_service.GitPathToWebPath(entry.Entry.Name())
701+
if err != nil {
702+
if repo_model.IsErrWikiInvalidFileName(err) {
703+
continue
704+
}
705+
ctx.ServerError("GitPathToWebPath", err)
706+
return
679707
}
680-
ctx.ServerError("WikiFilenameToName", err)
681-
return
682708
}
683709
_, displayName := wiki_service.WebPathToUserTitle(wikiName)
710+
urlPath := path.Join(string(dirPath), string(wikiName))
684711
pages = append(pages, PageMeta{
685712
Name: displayName,
686-
SubURL: wiki_service.WebPathToURLPath(wikiName),
687-
GitEntryName: entry.Entry.Name(),
713+
IsDir: entry.Entry.IsDir(),
714+
SubURL: wiki_service.WebPathToURLPath(wiki_service.WebPath(urlPath)),
715+
GitEntryName: path.Join(string(dirPath), entry.Entry.Name()),
688716
UpdatedUnix: timeutil.TimeStamp(entry.Commit.Author.When.Unix()),
689717
})
690718
}

services/wiki/wiki_path.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ func WebPathToGitPath(s WebPath) string {
9999
return util.PathJoinRelX(ret)
100100
}
101101

102+
return WebDirPathToGitPath(s) + ".md"
103+
}
104+
105+
func WebDirPathToGitPath(s WebPath) string {
102106
a := strings.Split(string(s), "/")
103107
for i := range a {
104108
shouldAddDashMarker := hasDashMarker(a[i])
@@ -107,7 +111,7 @@ func WebPathToGitPath(s WebPath) string {
107111
a[i] = strings.ReplaceAll(a[i], "%20", " ") // space is safe to be kept in git path
108112
a[i] = strings.ReplaceAll(a[i], "+", " ")
109113
}
110-
return strings.Join(a, "/") + ".md"
114+
return strings.Join(a, "/")
111115
}
112116

113117
func GitPathToWebPath(s string) (wp WebPath, err error) {
@@ -126,6 +130,18 @@ func GitPathToWebPath(s string) (wp WebPath, err error) {
126130
return WebPath(strings.Join(a, "/")), nil
127131
}
128132

133+
func GitDirPathToWebPath(s string) (wp WebPath, err error) {
134+
a := strings.Split(s, "/")
135+
for i := range a {
136+
shouldAddDashMarker := hasDashMarker(a[i])
137+
if a[i], err = unescapeSegment(a[i]); err != nil {
138+
return "", err
139+
}
140+
a[i] = escapeSegToWeb(a[i], shouldAddDashMarker)
141+
}
142+
return WebPath(strings.Join(a, "/")), nil
143+
}
144+
129145
func WebPathToUserTitle(s WebPath) (dir, display string) {
130146
dir = path.Dir(string(s))
131147
display = path.Base(string(s))

templates/repo/wiki/pages.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
{{range .Pages}}
1717
<tr>
1818
<td>
19-
{{svg "octicon-file"}}
20-
<a href="{{$.RepoLink}}/wiki/{{.SubURL}}">{{.Name}}</a>
19+
{{if .IsDir}}{{svg "octicon-file-directory"}}{{else}}{{svg "octicon-file"}}{{end}}
20+
<a href="{{$.RepoLink}}/wiki/{{.SubURL}}{{if .IsDir}}?action=_pages{{end}}">{{.Name}}</a>
2121
<a class="wiki-git-entry" href="{{$.RepoLink}}/wiki/{{.GitEntryName | PathEscape}}" data-tooltip-content="{{ctx.Locale.Tr "repo.wiki.original_git_entry_tooltip"}}">{{svg "octicon-chevron-right"}}</a>
2222
</td>
2323
{{$timeSince := DateUtils.TimeSince .UpdatedUnix}}

0 commit comments

Comments
 (0)