Files
Gogs/routes/api/v1/misc/markdown.go

43 lines
897 B
Go
Raw Normal View History

2014-03-29 21:16:06 +08:00
// Copyright 2014 The Gogs Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package misc
2014-03-29 21:16:06 +08:00
2014-03-29 10:01:52 -04:00
import (
2019-08-08 23:53:43 -07:00
"net/http"
2018-05-27 08:53:48 +08:00
api "github.com/gogs/go-gogs-client"
2018-05-27 08:53:48 +08:00
"github.com/gogs/gogs/pkg/context"
"github.com/gogs/gogs/pkg/markup"
2014-03-29 10:01:52 -04:00
)
2014-03-29 21:16:06 +08:00
2017-06-03 07:26:09 -04:00
func Markdown(c *context.APIContext, form api.MarkdownOption) {
if c.HasApiError() {
2019-08-08 23:53:43 -07:00
c.Error(http.StatusUnprocessableEntity, "", c.GetErrMsg())
2014-05-05 13:08:01 -04:00
return
}
2014-12-10 16:37:54 -05:00
if len(form.Text) == 0 {
2017-06-03 07:26:09 -04:00
c.Write([]byte(""))
2014-12-10 16:37:54 -05:00
return
}
2014-05-05 13:08:01 -04:00
switch form.Mode {
case "gfm":
2017-06-03 07:26:09 -04:00
c.Write(markup.Markdown([]byte(form.Text), form.Context, nil))
2014-05-05 13:08:01 -04:00
default:
2017-06-03 07:26:09 -04:00
c.Write(markup.RawMarkdown([]byte(form.Text), ""))
2014-05-05 13:08:01 -04:00
}
}
2017-06-03 07:26:09 -04:00
func MarkdownRaw(c *context.APIContext) {
body, err := c.Req.Body().Bytes()
2014-05-05 13:08:01 -04:00
if err != nil {
2019-08-08 23:53:43 -07:00
c.Error(http.StatusUnprocessableEntity, "", err)
2014-05-05 13:08:01 -04:00
return
}
2017-06-03 07:26:09 -04:00
c.Write(markup.RawMarkdown(body, ""))
2014-03-29 21:16:06 +08:00
}