新增:获取wiki接口

This commit is contained in:
yystopf 2022-10-31 17:54:43 +08:00
parent 905811e11e
commit 72815148b6
2 changed files with 71 additions and 0 deletions

View File

@ -61,6 +61,11 @@ func Routers() *web.Route {
m.Group("/wikies", func() {
m.Combo("").Get(repo.ListWikiPages).
Post(bind(hat_api.WikiOption{}), repo.CreateWiki)
m.Group("/{page}", func() {
m.Combo("").Get(repo.GetWiki) //.
// Patch(bind(hat_api.WikiOption{}), repo.EditWiki).
// Delete(repo.DeleteWiki)
})
})
}, repoAssignment())
})

View File

@ -238,3 +238,69 @@ func wikiContentsByName(ctx *context.Context, commit *git.Commit, wikiName strin
}
return wikiContentsByEntry(ctx, entry), entry, pageFilename
}
func GetWiki(ctx *context.APIContext) {
wikiRepo, commit, _ := findWikiRepoCommit(ctx.Context)
defer func() {
if wikiRepo != nil {
wikiRepo.Close()
}
}()
wikiCloneWiki := ctx.Repo.Repository.WikiCloneLink()
pageName := wiki_service.NormalizeWikiName(ctx.Params(":page"))
if len(pageName) == 0 {
pageName = "Home"
}
data, entry, pageFileName := wikiContentsByName(ctx.Context, commit, pageName)
if entry == nil {
ctx.NotFound()
return
}
metas := ctx.Repo.Repository.ComposeDocumentMetas()
var rctx = &markup.RenderContext{
URLPrefix: ctx.Repo.RepoLink,
Metas: metas,
IsWiki: true,
}
var buf strings.Builder
if err := markdown.Render(rctx, bytes.NewReader(data), &buf); err != nil {
ctx.ServerError("markdown.Render", err)
return
}
c, err := wikiRepo.GetCommitByPath(entry.Name())
if err != nil {
if models.IsErrWikiInvalidFileName(err) {
return
}
}
commitsCount, _ := wikiRepo.FileCommitsCount("master", pageFileName)
wiki := hat_api.WikiResponse{
WikiCloneLink: hat_api.CloneLink{
HTTPS: wikiCloneWiki.HTTPS,
SSH: wikiCloneWiki.SSH,
},
WikiMeta: hat_api.WikiMeta{
Name: pageName,
Commit: hat_api.WikiCommit{
Author: hat_api.WikiUser{
Name: c.Author.Name,
Email: c.Author.Email,
When: c.Author.When.Unix(),
},
Commiter: hat_api.WikiUser{
Name: c.Committer.Name,
Email: c.Committer.Email,
When: c.Committer.When.Unix(),
},
},
},
CommitCounts: commitsCount,
MdContent: string(data),
SimpleContent: buf.String(),
}
ctx.JSON(http.StatusOK, wiki)
}