aboutsummaryrefslogtreecommitdiffstats
path: root/routers/common
diff options
context:
space:
mode:
authorBrecht Van Lommel <brecht@blender.org>2024-06-17 08:16:14 +0200
committerGitHub <noreply@github.com>2024-06-17 14:16:14 +0800
commit597d1da96b92b181c106813ce26149334b2b44e5 (patch)
tree849dd929ac92427759deb57cd14e067cd517dba2 /routers/common
parentf5dfd7d73cbc606ae65e5bab33efad1604b6331e (diff)
downloadgitea-597d1da96b92b181c106813ce26149334b2b44e5.tar.gz
gitea-597d1da96b92b181c106813ce26149334b2b44e5.zip
Fix missing images in editor preview due to wrong links (#31299)
Parse base path and tree path so that media links can be correctly created with /media/. Resolves #31294 --------- Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
Diffstat (limited to 'routers/common')
-rw-r--r--routers/common/markup.go63
1 files changed, 32 insertions, 31 deletions
diff --git a/routers/common/markup.go b/routers/common/markup.go
index f7d096008a..0a00eac7d4 100644
--- a/routers/common/markup.go
+++ b/routers/common/markup.go
@@ -7,63 +7,67 @@ package common
import (
"fmt"
"net/http"
+ "path"
"strings"
repo_model "code.gitea.io/gitea/models/repo"
+ "code.gitea.io/gitea/modules/httplib"
"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"
"code.gitea.io/gitea/services/context"
-
- "mvdan.cc/xurls/v2"
)
// RenderMarkup renders markup text for the /markup and /markdown endpoints
-func RenderMarkup(ctx *context.Base, repo *context.Repository, mode, text, urlPrefix, filePath string, wiki bool) {
- var markupType string
- relativePath := ""
+func RenderMarkup(ctx *context.Base, repo *context.Repository, mode, text, urlPathContext, filePath string, wiki bool) {
+ // urlPathContext format is "/subpath/{user}/{repo}/src/{branch, commit, tag}/{identifier/path}/{file/dir}"
+ // filePath is the path of the file to render if the end user is trying to preview a repo file (mode == "file")
+ // filePath will be used as RenderContext.RelativePath
- if len(text) == 0 {
- _, _ = ctx.Write([]byte(""))
- return
+ // for example, when previewing file "/gitea/owner/repo/src/branch/features/feat-123/doc/CHANGE.md", then filePath is "doc/CHANGE.md"
+ // and the urlPathContext is "/gitea/owner/repo/src/branch/features/feat-123/doc"
+
+ var markupType, relativePath string
+
+ links := markup.Links{AbsolutePrefix: true}
+ if urlPathContext != "" {
+ links.Base = fmt.Sprintf("%s%s", httplib.GuessCurrentHostURL(ctx), urlPathContext)
}
switch mode {
case "markdown":
// Raw markdown
if err := markdown.RenderRaw(&markup.RenderContext{
- Ctx: ctx,
- Links: markup.Links{
- AbsolutePrefix: true,
- Base: urlPrefix,
- },
+ Ctx: ctx,
+ Links: links,
}, strings.NewReader(text), ctx.Resp); err != nil {
ctx.Error(http.StatusInternalServerError, err.Error())
}
return
case "comment":
- // Comment as markdown
+ // Issue & comment content
markupType = markdown.MarkupName
case "gfm":
- // Github Flavored Markdown as document
+ // GitHub Flavored Markdown
markupType = markdown.MarkupName
case "file":
- // File as document based on file extension
- markupType = ""
+ markupType = "" // render the repo file content by its extension
relativePath = filePath
default:
ctx.Error(http.StatusUnprocessableEntity, fmt.Sprintf("Unknown mode: %s", mode))
return
}
- if !strings.HasPrefix(setting.AppSubURL+"/", urlPrefix) {
- // check if urlPrefix is already set to a URL
- linkRegex, _ := xurls.StrictMatchingScheme("https?://")
- m := linkRegex.FindStringIndex(urlPrefix)
- if m == nil {
- urlPrefix = util.URLJoin(setting.AppURL, urlPrefix)
- }
+ fields := strings.SplitN(strings.TrimPrefix(urlPathContext, setting.AppSubURL+"/"), "/", 5)
+ if len(fields) == 5 && fields[2] == "src" && (fields[3] == "branch" || fields[3] == "commit" || fields[3] == "tag") {
+ // absolute base prefix is something like "https://host/subpath/{user}/{repo}"
+ absoluteBasePrefix := fmt.Sprintf("%s%s/%s", httplib.GuessCurrentAppURL(ctx), fields[0], fields[1])
+
+ fileDir := path.Dir(filePath) // it is "doc" if filePath is "doc/CHANGE.md"
+ refPath := strings.Join(fields[3:], "/") // it is "branch/features/feat-12/doc"
+ refPath = strings.TrimSuffix(refPath, "/"+fileDir) // now we get the correct branch path: "branch/features/feat-12"
+
+ links = markup.Links{AbsolutePrefix: true, Base: absoluteBasePrefix, BranchPath: refPath, TreePath: fileDir}
}
meta := map[string]string{}
@@ -81,12 +85,9 @@ func RenderMarkup(ctx *context.Base, repo *context.Repository, mode, text, urlPr
}
if err := markup.Render(&markup.RenderContext{
- Ctx: ctx,
- Repo: repoCtx,
- Links: markup.Links{
- AbsolutePrefix: true,
- Base: urlPrefix,
- },
+ Ctx: ctx,
+ Repo: repoCtx,
+ Links: links,
Metas: meta,
IsWiki: wiki,
Type: markupType,