2023-03-24 09:12:23 +03: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 10:12:22 +03:00
|
|
|
"code.gitea.io/gitea/services/context"
|
2023-03-24 09:12:23 +03:00
|
|
|
|
|
|
|
"mvdan.cc/xurls/v2"
|
|
|
|
)
|
|
|
|
|
2024-11-23 18:00:18 +03:00
|
|
|
type Renderer struct {
|
|
|
|
Mode, Text, URLPrefix, FilePath, BranchPath string
|
|
|
|
IsWiki bool
|
|
|
|
}
|
|
|
|
|
2023-03-24 09:12:23 +03:00
|
|
|
// RenderMarkup renders markup text for the /markup and /markdown endpoints
|
2024-11-23 18:00:18 +03:00
|
|
|
func (re *Renderer) RenderMarkup(ctx *context.Base, repo *context.Repository) {
|
2023-04-22 21:53:00 +03:00
|
|
|
var markupType string
|
2023-03-24 09:12:23 +03:00
|
|
|
relativePath := ""
|
|
|
|
|
2024-11-23 18:00:18 +03:00
|
|
|
if len(re.Text) == 0 {
|
2023-03-24 09:12:23 +03:00
|
|
|
_, _ = ctx.Write([]byte(""))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-11-23 18:00:18 +03:00
|
|
|
switch re.Mode {
|
2023-03-24 09:12:23 +03:00
|
|
|
case "markdown":
|
|
|
|
// Raw markdown
|
|
|
|
if err := markdown.RenderRaw(&markup.RenderContext{
|
2024-01-15 11:49:24 +03:00
|
|
|
Ctx: ctx,
|
|
|
|
Links: markup.Links{
|
2024-03-13 13:34:58 +03:00
|
|
|
AbsolutePrefix: true,
|
2024-11-23 18:00:18 +03:00
|
|
|
Base: re.URLPrefix,
|
2024-01-15 11:49:24 +03:00
|
|
|
},
|
2024-11-23 18:00:18 +03:00
|
|
|
}, strings.NewReader(re.Text), ctx.Resp); err != nil {
|
2023-03-24 09:12:23 +03: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 18:00:18 +03:00
|
|
|
relativePath = re.FilePath
|
2023-03-24 09:12:23 +03:00
|
|
|
default:
|
2024-11-23 18:00:18 +03:00
|
|
|
ctx.Error(http.StatusUnprocessableEntity, fmt.Sprintf("Unknown mode: %s", re.Mode))
|
2023-03-24 09:12:23 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-11-23 18:00:18 +03:00
|
|
|
if !strings.HasPrefix(setting.AppSubURL+"/", re.URLPrefix) {
|
2023-03-24 09:12:23 +03:00
|
|
|
// check if urlPrefix is already set to a URL
|
|
|
|
linkRegex, _ := xurls.StrictMatchingScheme("https?://")
|
2024-11-23 18:00:18 +03:00
|
|
|
m := linkRegex.FindStringIndex(re.URLPrefix)
|
2023-03-24 09:12:23 +03:00
|
|
|
if m == nil {
|
2024-11-23 18:00:18 +03:00
|
|
|
re.URLPrefix = util.URLJoin(setting.AppURL, re.URLPrefix)
|
2023-03-24 09:12:23 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
meta := map[string]string{}
|
2023-05-21 04:50:53 +03:00
|
|
|
if repo != nil && repo.Repository != nil {
|
2024-11-23 18:00:18 +03:00
|
|
|
if re.Mode == "comment" {
|
2023-10-11 07:24:07 +03:00
|
|
|
meta = repo.Repository.ComposeMetas(ctx)
|
2023-03-24 09:12:23 +03:00
|
|
|
} else {
|
2023-10-11 07:24:07 +03:00
|
|
|
meta = repo.Repository.ComposeDocumentMetas(ctx)
|
2023-03-24 09:12:23 +03:00
|
|
|
}
|
|
|
|
}
|
2024-11-23 18:00:18 +03:00
|
|
|
if re.Mode != "comment" {
|
2023-03-24 09:12:23 +03:00
|
|
|
meta["mode"] = "document"
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := markup.Render(&markup.RenderContext{
|
2024-01-15 11:49:24 +03:00
|
|
|
Ctx: ctx,
|
|
|
|
Links: markup.Links{
|
2024-03-13 13:34:58 +03:00
|
|
|
AbsolutePrefix: true,
|
2024-11-23 18:00:18 +03:00
|
|
|
Base: re.URLPrefix,
|
|
|
|
BranchPath: re.BranchPath,
|
2024-01-15 11:49:24 +03:00
|
|
|
},
|
2023-03-24 09:12:23 +03:00
|
|
|
Metas: meta,
|
2024-11-23 18:00:18 +03:00
|
|
|
IsWiki: re.IsWiki,
|
2023-03-24 09:12:23 +03:00
|
|
|
Type: markupType,
|
|
|
|
RelativePath: relativePath,
|
2024-11-23 18:00:18 +03:00
|
|
|
}, strings.NewReader(re.Text), ctx.Resp); err != nil {
|
2023-03-24 09:12:23 +03:00
|
|
|
if markup.IsErrUnsupportedRenderExtension(err) {
|
|
|
|
ctx.Error(http.StatusUnprocessableEntity, err.Error())
|
|
|
|
} else {
|
|
|
|
ctx.Error(http.StatusInternalServerError, err.Error())
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|