code.gitea.io/gitea@v1.21.7/routers/api/v1/misc/markup.go (about)

     1  // Copyright 2014 The Gogs Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package misc
     5  
     6  import (
     7  	"net/http"
     8  
     9  	"code.gitea.io/gitea/modules/context"
    10  	"code.gitea.io/gitea/modules/markup"
    11  	"code.gitea.io/gitea/modules/markup/markdown"
    12  	api "code.gitea.io/gitea/modules/structs"
    13  	"code.gitea.io/gitea/modules/web"
    14  	"code.gitea.io/gitea/routers/common"
    15  )
    16  
    17  // Markup render markup document to HTML
    18  func Markup(ctx *context.APIContext) {
    19  	// swagger:operation POST /markup miscellaneous renderMarkup
    20  	// ---
    21  	// summary: Render a markup document as HTML
    22  	// parameters:
    23  	// - name: body
    24  	//   in: body
    25  	//   schema:
    26  	//     "$ref": "#/definitions/MarkupOption"
    27  	// consumes:
    28  	// - application/json
    29  	// produces:
    30  	//     - text/html
    31  	// responses:
    32  	//   "200":
    33  	//     "$ref": "#/responses/MarkupRender"
    34  	//   "422":
    35  	//     "$ref": "#/responses/validationError"
    36  
    37  	form := web.GetForm(ctx).(*api.MarkupOption)
    38  
    39  	if ctx.HasAPIError() {
    40  		ctx.Error(http.StatusUnprocessableEntity, "", ctx.GetErrMsg())
    41  		return
    42  	}
    43  
    44  	common.RenderMarkup(ctx.Base, ctx.Repo, form.Mode, form.Text, form.Context, form.FilePath, form.Wiki)
    45  }
    46  
    47  // Markdown render markdown document to HTML
    48  func Markdown(ctx *context.APIContext) {
    49  	// swagger:operation POST /markdown miscellaneous renderMarkdown
    50  	// ---
    51  	// summary: Render a markdown document as HTML
    52  	// parameters:
    53  	// - name: body
    54  	//   in: body
    55  	//   schema:
    56  	//     "$ref": "#/definitions/MarkdownOption"
    57  	// consumes:
    58  	// - application/json
    59  	// produces:
    60  	//     - text/html
    61  	// responses:
    62  	//   "200":
    63  	//     "$ref": "#/responses/MarkdownRender"
    64  	//   "422":
    65  	//     "$ref": "#/responses/validationError"
    66  
    67  	form := web.GetForm(ctx).(*api.MarkdownOption)
    68  
    69  	if ctx.HasAPIError() {
    70  		ctx.Error(http.StatusUnprocessableEntity, "", ctx.GetErrMsg())
    71  		return
    72  	}
    73  
    74  	mode := "markdown"
    75  	if form.Mode == "comment" || form.Mode == "gfm" {
    76  		mode = form.Mode
    77  	}
    78  
    79  	common.RenderMarkup(ctx.Base, ctx.Repo, mode, form.Text, form.Context, "", form.Wiki)
    80  }
    81  
    82  // MarkdownRaw render raw markdown HTML
    83  func MarkdownRaw(ctx *context.APIContext) {
    84  	// swagger:operation POST /markdown/raw miscellaneous renderMarkdownRaw
    85  	// ---
    86  	// summary: Render raw markdown as HTML
    87  	// parameters:
    88  	//     - name: body
    89  	//       in: body
    90  	//       description: Request body to render
    91  	//       required: true
    92  	//       schema:
    93  	//         type: string
    94  	// consumes:
    95  	//     - text/plain
    96  	// produces:
    97  	//     - text/html
    98  	// responses:
    99  	//   "200":
   100  	//     "$ref": "#/responses/MarkdownRender"
   101  	//   "422":
   102  	//     "$ref": "#/responses/validationError"
   103  	defer ctx.Req.Body.Close()
   104  	if err := markdown.RenderRaw(&markup.RenderContext{
   105  		Ctx: ctx,
   106  	}, ctx.Req.Body, ctx.Resp); err != nil {
   107  		ctx.InternalServerError(err)
   108  		return
   109  	}
   110  }