mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-12-30 15:43:52 +03:00
Prioritize "readme.md" (#5691)
* prioritize readme.md * Improve IsReadmeFile * Add more tests
This commit is contained in:
parent
6868378673
commit
bd75965296
3 changed files with 47 additions and 7 deletions
|
@ -111,9 +111,14 @@ func IsMarkupFile(name, markup string) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsReadmeFile reports whether name looks like a README file
|
// IsReadmeFile reports whether name looks like a README file
|
||||||
// based on its name.
|
// based on its name. If an extension is provided, it will strictly
|
||||||
func IsReadmeFile(name string) bool {
|
// match that extension.
|
||||||
|
// Note that the '.' should be provided in ext, e.g ".md"
|
||||||
|
func IsReadmeFile(name string, ext ...string) bool {
|
||||||
name = strings.ToLower(name)
|
name = strings.ToLower(name)
|
||||||
|
if len(ext) > 0 {
|
||||||
|
return name == "readme"+ext[0]
|
||||||
|
}
|
||||||
if len(name) < 6 {
|
if len(name) < 6 {
|
||||||
return false
|
return false
|
||||||
} else if len(name) == 6 {
|
} else if len(name) == 6 {
|
||||||
|
|
|
@ -19,6 +19,7 @@ func TestMisc_IsReadmeFile(t *testing.T) {
|
||||||
"README",
|
"README",
|
||||||
"readME.mdown",
|
"readME.mdown",
|
||||||
"README.md",
|
"README.md",
|
||||||
|
"readme.i18n.md",
|
||||||
}
|
}
|
||||||
falseTestCases := []string{
|
falseTestCases := []string{
|
||||||
"test.md",
|
"test.md",
|
||||||
|
@ -37,4 +38,25 @@ func TestMisc_IsReadmeFile(t *testing.T) {
|
||||||
for _, testCase := range falseTestCases {
|
for _, testCase := range falseTestCases {
|
||||||
assert.False(t, IsReadmeFile(testCase))
|
assert.False(t, IsReadmeFile(testCase))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
trueTestCasesStrict := [][]string{
|
||||||
|
{"readme", ""},
|
||||||
|
{"readme.md", ".md"},
|
||||||
|
{"readme.txt", ".txt"},
|
||||||
|
}
|
||||||
|
falseTestCasesStrict := [][]string{
|
||||||
|
{"readme", ".md"},
|
||||||
|
{"readme.md", ""},
|
||||||
|
{"readme.md", ".txt"},
|
||||||
|
{"readme.md", "md"},
|
||||||
|
{"readmee.md", ".md"},
|
||||||
|
{"readme.i18n.md", ".md"},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, testCase := range trueTestCasesStrict {
|
||||||
|
assert.True(t, IsReadmeFile(testCase[0], testCase[1]))
|
||||||
|
}
|
||||||
|
for _, testCase := range falseTestCasesStrict {
|
||||||
|
assert.False(t, IsReadmeFile(testCase[0], testCase[1]))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -56,18 +56,31 @@ func renderDirectory(ctx *context.Context, treeLink string) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var readmeFile *git.Blob
|
// 3 for the extensions in exts[] in order
|
||||||
|
// the last one is for a readme that doesn't
|
||||||
|
// strictly match an extension
|
||||||
|
var readmeFiles [4]*git.Blob
|
||||||
|
var exts = []string{".md", ".txt", ""} // sorted by priority
|
||||||
for _, entry := range entries {
|
for _, entry := range entries {
|
||||||
if entry.IsDir() {
|
if entry.IsDir() {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if !markup.IsReadmeFile(entry.Name()) {
|
for i, ext := range exts {
|
||||||
continue
|
if markup.IsReadmeFile(entry.Name(), ext) {
|
||||||
|
readmeFiles[i] = entry.Blob()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
readmeFile = entry.Blob()
|
if markup.IsReadmeFile(entry.Name()) {
|
||||||
if markup.Type(entry.Name()) != "" {
|
readmeFiles[3] = entry.Blob()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var readmeFile *git.Blob
|
||||||
|
for _, f := range readmeFiles {
|
||||||
|
if f != nil {
|
||||||
|
readmeFile = f
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue