2023-03-24 02:12:23 -04:00
|
|
|
// Copyright 2014 The Gogs Authors. All rights reserved.
|
|
|
|
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/markup"
|
|
|
|
"code.gitea.io/gitea/modules/markup/markdown"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
"code.gitea.io/gitea/modules/util"
|
2024-02-27 02:12:22 -05:00
|
|
|
"code.gitea.io/gitea/services/context"
|
2023-03-24 02:12:23 -04:00
|
|
|
|
|
|
|
"mvdan.cc/xurls/v2"
|
|
|
|
)
|
|
|
|
|
2024-11-23 10:00:18 -05:00
|
|
|
type Renderer struct {
|
|
|
|
Mode, Text, URLPrefix, FilePath, BranchPath string
|
|
|
|
IsWiki bool
|
|
|
|
}
|
|
|
|
|
2023-03-24 02:12:23 -04:00
|
|
|
// RenderMarkup renders markup text for the /markup and /markdown endpoints
|
2024-11-23 10:00:18 -05:00
|
|
|
func (re *Renderer) RenderMarkup(ctx *context.Base, repo *context.Repository) {
|
2023-04-22 14:53:00 -04:00
|
|
|
var markupType string
|
2023-03-24 02:12:23 -04:00
|
|
|
relativePath := ""
|
|
|
|
|
2024-11-23 10:00:18 -05:00
|
|
|
if len(re.Text) == 0 {
|
2023-03-24 02:12:23 -04:00
|
|
|
_, _ = ctx.Write([]byte(""))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-11-23 10:00:18 -05:00
|
|
|
switch re.Mode {
|
2023-03-24 02:12:23 -04:00
|
|
|
case "markdown":
|
|
|
|
// Raw markdown
|
|
|
|
if err := markdown.RenderRaw(&markup.RenderContext{
|
2024-01-15 03:49:24 -05:00
|
|
|
Ctx: ctx,
|
|
|
|
Links: markup.Links{
|
2024-03-13 06:34:58 -04:00
|
|
|
AbsolutePrefix: true,
|
2024-11-23 10:00:18 -05:00
|
|
|
Base: re.URLPrefix,
|
2024-01-15 03:49:24 -05:00
|
|
|
},
|
2024-11-23 10:00:18 -05:00
|
|
|
}, strings.NewReader(re.Text), ctx.Resp); err != nil {
|
2023-03-24 02:12:23 -04:00
|
|
|
ctx.Error(http.StatusInternalServerError, err.Error())
|
|
|
|
}
|
|
|
|
return
|
|
|
|
case "comment":
|
|
|
|
// Comment as markdown
|
|
|
|
markupType = markdown.MarkupName
|
|
|
|
case "gfm":
|
|
|
|
// Github Flavored Markdown as document
|
|
|
|
markupType = markdown.MarkupName
|
|
|
|
case "file":
|
|
|
|
// File as document based on file extension
|
|
|
|
markupType = ""
|
2024-11-23 10:00:18 -05:00
|
|
|
relativePath = re.FilePath
|
2023-03-24 02:12:23 -04:00
|
|
|
default:
|
2024-11-23 10:00:18 -05:00
|
|
|
ctx.Error(http.StatusUnprocessableEntity, fmt.Sprintf("Unknown mode: %s", re.Mode))
|
2023-03-24 02:12:23 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-11-23 10:00:18 -05:00
|
|
|
if !strings.HasPrefix(setting.AppSubURL+"/", re.URLPrefix) {
|
2023-03-24 02:12:23 -04:00
|
|
|
// check if urlPrefix is already set to a URL
|
|
|
|
linkRegex, _ := xurls.StrictMatchingScheme("https?://")
|
2024-11-23 10:00:18 -05:00
|
|
|
m := linkRegex.FindStringIndex(re.URLPrefix)
|
2023-03-24 02:12:23 -04:00
|
|
|
if m == nil {
|
2024-11-23 10:00:18 -05:00
|
|
|
re.URLPrefix = util.URLJoin(setting.AppURL, re.URLPrefix)
|
2023-03-24 02:12:23 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
meta := map[string]string{}
|
2023-05-20 21:50:53 -04:00
|
|
|
if repo != nil && repo.Repository != nil {
|
2024-11-23 10:00:18 -05:00
|
|
|
if re.Mode == "comment" {
|
2023-10-11 00:24:07 -04:00
|
|
|
meta = repo.Repository.ComposeMetas(ctx)
|
2023-03-24 02:12:23 -04:00
|
|
|
} else {
|
2023-10-11 00:24:07 -04:00
|
|
|
meta = repo.Repository.ComposeDocumentMetas(ctx)
|
2023-03-24 02:12:23 -04:00
|
|
|
}
|
|
|
|
}
|
2024-11-23 10:00:18 -05:00
|
|
|
if re.Mode != "comment" {
|
2023-03-24 02:12:23 -04:00
|
|
|
meta["mode"] = "document"
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := markup.Render(&markup.RenderContext{
|
2024-01-15 03:49:24 -05:00
|
|
|
Ctx: ctx,
|
|
|
|
Links: markup.Links{
|
2024-03-13 06:34:58 -04:00
|
|
|
AbsolutePrefix: true,
|
2024-11-23 10:00:18 -05:00
|
|
|
Base: re.URLPrefix,
|
|
|
|
BranchPath: re.BranchPath,
|
2024-01-15 03:49:24 -05:00
|
|
|
},
|
2023-03-24 02:12:23 -04:00
|
|
|
Metas: meta,
|
2024-11-23 10:00:18 -05:00
|
|
|
IsWiki: re.IsWiki,
|
2023-03-24 02:12:23 -04:00
|
|
|
Type: markupType,
|
|
|
|
RelativePath: relativePath,
|
2024-11-23 10:00:18 -05:00
|
|
|
}, strings.NewReader(re.Text), ctx.Resp); err != nil {
|
2023-03-24 02:12:23 -04:00
|
|
|
if markup.IsErrUnsupportedRenderExtension(err) {
|
|
|
|
ctx.Error(http.StatusUnprocessableEntity, err.Error())
|
|
|
|
} else {
|
|
|
|
ctx.Error(http.StatusInternalServerError, err.Error())
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|