Skip to content

Commit 9dca6d4

Browse files
brechtvlwxiaoguang
authored andcommitted
Fix missing images in editor preview due to wrong links
Parse base path and tree path so that media links can be correctly created with /media/. Resolves go-gitea#31294
1 parent f446e3b commit 9dca6d4

File tree

2 files changed

+72
-30
lines changed

2 files changed

+72
-30
lines changed

routers/api/v1/misc/markup_test.go

Lines changed: 53 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,21 @@ import (
2222
const (
2323
AppURL = "http://localhost:3000/"
2424
Repo = "gogits/gogs"
25+
Branch = "main"
2526
FullURL = AppURL + Repo + "/"
2627
)
2728

28-
func testRenderMarkup(t *testing.T, mode, filePath, text, responseBody string, responseCode int) {
29+
func testRenderMarkup(t *testing.T, mode string, wiki bool, filePath, text, responseBody string, responseCode int) {
2930
setting.AppURL = AppURL
31+
context := Repo
32+
if !wiki {
33+
context += "/src/branch/" + Branch
34+
}
3035
options := api.MarkupOption{
3136
Mode: mode,
3237
Text: text,
33-
Context: Repo,
34-
Wiki: true,
38+
Context: context,
39+
Wiki: wiki,
3540
FilePath: filePath,
3641
}
3742
ctx, resp := contexttest.MockAPIContext(t, "POST /api/v1/markup")
@@ -42,13 +47,17 @@ func testRenderMarkup(t *testing.T, mode, filePath, text, responseBody string, r
4247
resp.Body.Reset()
4348
}
4449

45-
func testRenderMarkdown(t *testing.T, mode, text, responseBody string, responseCode int) {
50+
func testRenderMarkdown(t *testing.T, mode string, wiki bool, text, responseBody string, responseCode int) {
4651
setting.AppURL = AppURL
52+
context := Repo
53+
if !wiki {
54+
context += "/src/branch/" + Branch
55+
}
4756
options := api.MarkdownOption{
4857
Mode: mode,
4958
Text: text,
50-
Context: Repo,
51-
Wiki: true,
59+
Context: context,
60+
Wiki: wiki,
5261
}
5362
ctx, resp := contexttest.MockAPIContext(t, "POST /api/v1/markdown")
5463
web.SetForm(ctx, &options)
@@ -65,7 +74,7 @@ func TestAPI_RenderGFM(t *testing.T) {
6574
},
6675
})
6776

68-
testCasesCommon := []string{
77+
testCasesWiki := []string{
6978
// dear imgui wiki markdown extract: special wiki syntax
7079
`Wiki! Enjoy :)
7180
- [[Links, Language bindings, Engine bindings|Links]]
@@ -95,7 +104,7 @@ func TestAPI_RenderGFM(t *testing.T) {
95104
``,
96105
}
97106

98-
testCasesDocument := []string{
107+
testCasesWikiDocument := []string{
99108
// wine-staging wiki home extract: special wiki syntax, images
100109
`## What is Wine Staging?
101110
**Wine Staging** on website [wine-staging.com](http://wine-staging.com).
@@ -116,26 +125,46 @@ Here are some links to the most important topics. You can find the full list of
116125
`,
117126
}
118127

119-
for i := 0; i < len(testCasesCommon); i += 2 {
120-
text := testCasesCommon[i]
121-
response := testCasesCommon[i+1]
122-
testRenderMarkdown(t, "gfm", text, response, http.StatusOK)
123-
testRenderMarkup(t, "gfm", "", text, response, http.StatusOK)
124-
testRenderMarkdown(t, "comment", text, response, http.StatusOK)
125-
testRenderMarkup(t, "comment", "", text, response, http.StatusOK)
126-
testRenderMarkup(t, "file", "path/test.md", text, response, http.StatusOK)
128+
testCasesRepoBranch := []string{
129+
// links to other files in a branch, no wiki syntax
130+
`# Title
131+
[Link](test.md)
132+
![Image](image.png)`,
133+
// rendered
134+
`<h1 id="user-content-title">Title</h1>
135+
<p><a href="` + FullURL + `src/branch/` + Branch + `/test.md" rel="nofollow">Link</a>
136+
<a href="` + FullURL + `media/branch/` + Branch + `/image.png" target="_blank" rel="nofollow noopener"><img src="` + FullURL + `media/branch/` + Branch + `/image.png" alt="Image"/></a></p>
137+
`,
138+
}
139+
140+
for i := 0; i < len(testCasesWiki); i += 2 {
141+
text := testCasesWiki[i]
142+
response := testCasesWiki[i+1]
143+
testRenderMarkdown(t, "gfm", true, text, response, http.StatusOK)
144+
testRenderMarkup(t, "gfm", true, "", text, response, http.StatusOK)
145+
testRenderMarkdown(t, "comment", true, text, response, http.StatusOK)
146+
testRenderMarkup(t, "comment", true, "", text, response, http.StatusOK)
147+
testRenderMarkup(t, "file", true, "path/test.md", text, response, http.StatusOK)
148+
}
149+
150+
for i := 0; i < len(testCasesWikiDocument); i += 2 {
151+
text := testCasesWikiDocument[i]
152+
response := testCasesWikiDocument[i+1]
153+
testRenderMarkdown(t, "gfm", true, text, response, http.StatusOK)
154+
testRenderMarkup(t, "gfm", true, "", text, response, http.StatusOK)
155+
testRenderMarkup(t, "file", true, "path/test.md", text, response, http.StatusOK)
127156
}
128157

129-
for i := 0; i < len(testCasesDocument); i += 2 {
130-
text := testCasesDocument[i]
131-
response := testCasesDocument[i+1]
132-
testRenderMarkdown(t, "gfm", text, response, http.StatusOK)
133-
testRenderMarkup(t, "gfm", "", text, response, http.StatusOK)
134-
testRenderMarkup(t, "file", "path/test.md", text, response, http.StatusOK)
158+
for i := 0; i < len(testCasesRepoBranch); i += 2 {
159+
text := testCasesRepoBranch[i]
160+
response := testCasesRepoBranch[i+1]
161+
testRenderMarkdown(t, "gfm", false, text, response, http.StatusOK)
162+
testRenderMarkup(t, "gfm", false, "", text, response, http.StatusOK)
163+
testRenderMarkup(t, "file", false, "path/test.md", text, response, http.StatusOK)
135164
}
136165

137-
testRenderMarkup(t, "file", "path/test.unknown", "## Test", "Unsupported render extension: .unknown\n", http.StatusUnprocessableEntity)
138-
testRenderMarkup(t, "unknown", "", "## Test", "Unknown mode: unknown\n", http.StatusUnprocessableEntity)
166+
testRenderMarkup(t, "file", true, "path/test.unknown", "## Test", "Unsupported render extension: .unknown\n", http.StatusUnprocessableEntity)
167+
testRenderMarkup(t, "unknown", true, "", "## Test", "Unknown mode: unknown\n", http.StatusUnprocessableEntity)
139168
}
140169

141170
var simpleCases = []string{

routers/common/markup.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,22 @@ func RenderMarkup(ctx *context.Base, repo *context.Repository, mode, text, urlPr
6666
}
6767
}
6868

69+
links := markup.Links{
70+
AbsolutePrefix: true,
71+
Base: urlPrefix,
72+
}
73+
74+
// Parse branch path and tree path, for correct media links.
75+
urlElements := strings.Split(strings.TrimPrefix(urlPrefix, setting.AppURL), "/")
76+
if len(urlElements) >= 5 && urlElements[2] == "src" {
77+
links = markup.Links{
78+
AbsolutePrefix: true,
79+
Base: setting.AppURL + urlElements[0] + "/" + urlElements[1],
80+
BranchPath: urlElements[3] + "/" + urlElements[4],
81+
TreePath: strings.Join(urlElements[5:], "/"),
82+
}
83+
}
84+
6985
meta := map[string]string{}
7086
var repoCtx *repo_model.Repository
7187
if repo != nil && repo.Repository != nil {
@@ -81,12 +97,9 @@ func RenderMarkup(ctx *context.Base, repo *context.Repository, mode, text, urlPr
8197
}
8298

8399
if err := markup.Render(&markup.RenderContext{
84-
Ctx: ctx,
85-
Repo: repoCtx,
86-
Links: markup.Links{
87-
AbsolutePrefix: true,
88-
Base: urlPrefix,
89-
},
100+
Ctx: ctx,
101+
Repo: repoCtx,
102+
Links: links,
90103
Metas: meta,
91104
IsWiki: wiki,
92105
Type: markupType,

0 commit comments

Comments
 (0)