go-gitea / gitea

Git with a cup of tea! Painless self-hosted all-in-one software development service, including Git hosting, code review, team collaboration, package registry and CI/CD
https://gitea.com
MIT License
44.41k stars 5.43k forks source link

Align markdown and orgmode link rendering #29100

Open KN4CK3R opened 7 months ago

KN4CK3R commented 7 months ago

Do we want the orgmode renderer behave like the markdown renderer? I created multiple test cases extracted from the markdown renderer:

    input := `/just/a/path.bin
https://example.com/file.bin
[[file:file.bin][local link]]
[[https://example.com][remote link]]
[[file:image.jpg][local image]]
[[file:path/file.jpg][local image]]
[[file:/path/file.jpg][local image]]
[[file:https://example.com/image.jpg][remote image]]`

    cases := []struct {
        Links    markup.Links
        Expected string
    }{
        { // 0
            Links:  markup.Links{},
            Expected: `<p>/just/a/path.bin<br/>
<a href="https://example.com/file.bin" rel="nofollow">https://example.com/file.bin</a><br/>
<a href="/file.bin" rel="nofollow">local link</a><br/>
<a href="https://example.com" rel="nofollow">remote link</a><br/>
<a href="/image.jpg" target="_blank" rel="nofollow noopener"><img src="/image.jpg" alt="local image"/></a><br/>
<a href="/path/file.jpg" target="_blank" rel="nofollow noopener"><img src="/path/file.jpg" alt="local image"/></a><br/>
<a href="/path/file.jpg" target="_blank" rel="nofollow noopener"><img src="/path/file.jpg" alt="local image"/></a><br/>
<a href="https://example.com/image.jpg" target="_blank" rel="nofollow noopener"><img src="https://example.com/image.jpg" alt="remote image"/></a></p>`,
        },
        { // 1
            Links:  markup.Links{
                Base:"https://gitea.io/",
            },
            Expected: `<p>/just/a/path.bin<br/>
<a href="https://example.com/file.bin" rel="nofollow">https://example.com/file.bin</a><br/>
<a href="https://gitea.io/file.bin" rel="nofollow">local link</a><br/>
<a href="https://example.com" rel="nofollow">remote link</a><br/>
<a href="https://gitea.io/image.jpg" target="_blank" rel="nofollow noopener"><img src="https://gitea.io/image.jpg" alt="local image"/></a><br/>
<a href="https://gitea.io/path/file.jpg" target="_blank" rel="nofollow noopener"><img src="https://gitea.io/path/file.jpg" alt="local image"/></a><br/>
<a href="https://gitea.io/path/file.jpg" target="_blank" rel="nofollow noopener"><img src="https://gitea.io/path/file.jpg" alt="local image"/></a><br/>
<a href="https://example.com/image.jpg" target="_blank" rel="nofollow noopener"><img src="https://example.com/image.jpg" alt="remote image"/></a></p>`,
        },
        { // 2
            Links:  markup.Links{
                Base: "/relative/path",
            },
            Expected: `<p>/just/a/path.bin<br/>
<a href="https://example.com/file.bin" rel="nofollow">https://example.com/file.bin</a><br/>
<a href="/relative/path/file.bin" rel="nofollow">local link</a><br/>
<a href="https://example.com" rel="nofollow">remote link</a><br/>
<a href="/relative/path/image.jpg" target="_blank" rel="nofollow noopener"><img src="/relative/path/image.jpg" alt="local image"/></a><br/>
<a href="/relative/path/path/file.jpg" target="_blank" rel="nofollow noopener"><img src="/relative/path/path/file.jpg" alt="local image"/></a><br/>
<a href="/relative/path/path/file.jpg" target="_blank" rel="nofollow noopener"><img src="/relative/path/path/file.jpg" alt="local image"/></a><br/>
<a href="https://example.com/image.jpg" target="_blank" rel="nofollow noopener"><img src="https://example.com/image.jpg" alt="remote image"/></a></p>`,
        },
        { // 3
            Links:  markup.Links{
                Base:       "/user/repo",
                BranchPath: "branch/main",
            },
            Expected: `<p>/just/a/path.bin<br/>
<a href="https://example.com/file.bin" rel="nofollow">https://example.com/file.bin</a><br/>
<a href="/user/repo/src/branch/main/file.bin" rel="nofollow">local link</a><br/>
<a href="https://example.com" rel="nofollow">remote link</a><br/>
<a href="/user/repo/media/branch/main/image.jpg" target="_blank" rel="nofollow noopener"><img src="/user/repo/media/branch/main/image.jpg" alt="local image"/></a><br/>
<a href="/user/repo/media/branch/main/path/file.jpg" target="_blank" rel="nofollow noopener"><img src="/user/repo/media/branch/main/path/file.jpg" alt="local image"/></a><br/>
<a href="/user/repo/media/branch/main/path/file.jpg" target="_blank" rel="nofollow noopener"><img src="/user/repo/media/branch/main/path/file.jpg" alt="local image"/></a><br/>
<a href="https://example.com/image.jpg" target="_blank" rel="nofollow noopener"><img src="https://example.com/image.jpg" alt="remote image"/></a></p>`,
        },
        { // 4
            Links:  markup.Links{
                Base:     "/relative/path",
                TreePath: "sub/folder",
            },
            Expected: `<p>/just/a/path.bin<br/>
<a href="https://example.com/file.bin" rel="nofollow">https://example.com/file.bin</a><br/>
<a href="/relative/path/file.bin" rel="nofollow">local link</a><br/>
<a href="https://example.com" rel="nofollow">remote link</a><br/>
<a href="/relative/path/image.jpg" target="_blank" rel="nofollow noopener"><img src="/relative/path/image.jpg" alt="local image"/></a><br/>
<a href="/relative/path/path/file.jpg" target="_blank" rel="nofollow noopener"><img src="/relative/path/path/file.jpg" alt="local image"/></a><br/>
<a href="/relative/path/path/file.jpg" target="_blank" rel="nofollow noopener"><img src="/relative/path/path/file.jpg" alt="local image"/></a><br/>
<a href="https://example.com/image.jpg" target="_blank" rel="nofollow noopener"><img src="https://example.com/image.jpg" alt="remote image"/></a></p>`,
        },
        { // 5
            Links:  markup.Links{
                Base:       "/user/repo",
                BranchPath: "branch/main",
                TreePath:   "sub/folder",
            },
            Expected: `<p>/just/a/path.bin<br/>
<a href="https://example.com/file.bin" rel="nofollow">https://example.com/file.bin</a><br/>
<a href="/user/repo/src/branch/main/sub/folder/file.bin" rel="nofollow">local link</a><br/>
<a href="https://example.com" rel="nofollow">remote link</a><br/>
<a href="/user/repo/media/branch/main/sub/folder/image.jpg" target="_blank" rel="nofollow noopener"><img src="/user/repo/media/branch/main/sub/folder/image.jpg" alt="local image"/></a><br/>
<a href="/user/repo/media/branch/main/sub/folder/path/file.jpg" target="_blank" rel="nofollow noopener"><img src="/user/repo/media/branch/main/sub/folder/path/file.jpg" alt="local image"/></a><br/>
<a href="/user/repo/media/branch/main/sub/folder/path/file.jpg" target="_blank" rel="nofollow noopener"><img src="/user/repo/media/branch/main/sub/folder/path/file.jpg" alt="local image"/></a><br/>
<a href="https://example.com/image.jpg" target="_blank" rel="nofollow noopener"><img src="https://example.com/image.jpg" alt="remote image"/></a></p>`,
        },
    }

    for i, c := range cases {
        result, err := RenderString(&markup.RenderContext{Ctx: context.Background(), Links: c.Links}, input)
        assert.NoError(t, err, "Unexpected error in testcase: %v", i)
        assert.Equal(t, c.Expected, result, "Unexpected result in testcase %v", i)
    }

The corresponding markdown input looks like this:

input := `/just/a/path.bin
https://example.com/file.bin
[local link](file.bin)
[remote link](https://example.com)
![local image](image.jpg)
![local image](path/file.jpg)
![local image](/path/file.jpg)
![remote image](https://example.com/image.jpg)`

Things I noticed:

Originally posted by @KN4CK3R in https://github.com/go-gitea/gitea/issues/29024#issuecomment-1929989609

silverwind commented 7 months ago

Does Orgmode have a spec that shows expected HTML output like CommonMark for Markdown? If so, might be worth to look there. Otherwise I'd lean towards bringing the render output closer together, maybe even so close as to share functions for link generation etc.