diff --git a/routers/web/repo/view_home.go b/routers/web/repo/view_home.go index 3561db9101..f69336a014 100644 --- a/routers/web/repo/view_home.go +++ b/routers/web/repo/view_home.go @@ -343,12 +343,25 @@ func prepareHomeTreeSideBarSwitch(ctx *context.Context) { ctx.Data["UserSettingCodeViewShowFileTree"] = showFileTree } +func redirectSrcToRaw(ctx *context.Context) bool { + // GitHub redirects a tree path with "?raw=1" to the raw path + // It is useful to embed some raw contents into markdown files, + // then viewing the markdown in "src" path could embed the raw content correctly. + if ctx.Repo.TreePath != "" && ctx.FormBool("raw") { + ctx.Redirect(ctx.Repo.RepoLink + "/raw/" + ctx.Repo.RefTypeNameSubURL() + "/" + util.PathEscapeSegments(ctx.Repo.TreePath)) + return true + } + return false +} + // Home render repository home page func Home(ctx *context.Context) { if handleRepoHomeFeed(ctx) { return } - + if redirectSrcToRaw(ctx) { + return + } // Check whether the repo is viewable: not in migration, and the code unit should be enabled // Ideally the "feed" logic should be after this, but old code did so, so keep it as-is. checkHomeCodeViewable(ctx) diff --git a/tests/integration/links_test.go b/tests/integration/links_test.go index 5197601bbf..ec59e147d2 100644 --- a/tests/integration/links_test.go +++ b/tests/integration/links_test.go @@ -54,6 +54,7 @@ func TestRedirectsNoLogin(t *testing.T) { {"/user2/repo1/src/master", "/user2/repo1/src/branch/master"}, {"/user2/repo1/src/master/a%2fb.txt", "/user2/repo1/src/branch/master/a%2fb.txt"}, {"/user2/repo1/src/master/directory/file.txt?a=1", "/user2/repo1/src/branch/master/directory/file.txt?a=1"}, + {"/user2/repo1/src/branch/master/directory/file.txt?raw=1&other=2", "/user2/repo1/raw/branch/master/directory/file.txt"}, {"/user2/repo1/tree/a%2fb?a=1", "/user2/repo1/src/a%2fb?a=1"}, {"/user2/repo1/blob/123456/%20?a=1", "/user2/repo1/src/commit/123456/%20?a=1"}, {"/user/avatar/GhosT/-1", "/assets/img/avatar_default.png"},