@@ -10,79 +10,61 @@ import (
10
10
"strings"
11
11
12
12
repo_model "code.gitea.io/gitea/models/repo"
13
+ "code.gitea.io/gitea/modules/httplib"
13
14
"code.gitea.io/gitea/modules/markup"
14
15
"code.gitea.io/gitea/modules/markup/markdown"
15
16
"code.gitea.io/gitea/modules/setting"
16
- "code.gitea.io/gitea/modules/util"
17
17
"code.gitea.io/gitea/services/context"
18
-
19
- "mvdan.cc/xurls/v2"
20
18
)
21
19
22
20
// 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 "
26
24
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
30
34
}
31
35
32
36
switch mode {
33
37
case "markdown" :
34
38
// Raw markdown
35
39
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 ,
41
42
}, strings .NewReader (text ), ctx .Resp ); err != nil {
42
43
ctx .Error (http .StatusInternalServerError , err .Error ())
43
44
}
44
45
return
45
46
case "comment" :
46
- // Comment as markdown
47
+ // Issue & comment content
47
48
markupType = markdown .MarkupName
48
49
case "gfm" :
49
- // Github Flavored Markdown as document
50
+ // GitHub Flavored Markdown
50
51
markupType = markdown .MarkupName
51
52
case "file" :
52
- // File as document based on file extension
53
- markupType = ""
53
+ markupType = "" // render the repo file content by its extension
54
54
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
+ }
55
63
default :
56
64
ctx .Error (http .StatusUnprocessableEntity , fmt .Sprintf ("Unknown mode: %s" , mode ))
57
65
return
58
66
}
59
67
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
-
86
68
meta := map [string ]string {}
87
69
var repoCtx * repo_model.Repository
88
70
if repo != nil && repo .Repository != nil {
0 commit comments