You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

markdown.go 1.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package misc
  5. import (
  6. api "code.gitea.io/sdk/gitea"
  7. "code.gitea.io/gitea/modules/context"
  8. "code.gitea.io/gitea/modules/markdown"
  9. "code.gitea.io/gitea/modules/setting"
  10. )
  11. // Markdown render markdown document to HTML
  12. // see https://github.com/gogits/go-gogs-client/wiki/Miscellaneous#render-an-arbitrary-markdown-document
  13. func Markdown(ctx *context.APIContext, form api.MarkdownOption) {
  14. if ctx.HasAPIError() {
  15. ctx.Error(422, "", ctx.GetErrMsg())
  16. return
  17. }
  18. if len(form.Text) == 0 {
  19. ctx.Write([]byte(""))
  20. return
  21. }
  22. switch form.Mode {
  23. case "gfm":
  24. md := []byte(form.Text)
  25. context := markdown.URLJoin(setting.AppURL, form.Context)
  26. if form.Wiki {
  27. ctx.Write([]byte(markdown.RenderWiki(md, context, nil)))
  28. } else {
  29. ctx.Write(markdown.Render(md, context, nil))
  30. }
  31. default:
  32. ctx.Write(markdown.RenderRaw([]byte(form.Text), "", false))
  33. }
  34. }
  35. // MarkdownRaw render raw markdown HTML
  36. // see https://github.com/gogits/go-gogs-client/wiki/Miscellaneous#render-a-markdown-document-in-raw-mode
  37. func MarkdownRaw(ctx *context.APIContext) {
  38. body, err := ctx.Req.Body().Bytes()
  39. if err != nil {
  40. ctx.Error(422, "", err)
  41. return
  42. }
  43. ctx.Write(markdown.RenderRaw(body, "", false))
  44. }