File tree 2 files changed +15
-1
lines changed 2 files changed +15
-1
lines changed Original file line number Diff line number Diff line change @@ -343,12 +343,25 @@ func prepareHomeTreeSideBarSwitch(ctx *context.Context) {
343
343
ctx .Data ["UserSettingCodeViewShowFileTree" ] = showFileTree
344
344
}
345
345
346
+ func redirectSrcToRaw (ctx * context.Context ) bool {
347
+ // GitHub redirects a tree path with "?raw=1" to the raw path
348
+ // It is useful to embed some raw contents into markdown files,
349
+ // then viewing the markdown in "src" path could embed the raw content correctly.
350
+ if ctx .Repo .TreePath != "" && ctx .FormBool ("raw" ) {
351
+ ctx .Redirect (ctx .Repo .RepoLink + "/raw/" + ctx .Repo .RefTypeNameSubURL () + "/" + util .PathEscapeSegments (ctx .Repo .TreePath ))
352
+ return true
353
+ }
354
+ return false
355
+ }
356
+
346
357
// Home render repository home page
347
358
func Home (ctx * context.Context ) {
348
359
if handleRepoHomeFeed (ctx ) {
349
360
return
350
361
}
351
-
362
+ if redirectSrcToRaw (ctx ) {
363
+ return
364
+ }
352
365
// Check whether the repo is viewable: not in migration, and the code unit should be enabled
353
366
// Ideally the "feed" logic should be after this, but old code did so, so keep it as-is.
354
367
checkHomeCodeViewable (ctx )
Original file line number Diff line number Diff line change @@ -54,6 +54,7 @@ func TestRedirectsNoLogin(t *testing.T) {
54
54
{"/user2/repo1/src/master" , "/user2/repo1/src/branch/master" },
55
55
{"/user2/repo1/src/master/a%2fb.txt" , "/user2/repo1/src/branch/master/a%2fb.txt" },
56
56
{"/user2/repo1/src/master/directory/file.txt?a=1" , "/user2/repo1/src/branch/master/directory/file.txt?a=1" },
57
+ {"/user2/repo1/src/branch/master/directory/file.txt?raw=1&other=2" , "/user2/repo1/raw/branch/master/directory/file.txt" },
57
58
{"/user2/repo1/tree/a%2fb?a=1" , "/user2/repo1/src/a%2fb?a=1" },
58
59
{"/user2/repo1/blob/123456/%20?a=1" , "/user2/repo1/src/commit/123456/%20?a=1" },
59
60
{"/user/avatar/GhosT/-1" , "/assets/img/avatar_default.png" },
You can’t perform that action at this time.
0 commit comments