mirror of
				https://github.com/go-gitea/gitea.git
				synced 2025-10-29 10:57:44 +09:00 
			
		
		
		
	Support creating relative link to raw path in markdown (#34105)
Fix #34104
This commit is contained in:
		| @@ -343,12 +343,25 @@ func prepareHomeTreeSideBarSwitch(ctx *context.Context) { | |||||||
| 	ctx.Data["UserSettingCodeViewShowFileTree"] = showFileTree | 	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 | // Home render repository home page | ||||||
| func Home(ctx *context.Context) { | func Home(ctx *context.Context) { | ||||||
| 	if handleRepoHomeFeed(ctx) { | 	if handleRepoHomeFeed(ctx) { | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
|  | 	if redirectSrcToRaw(ctx) { | ||||||
|  | 		return | ||||||
|  | 	} | ||||||
| 	// Check whether the repo is viewable: not in migration, and the code unit should be enabled | 	// 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. | 	// Ideally the "feed" logic should be after this, but old code did so, so keep it as-is. | ||||||
| 	checkHomeCodeViewable(ctx) | 	checkHomeCodeViewable(ctx) | ||||||
|   | |||||||
| @@ -54,6 +54,7 @@ func TestRedirectsNoLogin(t *testing.T) { | |||||||
| 		{"/user2/repo1/src/master", "/user2/repo1/src/branch/master"}, | 		{"/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/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/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/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"}, | 		{"/user2/repo1/blob/123456/%20?a=1", "/user2/repo1/src/commit/123456/%20?a=1"}, | ||||||
| 		{"/user/avatar/GhosT/-1", "/assets/img/avatar_default.png"}, | 		{"/user/avatar/GhosT/-1", "/assets/img/avatar_default.png"}, | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user