Skip to content

Commit 285950a

Browse files
authored
Support creating relative link to raw path in markdown (#34105)
Fix #34104
1 parent 55a69ae commit 285950a

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

routers/web/repo/view_home.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,12 +343,25 @@ func prepareHomeTreeSideBarSwitch(ctx *context.Context) {
343343
ctx.Data["UserSettingCodeViewShowFileTree"] = showFileTree
344344
}
345345

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+
346357
// Home render repository home page
347358
func Home(ctx *context.Context) {
348359
if handleRepoHomeFeed(ctx) {
349360
return
350361
}
351-
362+
if redirectSrcToRaw(ctx) {
363+
return
364+
}
352365
// Check whether the repo is viewable: not in migration, and the code unit should be enabled
353366
// Ideally the "feed" logic should be after this, but old code did so, so keep it as-is.
354367
checkHomeCodeViewable(ctx)

tests/integration/links_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ func TestRedirectsNoLogin(t *testing.T) {
5454
{"/user2/repo1/src/master", "/user2/repo1/src/branch/master"},
5555
{"/user2/repo1/src/master/a%2fb.txt", "/user2/repo1/src/branch/master/a%2fb.txt"},
5656
{"/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"},
5758
{"/user2/repo1/tree/a%2fb?a=1", "/user2/repo1/src/a%2fb?a=1"},
5859
{"/user2/repo1/blob/123456/%20?a=1", "/user2/repo1/src/commit/123456/%20?a=1"},
5960
{"/user/avatar/GhosT/-1", "/assets/img/avatar_default.png"},

0 commit comments

Comments
 (0)