-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix orgmode link resolving #29024
Fix orgmode link resolving #29024
Conversation
35a4dd4
to
dda1a35
Compare
dda1a35
to
d868b92
Compare
e733bdc
to
28aa745
Compare
The markdown renderer had some problems with relative and absolute links (#15075, #28915) (fixed by #28932). The org mode renderer has the same problem. Should we fix that too? I think we don't have a bug report at the moment because only a few people use org mode.
|
I think this PR's main purpose is to "fix" the 500 error (the Feel free to edit it and add more fixes (eg: "relative and absolute links"), maybe the fixes could also be done in a separate PR. Either is fine to me. |
If branch infos are present, the wrong url is generated:
The generated path uses the |
Hmm, I see. I think I understand the old bug now. By old code, in many cases the "relative link" is still incorrect, because orgmode reports So, either I revert the code to that old (but buggy) behavior, or add more code to "fix". What do you think ? |
Made some "fixes" in 6cd906b, are they right now?
|
The following may be a different PR. Do we want the orgmode renderer behave like the markdown renderer? I would say yes but then we may have to put more work in this. 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="">https://example.com/file.bin</a><br/>
<a href="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/file.bin" rel="">local link</a><br/>
<a href="https://example.com" rel="">remote link</a><br/>
<a href="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/image.jpg" target="_blank" rel=" noopener"><img src="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/image.jpg" alt="local image"/></a><br/>
<a href="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/path/file.jpg" target="_blank" rel=" noopener"><img src="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/path/file.jpg" alt="local image"/></a><br/>
<a href="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/path/file.jpg" target="_blank" rel=" noopener"><img src="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/path/file.jpg" alt="local image"/></a><br/>
<a href="https://example.com/image.jpg" target="_blank" rel=" 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="">https://example.com/file.bin</a><br/>
<a href="https://gitea.io/file.bin" rel="">local link</a><br/>
<a href="https://example.com" rel="">remote link</a><br/>
<a href="https://gitea.io/image.jpg" target="_blank" rel=" noopener"><img src="https://gitea.io/image.jpg" alt="local image"/></a><br/>
<a href="https://gitea.io/path/file.jpg" target="_blank" rel=" 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=" noopener"><img src="https://gitea.io/path/file.jpg" alt="local image"/></a><br/>
<a href="https://example.com/image.jpg" target="_blank" rel=" 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="">https://example.com/file.bin</a><br/>
<a href="/relative/path/file.bin" rel="">local link</a><br/>
<a href="https://example.com" rel="">remote link</a><br/>
<a href="/relative/path/image.jpg" target="_blank" rel=" noopener"><img src="/relative/path/image.jpg" alt="local image"/></a><br/>
<a href="/relative/path/path/file.jpg" target="_blank" rel=" noopener"><img src="/relative/path/path/file.jpg" alt="local image"/></a><br/>
<a href="/relative/path/path/file.jpg" target="_blank" rel=" noopener"><img src="/relative/path/path/file.jpg" alt="local image"/></a><br/>
<a href="https://example.com/image.jpg" target="_blank" rel=" 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="">https://example.com/file.bin</a><br/>
<a href="/user/repo/src/branch/main/file.bin" rel="">local link</a><br/>
<a href="https://example.com" rel="">remote link</a><br/>
<a href="/user/repo/media/branch/main/image.jpg" target="_blank" rel=" 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=" 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=" 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=" 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="">https://example.com/file.bin</a><br/>
<a href="/relative/path/file.bin" rel="">local link</a><br/>
<a href="https://example.com" rel="">remote link</a><br/>
<a href="/relative/path/image.jpg" target="_blank" rel=" noopener"><img src="/relative/path/image.jpg" alt="local image"/></a><br/>
<a href="/relative/path/path/file.jpg" target="_blank" rel=" noopener"><img src="/relative/path/path/file.jpg" alt="local image"/></a><br/>
<a href="/relative/path/path/file.jpg" target="_blank" rel=" noopener"><img src="/relative/path/path/file.jpg" alt="local image"/></a><br/>
<a href="https://example.com/image.jpg" target="_blank" rel=" 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="">https://example.com/file.bin</a><br/>
<a href="/user/repo/src/branch/main/sub/folder/file.bin" rel="">local link</a><br/>
<a href="https://example.com" rel="">remote link</a><br/>
<a href="/user/repo/media/branch/main/sub/folder/image.jpg" target="_blank" rel=" 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=" 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=" 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=" 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:
|
The test cases are really helpful, thank you very much.
So if I understand correctly, this PR doesn't need to introduce more changes at the moment? |
Yes, want to let you decide if you want to make changes here or we use another PR to adjust the code. It's different to markdown for a long time, no need to hurry. |
I was unable to create a backport for 1.21. @wxiaoguang, please send one manually. 🍵
|
Fix go-gitea#28974 Add some new tests and fix some legacy unclear tests. # Conflicts: # modules/markup/orgmode/orgmode.go # modules/markup/orgmode/orgmode_test.go
Fix go-gitea#28974 Add some new tests and fix some legacy unclear tests. # Conflicts: # modules/markup/orgmode/orgmode_test.go
Fix go-gitea#28974 Add some new tests and fix some legacy unclear tests.
Follow #29024 Major changes: * refactor validLinksPattern to fullURLPattern and add comments, now it accepts "protocol:" prefix * rename `IsLink*` to `IsFullURL*`, and remove unnecessray "mailto:" check * fix some comments (by the way) * rename EmojiShortCodeRegex -> emojiShortCodeRegex (by the way)
Fix #28974
Add some new tests and fix some legacy unclear tests.