2020-04-05 08:20:50 +02:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-27 19:20:29 +01:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-04-05 08:20:50 +02:00
|
|
|
|
2017-02-14 02:13:59 +01:00
|
|
|
package misc
|
|
|
|
|
|
|
|
import (
|
2022-10-22 19:15:52 +02:00
|
|
|
go_context "context"
|
2021-09-22 07:38:34 +02:00
|
|
|
"io"
|
2017-02-14 02:13:59 +01:00
|
|
|
"net/http"
|
2024-06-17 08:16:14 +02:00
|
|
|
"path"
|
2017-02-14 02:13:59 +01:00
|
|
|
"strings"
|
2017-09-16 19:17:57 +02:00
|
|
|
"testing"
|
2017-02-14 02:13:59 +01:00
|
|
|
|
2022-10-22 19:15:52 +02:00
|
|
|
"code.gitea.io/gitea/modules/markup"
|
2017-02-14 02:13:59 +01:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2019-05-11 12:21:34 +02:00
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2021-01-26 16:36:53 +01:00
|
|
|
"code.gitea.io/gitea/modules/web"
|
2024-02-27 08:12:22 +01:00
|
|
|
"code.gitea.io/gitea/services/contexttest"
|
2017-09-16 19:17:57 +02:00
|
|
|
|
2017-02-14 02:13:59 +01:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2024-06-17 08:16:14 +02:00
|
|
|
const AppURL = "http://localhost:3000/"
|
2017-02-14 02:13:59 +01:00
|
|
|
|
2024-06-17 08:16:14 +02:00
|
|
|
func testRenderMarkup(t *testing.T, mode string, wiki bool, filePath, text, expectedBody string, expectedCode int) {
|
2023-03-24 07:12:23 +01:00
|
|
|
setting.AppURL = AppURL
|
2024-06-17 08:16:14 +02:00
|
|
|
context := "/gogits/gogs"
|
|
|
|
if !wiki {
|
|
|
|
context += path.Join("/src/branch/main", path.Dir(filePath))
|
|
|
|
}
|
2023-03-24 07:12:23 +01:00
|
|
|
options := api.MarkupOption{
|
|
|
|
Mode: mode,
|
2023-06-18 09:59:09 +02:00
|
|
|
Text: text,
|
2024-06-17 08:16:14 +02:00
|
|
|
Context: context,
|
|
|
|
Wiki: wiki,
|
2023-03-24 07:12:23 +01:00
|
|
|
FilePath: filePath,
|
|
|
|
}
|
2023-09-01 13:26:07 +02:00
|
|
|
ctx, resp := contexttest.MockAPIContext(t, "POST /api/v1/markup")
|
2023-03-24 07:12:23 +01:00
|
|
|
web.SetForm(ctx, &options)
|
|
|
|
Markup(ctx)
|
2024-11-07 04:57:07 +01:00
|
|
|
actual := strings.ReplaceAll(resp.Body.String(), ` data-markdown-generated-content=""`, "")
|
|
|
|
assert.Equal(t, expectedBody, actual)
|
2024-06-17 08:16:14 +02:00
|
|
|
assert.Equal(t, expectedCode, resp.Code)
|
2023-03-24 07:12:23 +01:00
|
|
|
resp.Body.Reset()
|
|
|
|
}
|
|
|
|
|
2024-06-17 08:16:14 +02:00
|
|
|
func testRenderMarkdown(t *testing.T, mode string, wiki bool, text, responseBody string, responseCode int) {
|
2017-02-14 02:13:59 +01:00
|
|
|
setting.AppURL = AppURL
|
2024-06-17 08:16:14 +02:00
|
|
|
context := "/gogits/gogs"
|
|
|
|
if !wiki {
|
|
|
|
context += "/src/branch/main"
|
|
|
|
}
|
2017-02-14 02:13:59 +01:00
|
|
|
options := api.MarkdownOption{
|
2023-03-24 07:12:23 +01:00
|
|
|
Mode: mode,
|
2023-06-18 09:59:09 +02:00
|
|
|
Text: text,
|
2024-06-17 08:16:14 +02:00
|
|
|
Context: context,
|
|
|
|
Wiki: wiki,
|
2017-02-14 02:13:59 +01:00
|
|
|
}
|
2023-09-01 13:26:07 +02:00
|
|
|
ctx, resp := contexttest.MockAPIContext(t, "POST /api/v1/markdown")
|
2023-03-24 07:12:23 +01:00
|
|
|
web.SetForm(ctx, &options)
|
|
|
|
Markdown(ctx)
|
2024-11-07 04:57:07 +01:00
|
|
|
actual := strings.ReplaceAll(resp.Body.String(), ` data-markdown-generated-content=""`, "")
|
|
|
|
assert.Equal(t, responseBody, actual)
|
2023-03-24 07:12:23 +01:00
|
|
|
assert.Equal(t, responseCode, resp.Code)
|
|
|
|
resp.Body.Reset()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAPI_RenderGFM(t *testing.T) {
|
|
|
|
markup.Init(&markup.ProcessorHelper{
|
|
|
|
IsUsernameMentionable: func(ctx go_context.Context, username string) bool {
|
|
|
|
return username == "r-lyeh"
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2024-06-17 08:16:14 +02:00
|
|
|
testCasesWiki := []string{
|
2017-02-14 02:13:59 +01:00
|
|
|
// dear imgui wiki markdown extract: special wiki syntax
|
|
|
|
`Wiki! Enjoy :)
|
|
|
|
- [[Links, Language bindings, Engine bindings|Links]]
|
|
|
|
- [[Tips]]
|
|
|
|
- Bezier widget (by @r-lyeh) https://github.com/ocornut/imgui/issues/786`,
|
|
|
|
// rendered
|
|
|
|
`<p>Wiki! Enjoy :)</p>
|
|
|
|
<ul>
|
2024-06-17 08:16:14 +02:00
|
|
|
<li><a href="http://localhost:3000/gogits/gogs/wiki/Links" rel="nofollow">Links, Language bindings, Engine bindings</a></li>
|
|
|
|
<li><a href="http://localhost:3000/gogits/gogs/wiki/Tips" rel="nofollow">Tips</a></li>
|
|
|
|
<li>Bezier widget (by <a href="http://localhost:3000/r-lyeh" rel="nofollow">@r-lyeh</a>) <a href="https://github.com/ocornut/imgui/issues/786" rel="nofollow">https://github.com/ocornut/imgui/issues/786</a></li>
|
2017-02-14 02:13:59 +01:00
|
|
|
</ul>
|
|
|
|
`,
|
2023-03-24 07:12:23 +01:00
|
|
|
// Guard wiki sidebar: special syntax
|
|
|
|
`[[Guardfile-DSL / Configuring-Guard|Guardfile-DSL---Configuring-Guard]]`,
|
|
|
|
// rendered
|
2024-06-17 08:16:14 +02:00
|
|
|
`<p><a href="http://localhost:3000/gogits/gogs/wiki/Guardfile-DSL---Configuring-Guard" rel="nofollow">Guardfile-DSL / Configuring-Guard</a></p>
|
2023-03-24 07:12:23 +01:00
|
|
|
`,
|
|
|
|
// special syntax
|
|
|
|
`[[Name|Link]]`,
|
|
|
|
// rendered
|
2024-06-17 08:16:14 +02:00
|
|
|
`<p><a href="http://localhost:3000/gogits/gogs/wiki/Link" rel="nofollow">Name</a></p>
|
2023-03-24 07:12:23 +01:00
|
|
|
`,
|
|
|
|
// empty
|
|
|
|
``,
|
|
|
|
// rendered
|
|
|
|
``,
|
|
|
|
}
|
|
|
|
|
2024-06-17 08:16:14 +02:00
|
|
|
testCasesWikiDocument := []string{
|
2017-02-14 02:13:59 +01:00
|
|
|
// wine-staging wiki home extract: special wiki syntax, images
|
|
|
|
`## What is Wine Staging?
|
|
|
|
**Wine Staging** on website [wine-staging.com](http://wine-staging.com).
|
|
|
|
|
|
|
|
## Quick Links
|
|
|
|
Here are some links to the most important topics. You can find the full list of pages at the sidebar.
|
|
|
|
|
|
|
|
[[Configuration]]
|
|
|
|
[[images/icon-bug.png]]
|
|
|
|
`,
|
|
|
|
// rendered
|
2019-12-23 23:38:50 +01:00
|
|
|
`<h2 id="user-content-what-is-wine-staging">What is Wine Staging?</h2>
|
2017-02-14 02:13:59 +01:00
|
|
|
<p><strong>Wine Staging</strong> on website <a href="http://wine-staging.com" rel="nofollow">wine-staging.com</a>.</p>
|
2019-12-23 23:38:50 +01:00
|
|
|
<h2 id="user-content-quick-links">Quick Links</h2>
|
2017-02-14 02:13:59 +01:00
|
|
|
<p>Here are some links to the most important topics. You can find the full list of pages at the sidebar.</p>
|
2024-06-17 08:16:14 +02:00
|
|
|
<p><a href="http://localhost:3000/gogits/gogs/wiki/Configuration" rel="nofollow">Configuration</a>
|
|
|
|
<a href="http://localhost:3000/gogits/gogs/wiki/raw/images/icon-bug.png" rel="nofollow"><img src="http://localhost:3000/gogits/gogs/wiki/raw/images/icon-bug.png" title="icon-bug.png" alt="images/icon-bug.png"/></a></p>
|
2017-02-14 02:13:59 +01:00
|
|
|
`,
|
|
|
|
}
|
|
|
|
|
2024-06-17 08:16:14 +02:00
|
|
|
for i := 0; i < len(testCasesWiki); i += 2 {
|
|
|
|
text := testCasesWiki[i]
|
|
|
|
response := testCasesWiki[i+1]
|
|
|
|
testRenderMarkdown(t, "gfm", true, text, response, http.StatusOK)
|
|
|
|
testRenderMarkup(t, "gfm", true, "", text, response, http.StatusOK)
|
|
|
|
testRenderMarkdown(t, "comment", true, text, response, http.StatusOK)
|
|
|
|
testRenderMarkup(t, "comment", true, "", text, response, http.StatusOK)
|
|
|
|
testRenderMarkup(t, "file", true, "path/test.md", text, response, http.StatusOK)
|
2017-02-14 02:13:59 +01:00
|
|
|
}
|
2023-03-24 07:12:23 +01:00
|
|
|
|
2024-06-17 08:16:14 +02:00
|
|
|
for i := 0; i < len(testCasesWikiDocument); i += 2 {
|
|
|
|
text := testCasesWikiDocument[i]
|
|
|
|
response := testCasesWikiDocument[i+1]
|
|
|
|
testRenderMarkdown(t, "gfm", true, text, response, http.StatusOK)
|
|
|
|
testRenderMarkup(t, "gfm", true, "", text, response, http.StatusOK)
|
|
|
|
testRenderMarkup(t, "file", true, "path/test.md", text, response, http.StatusOK)
|
2023-03-24 07:12:23 +01:00
|
|
|
}
|
|
|
|
|
2024-06-17 08:16:14 +02:00
|
|
|
input := "[Link](test.md)\n![Image](image.png)"
|
|
|
|
testRenderMarkdown(t, "gfm", false, input, `<p><a href="http://localhost:3000/gogits/gogs/src/branch/main/test.md" rel="nofollow">Link</a>
|
|
|
|
<a href="http://localhost:3000/gogits/gogs/media/branch/main/image.png" target="_blank" rel="nofollow noopener"><img src="http://localhost:3000/gogits/gogs/media/branch/main/image.png" alt="Image"/></a></p>
|
|
|
|
`, http.StatusOK)
|
|
|
|
|
|
|
|
testRenderMarkdown(t, "gfm", false, input, `<p><a href="http://localhost:3000/gogits/gogs/src/branch/main/test.md" rel="nofollow">Link</a>
|
|
|
|
<a href="http://localhost:3000/gogits/gogs/media/branch/main/image.png" target="_blank" rel="nofollow noopener"><img src="http://localhost:3000/gogits/gogs/media/branch/main/image.png" alt="Image"/></a></p>
|
|
|
|
`, http.StatusOK)
|
|
|
|
|
|
|
|
testRenderMarkup(t, "gfm", false, "", input, `<p><a href="http://localhost:3000/gogits/gogs/src/branch/main/test.md" rel="nofollow">Link</a>
|
|
|
|
<a href="http://localhost:3000/gogits/gogs/media/branch/main/image.png" target="_blank" rel="nofollow noopener"><img src="http://localhost:3000/gogits/gogs/media/branch/main/image.png" alt="Image"/></a></p>
|
|
|
|
`, http.StatusOK)
|
|
|
|
|
|
|
|
testRenderMarkup(t, "file", false, "path/new-file.md", input, `<p><a href="http://localhost:3000/gogits/gogs/src/branch/main/path/test.md" rel="nofollow">Link</a>
|
|
|
|
<a href="http://localhost:3000/gogits/gogs/media/branch/main/path/image.png" target="_blank" rel="nofollow noopener"><img src="http://localhost:3000/gogits/gogs/media/branch/main/path/image.png" alt="Image"/></a></p>
|
|
|
|
`, http.StatusOK)
|
|
|
|
|
|
|
|
testRenderMarkup(t, "file", true, "path/test.unknown", "## Test", "Unsupported render extension: .unknown\n", http.StatusUnprocessableEntity)
|
|
|
|
testRenderMarkup(t, "unknown", true, "", "## Test", "Unknown mode: unknown\n", http.StatusUnprocessableEntity)
|
2017-02-14 02:13:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var simpleCases = []string{
|
|
|
|
// Guard wiki sidebar: special syntax
|
|
|
|
`[[Guardfile-DSL / Configuring-Guard|Guardfile-DSL---Configuring-Guard]]`,
|
|
|
|
// rendered
|
|
|
|
`<p>[[Guardfile-DSL / Configuring-Guard|Guardfile-DSL---Configuring-Guard]]</p>
|
|
|
|
`,
|
|
|
|
// special syntax
|
|
|
|
`[[Name|Link]]`,
|
|
|
|
// rendered
|
|
|
|
`<p>[[Name|Link]]</p>
|
|
|
|
`,
|
|
|
|
// empty
|
|
|
|
``,
|
|
|
|
// rendered
|
|
|
|
``,
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAPI_RenderSimple(t *testing.T) {
|
|
|
|
setting.AppURL = AppURL
|
|
|
|
options := api.MarkdownOption{
|
|
|
|
Mode: "markdown",
|
|
|
|
Text: "",
|
2024-06-17 08:16:14 +02:00
|
|
|
Context: "/gogits/gogs",
|
2017-02-14 02:13:59 +01:00
|
|
|
}
|
2023-09-01 13:26:07 +02:00
|
|
|
ctx, resp := contexttest.MockAPIContext(t, "POST /api/v1/markdown")
|
2017-02-14 02:13:59 +01:00
|
|
|
for i := 0; i < len(simpleCases); i += 2 {
|
|
|
|
options.Text = simpleCases[i]
|
2021-01-26 16:36:53 +01:00
|
|
|
web.SetForm(ctx, &options)
|
|
|
|
Markdown(ctx)
|
2017-02-14 02:13:59 +01:00
|
|
|
assert.Equal(t, simpleCases[i+1], resp.Body.String())
|
|
|
|
resp.Body.Reset()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAPI_RenderRaw(t *testing.T) {
|
|
|
|
setting.AppURL = AppURL
|
2023-09-01 13:26:07 +02:00
|
|
|
ctx, resp := contexttest.MockAPIContext(t, "POST /api/v1/markdown")
|
2017-02-14 02:13:59 +01:00
|
|
|
for i := 0; i < len(simpleCases); i += 2 {
|
2021-09-22 07:38:34 +02:00
|
|
|
ctx.Req.Body = io.NopCloser(strings.NewReader(simpleCases[i]))
|
2017-02-14 02:13:59 +01:00
|
|
|
MarkdownRaw(ctx)
|
|
|
|
assert.Equal(t, simpleCases[i+1], resp.Body.String())
|
|
|
|
resp.Body.Reset()
|
|
|
|
}
|
|
|
|
}
|