2017-09-20 23:20:14 -06:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
2022-11-27 11:20:29 -07:00
|
|
|
// SPDX-License-Identifier: MIT
|
2017-09-20 23:20:14 -06:00
|
|
|
|
|
|
|
package markup
|
|
|
|
|
|
|
|
import (
|
2024-11-21 22:48:09 -07:00
|
|
|
"os"
|
2017-09-20 23:20:14 -06:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2021-04-19 16:25:08 -06:00
|
|
|
"code.gitea.io/gitea/modules/markup"
|
2017-09-20 23:20:14 -06:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2024-11-21 22:48:09 -07:00
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
setting.AppURL = "http://localhost:3000/"
|
|
|
|
setting.IsInTesting = true
|
|
|
|
os.Exit(m.Run())
|
|
|
|
}
|
2017-09-20 23:20:14 -06:00
|
|
|
|
|
|
|
func TestRender_StandardLinks(t *testing.T) {
|
2024-11-24 01:18:57 -07:00
|
|
|
test := func(input, expected string) {
|
|
|
|
buffer, err := RenderString(markup.NewTestRenderContext("/relative-path/media/branch/main/"), input)
|
2021-04-19 16:25:08 -06:00
|
|
|
assert.NoError(t, err)
|
2017-09-20 23:20:14 -06:00
|
|
|
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
|
|
|
|
}
|
|
|
|
|
2023-11-09 18:45:13 -07:00
|
|
|
test("[[https://google.com/]]",
|
2024-11-24 01:18:57 -07:00
|
|
|
`<p><a href="https://google.com/">https://google.com/</a></p>`)
|
2024-02-07 01:32:31 -07:00
|
|
|
test("[[ImageLink.svg][The Image Desc]]",
|
2024-11-24 01:18:57 -07:00
|
|
|
`<p><a href="/relative-path/media/branch/main/ImageLink.svg">The Image Desc</a></p>`)
|
2024-03-10 10:30:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRender_InternalLinks(t *testing.T) {
|
|
|
|
test := func(input, expected string) {
|
2024-11-24 01:18:57 -07:00
|
|
|
buffer, err := RenderString(markup.NewTestRenderContext("/relative-path/src/branch/main"), input)
|
2024-03-10 10:30:14 -06:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
|
|
|
|
}
|
|
|
|
|
|
|
|
test("[[file:test.org][Test]]",
|
|
|
|
`<p><a href="/relative-path/src/branch/main/test.org">Test</a></p>`)
|
|
|
|
test("[[./test.org][Test]]",
|
|
|
|
`<p><a href="/relative-path/src/branch/main/test.org">Test</a></p>`)
|
|
|
|
test("[[test.org][Test]]",
|
|
|
|
`<p><a href="/relative-path/src/branch/main/test.org">Test</a></p>`)
|
|
|
|
test("[[path/to/test.org][Test]]",
|
|
|
|
`<p><a href="/relative-path/src/branch/main/path/to/test.org">Test</a></p>`)
|
2017-09-20 23:20:14 -06:00
|
|
|
}
|
|
|
|
|
2023-09-12 23:44:59 -06:00
|
|
|
func TestRender_Media(t *testing.T) {
|
2017-09-20 23:20:14 -06:00
|
|
|
test := func(input, expected string) {
|
2024-11-24 01:18:57 -07:00
|
|
|
buffer, err := RenderString(markup.NewTestRenderContext("./relative-path"), input)
|
2021-04-19 16:25:08 -06:00
|
|
|
assert.NoError(t, err)
|
2017-09-20 23:20:14 -06:00
|
|
|
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
|
|
|
|
}
|
|
|
|
|
2024-02-07 01:32:31 -07:00
|
|
|
test("[[file:../../.images/src/02/train.jpg]]",
|
2024-11-29 01:08:29 -07:00
|
|
|
`<p><img src=".images/src/02/train.jpg" alt=".images/src/02/train.jpg"></p>`)
|
2024-02-07 01:32:31 -07:00
|
|
|
test("[[file:train.jpg]]",
|
2024-11-29 01:08:29 -07:00
|
|
|
`<p><img src="relative-path/train.jpg" alt="relative-path/train.jpg"></p>`)
|
2023-09-12 23:44:59 -06:00
|
|
|
|
|
|
|
// With description.
|
|
|
|
test("[[https://example.com][https://example.com/example.svg]]",
|
2024-11-29 01:08:29 -07:00
|
|
|
`<p><a href="https://example.com"><img src="https://example.com/example.svg" alt="https://example.com/example.svg"></a></p>`)
|
2023-11-09 18:45:13 -07:00
|
|
|
test("[[https://example.com][pre https://example.com/example.svg post]]",
|
2024-11-29 01:08:29 -07:00
|
|
|
`<p><a href="https://example.com">pre <img src="https://example.com/example.svg" alt="https://example.com/example.svg"> post</a></p>`)
|
2023-09-12 23:44:59 -06:00
|
|
|
test("[[https://example.com][https://example.com/example.mp4]]",
|
2023-11-09 18:45:13 -07:00
|
|
|
`<p><a href="https://example.com"><video src="https://example.com/example.mp4">https://example.com/example.mp4</video></a></p>`)
|
|
|
|
test("[[https://example.com][pre https://example.com/example.mp4 post]]",
|
|
|
|
`<p><a href="https://example.com">pre <video src="https://example.com/example.mp4">https://example.com/example.mp4</video> post</a></p>`)
|
2023-09-12 23:44:59 -06:00
|
|
|
|
|
|
|
// Without description.
|
|
|
|
test("[[https://example.com/example.svg]]",
|
2024-11-29 01:08:29 -07:00
|
|
|
`<p><img src="https://example.com/example.svg" alt="https://example.com/example.svg"></p>`)
|
2023-09-12 23:44:59 -06:00
|
|
|
test("[[https://example.com/example.mp4]]",
|
2023-11-09 18:45:13 -07:00
|
|
|
`<p><video src="https://example.com/example.mp4">https://example.com/example.mp4</video></p>`)
|
2024-02-07 01:32:31 -07:00
|
|
|
|
|
|
|
// test [[LINK][DESCRIPTION]] syntax with "file:" prefix
|
|
|
|
test(`[[https://example.com/][file:https://example.com/foo%20bar.svg]]`,
|
2024-11-29 01:08:29 -07:00
|
|
|
`<p><a href="https://example.com/"><img src="https://example.com/foo%20bar.svg" alt="https://example.com/foo%20bar.svg"></a></p>`)
|
2024-02-07 01:32:31 -07:00
|
|
|
test(`[[file:https://example.com/foo%20bar.svg][Goto Image]]`,
|
|
|
|
`<p><a href="https://example.com/foo%20bar.svg">Goto Image</a></p>`)
|
|
|
|
test(`[[file:https://example.com/link][https://example.com/image.jpg]]`,
|
2024-11-29 01:08:29 -07:00
|
|
|
`<p><a href="https://example.com/link"><img src="https://example.com/image.jpg" alt="https://example.com/image.jpg"></a></p>`)
|
2024-02-07 01:32:31 -07:00
|
|
|
test(`[[file:https://example.com/link][file:https://example.com/image.jpg]]`,
|
2024-11-29 01:08:29 -07:00
|
|
|
`<p><a href="https://example.com/link"><img src="https://example.com/image.jpg" alt="https://example.com/image.jpg"></a></p>`)
|
2017-09-20 23:20:14 -06:00
|
|
|
}
|
2021-09-24 07:29:32 -06:00
|
|
|
|
|
|
|
func TestRender_Source(t *testing.T) {
|
|
|
|
test := func(input, expected string) {
|
2024-11-21 22:48:09 -07:00
|
|
|
buffer, err := RenderString(markup.NewTestRenderContext(), input)
|
2021-09-24 07:29:32 -06:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
|
|
|
|
}
|
|
|
|
|
|
|
|
test(`#+begin_src go
|
|
|
|
// HelloWorld prints "Hello World"
|
|
|
|
func HelloWorld() {
|
|
|
|
fmt.Println("Hello World")
|
|
|
|
}
|
|
|
|
#+end_src
|
|
|
|
`, `<div class="src src-go">
|
2022-09-25 23:50:03 -06:00
|
|
|
<pre><code class="chroma language-go"><span class="c1">// HelloWorld prints "Hello World"
|
|
|
|
</span><span class="c1"></span><span class="kd">func</span> <span class="nf">HelloWorld</span><span class="p">()</span> <span class="p">{</span>
|
|
|
|
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="s">"Hello World"</span><span class="p">)</span>
|
|
|
|
<span class="p">}</span></code></pre>
|
2021-09-24 07:29:32 -06:00
|
|
|
</div>`)
|
|
|
|
}
|