mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-11-08 18:04:14 +01:00
Fix rendering of wiki page list if wiki repo contains other files (#3454)
* Fix rendering of wiki page list if wiki repo contains other files * Improve wiki filename tests
This commit is contained in:
parent
b16c84de7b
commit
283e87d814
4 changed files with 26 additions and 2 deletions
|
@ -216,6 +216,21 @@ func (err ErrWikiReservedName) Error() string {
|
||||||
return fmt.Sprintf("wiki title is reserved: %s", err.Title)
|
return fmt.Sprintf("wiki title is reserved: %s", err.Title)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ErrWikiInvalidFileName represents an invalid wiki file name.
|
||||||
|
type ErrWikiInvalidFileName struct {
|
||||||
|
FileName string
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsErrWikiInvalidFileName checks if an error is an ErrWikiInvalidFileName.
|
||||||
|
func IsErrWikiInvalidFileName(err error) bool {
|
||||||
|
_, ok := err.(ErrWikiInvalidFileName)
|
||||||
|
return ok
|
||||||
|
}
|
||||||
|
|
||||||
|
func (err ErrWikiInvalidFileName) Error() string {
|
||||||
|
return fmt.Sprintf("Invalid wiki filename: %s", err.FileName)
|
||||||
|
}
|
||||||
|
|
||||||
// __________ ___. .__ .__ ____ __.
|
// __________ ___. .__ .__ ____ __.
|
||||||
// \______ \__ _\_ |__ | | |__| ____ | |/ _|____ ___.__.
|
// \______ \__ _\_ |__ | | |__| ____ | |/ _|____ ___.__.
|
||||||
// | ___/ | \ __ \| | | |/ ___\ | <_/ __ < | |
|
// | ___/ | \ __ \| | | |/ ___\ | <_/ __ < | |
|
||||||
|
|
|
@ -45,7 +45,7 @@ func WikiNameToFilename(name string) string {
|
||||||
// WikiFilenameToName converts a wiki filename to its corresponding page name.
|
// WikiFilenameToName converts a wiki filename to its corresponding page name.
|
||||||
func WikiFilenameToName(filename string) (string, error) {
|
func WikiFilenameToName(filename string) (string, error) {
|
||||||
if !strings.HasSuffix(filename, ".md") {
|
if !strings.HasSuffix(filename, ".md") {
|
||||||
return "", fmt.Errorf("Invalid wiki filename: %s", filename)
|
return "", ErrWikiInvalidFileName{filename}
|
||||||
}
|
}
|
||||||
basename := filename[:len(filename)-3]
|
basename := filename[:len(filename)-3]
|
||||||
unescaped, err := url.QueryUnescape(basename)
|
unescaped, err := url.QueryUnescape(basename)
|
||||||
|
|
|
@ -77,11 +77,14 @@ func TestWikiFilenameToName(t *testing.T) {
|
||||||
for _, badFilename := range []string{
|
for _, badFilename := range []string{
|
||||||
"nofileextension",
|
"nofileextension",
|
||||||
"wrongfileextension.txt",
|
"wrongfileextension.txt",
|
||||||
"badescaping%%.md",
|
|
||||||
} {
|
} {
|
||||||
_, err := WikiFilenameToName(badFilename)
|
_, err := WikiFilenameToName(badFilename)
|
||||||
assert.Error(t, err)
|
assert.Error(t, err)
|
||||||
|
assert.True(t, IsErrWikiInvalidFileName(err))
|
||||||
}
|
}
|
||||||
|
_, err := WikiFilenameToName("badescaping%%.md")
|
||||||
|
assert.Error(t, err)
|
||||||
|
assert.False(t, IsErrWikiInvalidFileName(err))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestWikiNameToFilenameToName(t *testing.T) {
|
func TestWikiNameToFilenameToName(t *testing.T) {
|
||||||
|
|
|
@ -128,6 +128,9 @@ func renderWikiPage(ctx *context.Context, isViewPage bool) (*git.Repository, *gi
|
||||||
}
|
}
|
||||||
wikiName, err := models.WikiFilenameToName(entry.Name())
|
wikiName, err := models.WikiFilenameToName(entry.Name())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if models.IsErrWikiInvalidFileName(err) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
ctx.ServerError("WikiFilenameToName", err)
|
ctx.ServerError("WikiFilenameToName", err)
|
||||||
return nil, nil
|
return nil, nil
|
||||||
} else if wikiName == "_Sidebar" || wikiName == "_Footer" {
|
} else if wikiName == "_Sidebar" || wikiName == "_Footer" {
|
||||||
|
@ -262,6 +265,9 @@ func WikiPages(ctx *context.Context) {
|
||||||
}
|
}
|
||||||
wikiName, err := models.WikiFilenameToName(entry.Name())
|
wikiName, err := models.WikiFilenameToName(entry.Name())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if models.IsErrWikiInvalidFileName(err) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
ctx.ServerError("WikiFilenameToName", err)
|
ctx.ServerError("WikiFilenameToName", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue