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

     1  // Copyright 2014 The Gogs Authors. All rights reserved.
     2  // Copyright 2023 The Gitea Authors. All rights reserved.
     3  // SPDX-License-Identifier: MIT
     4  
     5  package common
     6  
     7  import (
     8  	"fmt"
     9  	"net/http"
    10  	"strings"
    11  
    12  	"code.gitea.io/gitea/modules/context"
    13  	"code.gitea.io/gitea/modules/markup"
    14  	"code.gitea.io/gitea/modules/markup/markdown"
    15  	"code.gitea.io/gitea/modules/setting"
    16  	"code.gitea.io/gitea/modules/util"
    17  
    18  	"mvdan.cc/xurls/v2"
    19  )
    20  
    21  // RenderMarkup renders markup text for the /markup and /markdown endpoints
    22  func RenderMarkup(ctx *context.Base, repo *context.Repository, mode, text, urlPrefix, filePath string, wiki bool) {
    23  	var markupType string
    24  	relativePath := ""
    25  
    26  	if len(text) == 0 {
    27  		_, _ = ctx.Write([]byte(""))
    28  		return
    29  	}
    30  
    31  	switch mode {
    32  	case "markdown":
    33  		// Raw markdown
    34  		if err := markdown.RenderRaw(&markup.RenderContext{
    35  			Ctx: ctx,
    36  			Links: markup.Links{
    37  				Base: urlPrefix,
    38  			},
    39  		}, strings.NewReader(text), ctx.Resp); err != nil {
    40  			ctx.Error(http.StatusInternalServerError, err.Error())
    41  		}
    42  		return
    43  	case "comment":
    44  		// Comment as markdown
    45  		markupType = markdown.MarkupName
    46  	case "gfm":
    47  		// Github Flavored Markdown as document
    48  		markupType = markdown.MarkupName
    49  	case "file":
    50  		// File as document based on file extension
    51  		markupType = ""
    52  		relativePath = filePath
    53  	default:
    54  		ctx.Error(http.StatusUnprocessableEntity, fmt.Sprintf("Unknown mode: %s", mode))
    55  		return
    56  	}
    57  
    58  	if !strings.HasPrefix(setting.AppSubURL+"/", urlPrefix) {
    59  		// check if urlPrefix is already set to a URL
    60  		linkRegex, _ := xurls.StrictMatchingScheme("https?://")
    61  		m := linkRegex.FindStringIndex(urlPrefix)
    62  		if m == nil {
    63  			urlPrefix = util.URLJoin(setting.AppURL, urlPrefix)
    64  		}
    65  	}
    66  
    67  	meta := map[string]string{}
    68  	if repo != nil && repo.Repository != nil {
    69  		if mode == "comment" {
    70  			meta = repo.Repository.ComposeMetas()
    71  		} else {
    72  			meta = repo.Repository.ComposeDocumentMetas()
    73  		}
    74  	}
    75  	if mode != "comment" {
    76  		meta["mode"] = "document"
    77  	}
    78  
    79  	if err := markup.Render(&markup.RenderContext{
    80  		Ctx: ctx,
    81  		Links: markup.Links{
    82  			Base: urlPrefix,
    83  		},
    84  		Metas:        meta,
    85  		IsWiki:       wiki,
    86  		Type:         markupType,
    87  		RelativePath: relativePath,
    88  	}, strings.NewReader(text), ctx.Resp); err != nil {
    89  		if markup.IsErrUnsupportedRenderExtension(err) {
    90  			ctx.Error(http.StatusUnprocessableEntity, err.Error())
    91  		} else {
    92  			ctx.Error(http.StatusInternalServerError, err.Error())
    93  		}
    94  		return
    95  	}
    96  }