@@ -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 {
0 commit comments