Skip to content

Align markdown and orgmode link rendering #29100

Closed
@KN4CK3R

Description

@KN4CK3R

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:

  • \n gets rendered as <br/>\n in markdown and just \n in orgmode
  • links in markdown get decorated with target and rel attributes.

Originally posted by @KN4CK3R in #29024 (comment)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions