Skip to content

Commit 3c2c87a

Browse files
committed
fix
1 parent 7690b3c commit 3c2c87a

File tree

4 files changed

+34
-50
lines changed

4 files changed

+34
-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: 28 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -7,82 +7,66 @@ package common
77
import (
88
"fmt"
99
"net/http"
10+
"path"
1011
"strings"
1112

1213
repo_model "code.gitea.io/gitea/models/repo"
14+
"code.gitea.io/gitea/modules/httplib"
1315
"code.gitea.io/gitea/modules/markup"
1416
"code.gitea.io/gitea/modules/markup/markdown"
1517
"code.gitea.io/gitea/modules/setting"
16-
"code.gitea.io/gitea/modules/util"
1718
"code.gitea.io/gitea/services/context"
18-
19-
"mvdan.cc/xurls/v2"
2019
)
2120

2221
// 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 := ""
22+
func RenderMarkup(ctx *context.Base, repo *context.Repository, mode, text, pathContext, filePath string, wiki bool) {
23+
// pathContext format is /subpath/{user}/{repo}/src/{branch, commit, tag}/{identifier/path}
24+
// for example: "/gitea/owner/repo/src/branch/features/feat-123"
2625

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

3237
switch mode {
3338
case "markdown":
3439
// Raw markdown
3540
if err := markdown.RenderRaw(&markup.RenderContext{
36-
Ctx: ctx,
37-
Links: markup.Links{
38-
AbsolutePrefix: true,
39-
Base: urlPrefix,
40-
},
41+
Ctx: ctx,
42+
Links: links,
4143
}, strings.NewReader(text), ctx.Resp); err != nil {
4244
ctx.Error(http.StatusInternalServerError, err.Error())
4345
}
4446
return
4547
case "comment":
46-
// Comment as markdown
48+
// Issue & comment content
4749
markupType = markdown.MarkupName
4850
case "gfm":
49-
// Github Flavored Markdown as document
51+
// GitHub Flavored Markdown
5052
markupType = markdown.MarkupName
5153
case "file":
52-
// File as document based on file extension
53-
markupType = ""
54+
markupType = "" // render the repo file content by its extension
5455
relativePath = filePath
56+
fields := strings.SplitN(strings.TrimPrefix(pathContext, setting.AppSubURL+"/"), "/", 5)
57+
if len(fields) == 5 && fields[2] == "src" && fields[3] == "branch" {
58+
links = markup.Links{
59+
AbsolutePrefix: true,
60+
Base: fmt.Sprintf("%s%s/%s", httplib.GuessCurrentAppURL(ctx), fields[0], fields[1]), // provides "https://host/subpath/{user}/{repo}"
61+
BranchPath: strings.Join(fields[4:], "/"),
62+
TreePath: path.Dir(filePath),
63+
}
64+
}
5565
default:
5666
ctx.Error(http.StatusUnprocessableEntity, fmt.Sprintf("Unknown mode: %s", mode))
5767
return
5868
}
5969

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-
8670
meta := map[string]string{}
8771
var repoCtx *repo_model.Repository
8872
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)