code.gitea.io/gitea@v1.22.3/modules/markup/orgmode/orgmode_test.go (about)

     1  // Copyright 2017 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package markup
     5  
     6  import (
     7  	"strings"
     8  	"testing"
     9  
    10  	"code.gitea.io/gitea/modules/git"
    11  	"code.gitea.io/gitea/modules/markup"
    12  	"code.gitea.io/gitea/modules/setting"
    13  
    14  	"github.com/stretchr/testify/assert"
    15  )
    16  
    17  const AppURL = "http://localhost:3000/"
    18  
    19  func TestRender_StandardLinks(t *testing.T) {
    20  	setting.AppURL = AppURL
    21  
    22  	test := func(input, expected string, isWiki bool) {
    23  		buffer, err := RenderString(&markup.RenderContext{
    24  			Ctx: git.DefaultContext,
    25  			Links: markup.Links{
    26  				Base:       "/relative-path",
    27  				BranchPath: "branch/main",
    28  			},
    29  			IsWiki: isWiki,
    30  		}, input)
    31  		assert.NoError(t, err)
    32  		assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
    33  	}
    34  
    35  	test("[[https://google.com/]]",
    36  		`<p><a href="https://google.com/">https://google.com/</a></p>`, false)
    37  	test("[[WikiPage][The WikiPage Desc]]",
    38  		`<p><a href="/relative-path/wiki/WikiPage">The WikiPage Desc</a></p>`, true)
    39  	test("[[ImageLink.svg][The Image Desc]]",
    40  		`<p><a href="/relative-path/media/branch/main/ImageLink.svg">The Image Desc</a></p>`, false)
    41  }
    42  
    43  func TestRender_InternalLinks(t *testing.T) {
    44  	setting.AppURL = AppURL
    45  
    46  	test := func(input, expected string) {
    47  		buffer, err := RenderString(&markup.RenderContext{
    48  			Ctx: git.DefaultContext,
    49  			Links: markup.Links{
    50  				Base:       "/relative-path",
    51  				BranchPath: "branch/main",
    52  			},
    53  		}, input)
    54  		assert.NoError(t, err)
    55  		assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
    56  	}
    57  
    58  	test("[[file:test.org][Test]]",
    59  		`<p><a href="/relative-path/src/branch/main/test.org">Test</a></p>`)
    60  	test("[[./test.org][Test]]",
    61  		`<p><a href="/relative-path/src/branch/main/test.org">Test</a></p>`)
    62  	test("[[test.org][Test]]",
    63  		`<p><a href="/relative-path/src/branch/main/test.org">Test</a></p>`)
    64  	test("[[path/to/test.org][Test]]",
    65  		`<p><a href="/relative-path/src/branch/main/path/to/test.org">Test</a></p>`)
    66  }
    67  
    68  func TestRender_Media(t *testing.T) {
    69  	setting.AppURL = AppURL
    70  
    71  	test := func(input, expected string) {
    72  		buffer, err := RenderString(&markup.RenderContext{
    73  			Ctx: git.DefaultContext,
    74  			Links: markup.Links{
    75  				Base: "./relative-path",
    76  			},
    77  		}, input)
    78  		assert.NoError(t, err)
    79  		assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
    80  	}
    81  
    82  	test("[[file:../../.images/src/02/train.jpg]]",
    83  		`<p><img src=".images/src/02/train.jpg" alt=".images/src/02/train.jpg" /></p>`)
    84  	test("[[file:train.jpg]]",
    85  		`<p><img src="relative-path/train.jpg" alt="relative-path/train.jpg" /></p>`)
    86  
    87  	// With description.
    88  	test("[[https://example.com][https://example.com/example.svg]]",
    89  		`<p><a href="https://example.com"><img src="https://example.com/example.svg" alt="https://example.com/example.svg" /></a></p>`)
    90  	test("[[https://example.com][pre https://example.com/example.svg post]]",
    91  		`<p><a href="https://example.com">pre <img src="https://example.com/example.svg" alt="https://example.com/example.svg" /> post</a></p>`)
    92  	test("[[https://example.com][https://example.com/example.mp4]]",
    93  		`<p><a href="https://example.com"><video src="https://example.com/example.mp4">https://example.com/example.mp4</video></a></p>`)
    94  	test("[[https://example.com][pre https://example.com/example.mp4 post]]",
    95  		`<p><a href="https://example.com">pre <video src="https://example.com/example.mp4">https://example.com/example.mp4</video> post</a></p>`)
    96  
    97  	// Without description.
    98  	test("[[https://example.com/example.svg]]",
    99  		`<p><img src="https://example.com/example.svg" alt="https://example.com/example.svg" /></p>`)
   100  	test("[[https://example.com/example.mp4]]",
   101  		`<p><video src="https://example.com/example.mp4">https://example.com/example.mp4</video></p>`)
   102  
   103  	// test [[LINK][DESCRIPTION]] syntax with "file:" prefix
   104  	test(`[[https://example.com/][file:https://example.com/foo%20bar.svg]]`,
   105  		`<p><a href="https://example.com/"><img src="https://example.com/foo%20bar.svg" alt="https://example.com/foo%20bar.svg" /></a></p>`)
   106  	test(`[[file:https://example.com/foo%20bar.svg][Goto Image]]`,
   107  		`<p><a href="https://example.com/foo%20bar.svg">Goto Image</a></p>`)
   108  	test(`[[file:https://example.com/link][https://example.com/image.jpg]]`,
   109  		`<p><a href="https://example.com/link"><img src="https://example.com/image.jpg" alt="https://example.com/image.jpg" /></a></p>`)
   110  	test(`[[file:https://example.com/link][file:https://example.com/image.jpg]]`,
   111  		`<p><a href="https://example.com/link"><img src="https://example.com/image.jpg" alt="https://example.com/image.jpg" /></a></p>`)
   112  }
   113  
   114  func TestRender_Source(t *testing.T) {
   115  	setting.AppURL = AppURL
   116  
   117  	test := func(input, expected string) {
   118  		buffer, err := RenderString(&markup.RenderContext{
   119  			Ctx: git.DefaultContext,
   120  		}, input)
   121  		assert.NoError(t, err)
   122  		assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
   123  	}
   124  
   125  	test(`#+begin_src go
   126  // HelloWorld prints "Hello World"
   127  func HelloWorld() {
   128  	fmt.Println("Hello World")
   129  }
   130  #+end_src
   131  `, `<div class="src src-go">
   132  <pre><code class="chroma language-go"><span class="c1">// HelloWorld prints &#34;Hello World&#34;
   133  </span><span class="c1"></span><span class="kd">func</span> <span class="nf">HelloWorld</span><span class="p">()</span> <span class="p">{</span>
   134  	<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="s">&#34;Hello World&#34;</span><span class="p">)</span>
   135  <span class="p">}</span></code></pre>
   136  </div>`)
   137  }