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

43 lines
1.0 KiB
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 (
api "github.com/gogits/go-gogs-client"
"github.com/gogits/gogs/pkg/context"
"github.com/gogits/gogs/pkg/markup"
2014-03-29 10:01:52 -04:00
)
2014-03-29 21:16:06 +08:00
2015-12-03 00:24:37 -05:00
// https://github.com/gogits/go-gogs-client/wiki/Miscellaneous#render-an-arbitrary-markdown-document
2017-06-03 07:26:09 -04:00
func Markdown(c *context.APIContext, form api.MarkdownOption) {
if c.HasApiError() {
c.Error(422, "", 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
}
}
2015-12-03 00:24:37 -05:00
// https://github.com/gogits/go-gogs-client/wiki/Miscellaneous#render-a-markdown-document-in-raw-mode
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 {
2017-06-03 07:26:09 -04:00
c.Error(422, "", 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
}