更改:readme显示顺序

This commit is contained in:
yystopf 2023-03-30 11:26:20 +08:00
parent 36b8cb828c
commit 7968d1ad46
1 changed files with 24 additions and 34 deletions

View File

@ -134,8 +134,9 @@ func GetReadmeContents(ctx *context.APIContext) {
} }
ref := ctx.Params(":ref") ref := ctx.Params(":ref")
readmePath := "README.md" readmeSortArr := []string{"readme", "readme.en.md", "readme_en.md", "readme.md", "readme.ch.md", "readme_ch.md", "readme.zh.md", "readme_zh.md", "readme.cn.md", "readme_cn.md"}
readmezhPath := "" readmePath := ""
readmePathInArrIndex := 0
filesListInterface, err := files_service.GetContentsOrList(ctx, ctx.Repo.Repository, "", ref) filesListInterface, err := files_service.GetContentsOrList(ctx, ctx.Repo.Repository, "", ref)
if err != nil { if err != nil {
ctx.Error(http.StatusInternalServerError, "GetContentsOrList", err) ctx.Error(http.StatusInternalServerError, "GetContentsOrList", err)
@ -144,25 +145,19 @@ func GetReadmeContents(ctx *context.APIContext) {
filesList, ok := filesListInterface.([]*api.ContentsResponse) filesList, ok := filesListInterface.([]*api.ContentsResponse)
if ok { if ok {
for _, file := range filesList { for _, file := range filesList {
if strings.ToLower(file.Name) == "readme.md" { for i, sortItem := range readmeSortArr {
readmePath = file.Name if strings.ToLower(file.Name) == sortItem {
} if readmePath != "" && readmePathInArrIndex > i {
if strings.ToLower(file.Name) == "readme_zh.md" { continue
readmezhPath = file.Name } else {
} readmePathInArrIndex = i
if strings.ToLower(file.Name) == "readme_cn.md" { readmePath = file.Name
readmezhPath = file.Name }
} }
if strings.ToLower(file.Name) == "readme" {
readmePath = file.Name
} }
} }
} }
if readmezhPath != "" {
readmePath = readmezhPath
}
if fileList, err := files_service.GetContentsOrList(ctx, ctx.Repo.Repository, readmePath, ref); err != nil { if fileList, err := files_service.GetContentsOrList(ctx, ctx.Repo.Repository, readmePath, ref); err != nil {
if git.IsErrNotExist(err) { if git.IsErrNotExist(err) {
ctx.NotFound("GetContentsOrList", err) ctx.NotFound("GetContentsOrList", err)
@ -193,8 +188,9 @@ func GetReadmeContentsByPath(ctx *context.APIContext) {
} }
treePath := ctx.Params("*") treePath := ctx.Params("*")
ref := ctx.Params(":ref") ref := ctx.Params(":ref")
readmePath := "README.md" readmeSortArr := []string{"readme", "readme.en.md", "readme_en.md", "readme.md", "readme.ch.md", "readme_ch.md", "readme.zh.md", "readme_zh.md", "readme.cn.md", "readme_cn.md"}
readmezhPath := "" readmePath := ""
readmePathInArrIndex := 0
filesListInterface, err := files_service.GetContentsOrList(ctx, ctx.Repo.Repository, treePath, ref) filesListInterface, err := files_service.GetContentsOrList(ctx, ctx.Repo.Repository, treePath, ref)
if err != nil { if err != nil {
ctx.Error(http.StatusInternalServerError, "GetContentsOrList", err) ctx.Error(http.StatusInternalServerError, "GetContentsOrList", err)
@ -203,25 +199,19 @@ func GetReadmeContentsByPath(ctx *context.APIContext) {
filesList, ok := filesListInterface.([]*api.ContentsResponse) filesList, ok := filesListInterface.([]*api.ContentsResponse)
if ok { if ok {
for _, file := range filesList { for _, file := range filesList {
if strings.ToLower(file.Name) == "readme.md" { for i, sortItem := range readmeSortArr {
readmePath = file.Name if strings.ToLower(file.Name) == sortItem {
} if readmePath != "" && readmePathInArrIndex > i {
if strings.ToLower(file.Name) == "readme_zh.md" { continue
readmezhPath = file.Name } else {
} readmePathInArrIndex = i
if strings.ToLower(file.Name) == "readme_ch.md" { readmePath = file.Name
readmezhPath = file.Name }
} }
if strings.ToLower(file.Name) == "readme" {
readmePath = file.Name
} }
} }
} }
if readmezhPath != "" {
readmePath = readmezhPath
}
newTreePath := treePath + "/" + readmePath newTreePath := treePath + "/" + readmePath
if fileList, err := files_service.GetContentsOrList(ctx, ctx.Repo.Repository, newTreePath, ref); err != nil { if fileList, err := files_service.GetContentsOrList(ctx, ctx.Repo.Repository, newTreePath, ref); err != nil {
if git.IsErrNotExist(err) { if git.IsErrNotExist(err) {