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