aboutsummaryrefslogtreecommitdiffstats
path: root/routers/api/v1/misc
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2021-04-20 06:25:08 +0800
committerGitHub <noreply@github.com>2021-04-19 18:25:08 -0400
commit9d99f6ab19ac3f97af3ca126720e9075c127a652 (patch)
treeb817b4582a871f83b91ad7977fe772fc3501c1e8 /routers/api/v1/misc
parentc9cc6698d2172625854cd063301e63602204a2a1 (diff)
downloadgitea-9d99f6ab19ac3f97af3ca126720e9075c127a652.tar.gz
gitea-9d99f6ab19ac3f97af3ca126720e9075c127a652.zip
Refactor renders (#15175)
* Refactor renders * Some performance optimization * Fix comment * Transform reader * Fix csv test * Fix test * Fix tests * Improve optimaziation * Fix test * Fix test * Detect file encoding with reader * Improve optimaziation * reduce memory usage * improve code * fix build * Fix test * Fix for go1.15 * Fix render * Fix comment * Fix lint * Fix test * Don't use NormalEOF when unnecessary * revert change on util.go * Apply suggestions from code review Co-authored-by: zeripath <art27@cantab.net> * rename function * Take NormalEOF back Co-authored-by: zeripath <art27@cantab.net>
Diffstat (limited to 'routers/api/v1/misc')
-rw-r--r--routers/api/v1/misc/markdown.go38
1 files changed, 14 insertions, 24 deletions
diff --git a/routers/api/v1/misc/markdown.go b/routers/api/v1/misc/markdown.go
index 5718185309..f1007b7ee2 100644
--- a/routers/api/v1/misc/markdown.go
+++ b/routers/api/v1/misc/markdown.go
@@ -5,11 +5,11 @@
package misc
import (
- "io/ioutil"
"net/http"
"strings"
"code.gitea.io/gitea/modules/context"
+ "code.gitea.io/gitea/modules/markup"
"code.gitea.io/gitea/modules/markup/markdown"
"code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs"
@@ -55,7 +55,6 @@ func Markdown(ctx *context.APIContext) {
case "comment":
fallthrough
case "gfm":
- md := []byte(form.Text)
urlPrefix := form.Context
meta := map[string]string{}
if !strings.HasPrefix(setting.AppSubURL+"/", urlPrefix) {
@@ -77,22 +76,19 @@ func Markdown(ctx *context.APIContext) {
if form.Mode == "gfm" {
meta["mode"] = "document"
}
- if form.Wiki {
- _, err := ctx.Write([]byte(markdown.RenderWiki(md, urlPrefix, meta)))
- if err != nil {
- ctx.InternalServerError(err)
- return
- }
- } else {
- _, err := ctx.Write(markdown.Render(md, urlPrefix, meta))
- if err != nil {
- ctx.InternalServerError(err)
- return
- }
+
+ if err := markdown.Render(&markup.RenderContext{
+ URLPrefix: urlPrefix,
+ Metas: meta,
+ IsWiki: form.Wiki,
+ }, strings.NewReader(form.Text), ctx.Resp); err != nil {
+ ctx.InternalServerError(err)
+ return
}
default:
- _, err := ctx.Write(markdown.RenderRaw([]byte(form.Text), "", false))
- if err != nil {
+ if err := markdown.RenderRaw(&markup.RenderContext{
+ URLPrefix: form.Context,
+ }, strings.NewReader(form.Text), ctx.Resp); err != nil {
ctx.InternalServerError(err)
return
}
@@ -120,14 +116,8 @@ func MarkdownRaw(ctx *context.APIContext) {
// "$ref": "#/responses/MarkdownRender"
// "422":
// "$ref": "#/responses/validationError"
-
- body, err := ioutil.ReadAll(ctx.Req.Body)
- if err != nil {
- ctx.Error(http.StatusUnprocessableEntity, "", err)
- return
- }
- _, err = ctx.Write(markdown.RenderRaw(body, "", false))
- if err != nil {
+ defer ctx.Req.Body.Close()
+ if err := markdown.RenderRaw(&markup.RenderContext{}, ctx.Req.Body, ctx.Resp); err != nil {
ctx.InternalServerError(err)
return
}