From c1eb4d894a092aed1b87ddf5f80ee824fd56789d Mon Sep 17 00:00:00 2001 From: Unknown Date: Mon, 5 May 2014 13:08:01 -0400 Subject: Clean api code --- routers/api/v1/miscellaneous.go | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) (limited to 'routers/api') diff --git a/routers/api/v1/miscellaneous.go b/routers/api/v1/miscellaneous.go index babdfce9b2..30751efcca 100644 --- a/routers/api/v1/miscellaneous.go +++ b/routers/api/v1/miscellaneous.go @@ -5,14 +5,38 @@ package v1 import ( + "io/ioutil" + "strings" + + "github.com/gogits/gogs/modules/auth/apiv1" "github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/middleware" ) -func Markdown(ctx *middleware.Context) { - content := ctx.Query("content") - ctx.Render.JSON(200, map[string]interface{}{ - "ok": true, - "content": string(base.RenderMarkdown([]byte(content), ctx.Query("repoLink"))), - }) +const DOC_URL = "http://gogs.io/docs" + +// Render an arbitrary Markdown document. +func Markdown(ctx *middleware.Context, form apiv1.MarkdownForm) { + if ctx.HasApiError() { + ctx.JSON(422, base.ApiJsonErr{ctx.GetErrMsg(), DOC_URL}) + return + } + + switch form.Mode { + case "gfm": + ctx.Write(base.RenderMarkdown([]byte(form.Text), + base.AppUrl+strings.TrimPrefix(form.Context, "/"))) + default: + ctx.Write(base.RenderRawMarkdown([]byte(form.Text), "")) + } +} + +// Render a Markdown document in raw mode. +func MarkdownRaw(ctx *middleware.Context) { + body, err := ioutil.ReadAll(ctx.Req.Body) + if err != nil { + ctx.JSON(422, base.ApiJsonErr{err.Error(), DOC_URL}) + return + } + ctx.Write(base.RenderRawMarkdown(body, "")) } -- cgit v1.2.3