Skip to content

Commit 75d5e92

Browse files
committed
fix
1 parent 7690b3c commit 75d5e92

File tree

4 files changed

+32
-50
lines changed

4 files changed

+32
-50
lines changed

modules/markup/renderer.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ type RenderContext struct {
8686
}
8787

8888
type Links struct {
89-
AbsolutePrefix bool
90-
Base string
89+
AbsolutePrefix bool // add absolute URL prefix to auto-resolved links like "#issue", but not for pre-provided links and medias
90+
Base string // base prefix for pre-provided links and medias (images, videos)
9191
BranchPath string
9292
TreePath string
9393
}

modules/structs/miscellaneous.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ type MarkupOption struct {
2626
// in: body
2727
Mode string
2828
// URL path for rendering issue, media and file links
29-
// Expected format: {user}/{repo}/src/{branch, commit, tag}/{identifier}/{filepath}
29+
// Expected format: /subpath/{user}/{repo}/src/{branch, commit, tag}/{identifier/path}
3030
//
3131
// in: body
3232
Context string
@@ -55,7 +55,7 @@ type MarkdownOption struct {
5555
// in: body
5656
Mode string
5757
// URL path for rendering issue, media and file links
58-
// Expected format: {user}/{repo}/src/{branch, commit, tag}/{identifier}/{filepath}
58+
// Expected format: /subpath/{user}/{repo}/src/{branch, commit, tag}/{identifier/path}
5959
//
6060
// in: body
6161
Context string

routers/common/markup.go

Lines changed: 26 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -10,79 +10,61 @@ import (
1010
"strings"
1111

1212
repo_model "code.gitea.io/gitea/models/repo"
13+
"code.gitea.io/gitea/modules/httplib"
1314
"code.gitea.io/gitea/modules/markup"
1415
"code.gitea.io/gitea/modules/markup/markdown"
1516
"code.gitea.io/gitea/modules/setting"
16-
"code.gitea.io/gitea/modules/util"
1717
"code.gitea.io/gitea/services/context"
18-
19-
"mvdan.cc/xurls/v2"
2018
)
2119

2220
// RenderMarkup renders markup text for the /markup and /markdown endpoints
23-
func RenderMarkup(ctx *context.Base, repo *context.Repository, mode, text, urlPrefix, filePath string, wiki bool) {
24-
var markupType string
25-
relativePath := ""
21+
func RenderMarkup(ctx *context.Base, repo *context.Repository, mode, text, pathContext, filePath string, wiki bool) {
22+
// pathContext format is /subpath/{user}/{repo}/src/{branch, commit, tag}/{identifier/path}
23+
// for example: "/gitea/owner/repo/src/branch/features/feat-123"
2624

27-
if len(text) == 0 {
28-
_, _ = ctx.Write([]byte(""))
29-
return
25+
// filePath is the path of the file to render if the end user is trying to preview a repo file (mode == "file")
26+
// for example, when previewing file ""/gitea/owner/repo/src/branch/features/feat-123/doc/CHANGE.md", then filePath is "doc/CHANGE.md"
27+
// and filePath will be used as RenderContext.RelativePath
28+
29+
markupType := ""
30+
relativePath := ""
31+
links := markup.Links{
32+
AbsolutePrefix: true,
33+
Base: pathContext, // TODO: this is the legacy logic, but it doesn't seem right, "Base" should also use an absolute URL
3034
}
3135

3236
switch mode {
3337
case "markdown":
3438
// Raw markdown
3539
if err := markdown.RenderRaw(&markup.RenderContext{
36-
Ctx: ctx,
37-
Links: markup.Links{
38-
AbsolutePrefix: true,
39-
Base: urlPrefix,
40-
},
40+
Ctx: ctx,
41+
Links: links,
4142
}, strings.NewReader(text), ctx.Resp); err != nil {
4243
ctx.Error(http.StatusInternalServerError, err.Error())
4344
}
4445
return
4546
case "comment":
46-
// Comment as markdown
47+
// Issue & comment content
4748
markupType = markdown.MarkupName
4849
case "gfm":
49-
// Github Flavored Markdown as document
50+
// GitHub Flavored Markdown
5051
markupType = markdown.MarkupName
5152
case "file":
52-
// File as document based on file extension
53-
markupType = ""
53+
markupType = "" // render the repo file content by its extension
5454
relativePath = filePath
55+
fields := strings.SplitN(strings.TrimPrefix(pathContext, setting.AppSubURL+"/"), "/", 5)
56+
if len(fields) == 5 && fields[2] == "src" && fields[3] == "branch" {
57+
links = markup.Links{
58+
AbsolutePrefix: true,
59+
Base: fmt.Sprintf("%s%s/%s", httplib.GuessCurrentAppURL(ctx), fields[0], fields[1]), // provides "https://host/subpath/{user}/{repo}"
60+
BranchPath: strings.Join(fields[4:], "/"),
61+
}
62+
}
5563
default:
5664
ctx.Error(http.StatusUnprocessableEntity, fmt.Sprintf("Unknown mode: %s", mode))
5765
return
5866
}
5967

60-
if !strings.HasPrefix(setting.AppSubURL+"/", urlPrefix) {
61-
// check if urlPrefix is already set to a URL
62-
linkRegex, _ := xurls.StrictMatchingScheme("https?://")
63-
m := linkRegex.FindStringIndex(urlPrefix)
64-
if m == nil {
65-
urlPrefix = util.URLJoin(setting.AppURL, urlPrefix)
66-
}
67-
}
68-
69-
links := markup.Links{
70-
AbsolutePrefix: true,
71-
Base: urlPrefix,
72-
}
73-
74-
// Parse branch path and tree path, for correct media links.
75-
// The expected route is "{user}/{repo}/src/{branch, commit, tag]/{identifier}"
76-
urlElements := strings.Split(strings.TrimPrefix(urlPrefix, setting.AppURL), "/")
77-
if len(urlElements) >= 5 && urlElements[2] == "src" {
78-
links = markup.Links{
79-
AbsolutePrefix: true,
80-
Base: setting.AppURL + urlElements[0] + "/" + urlElements[1],
81-
BranchPath: urlElements[3] + "/" + urlElements[4],
82-
TreePath: strings.Join(urlElements[5:], "/"),
83-
}
84-
}
85-
8668
meta := map[string]string{}
8769
var repoCtx *repo_model.Repository
8870
if repo != nil && repo.Repository != nil {

templates/swagger/v1_json.tmpl

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)