Skip to content
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

Merged
merged 6 commits into from
Feb 7, 2024
Merged

Fix orgmode link resolving #29024

merged 6 commits into from
Feb 7, 2024

Conversation

wxiaoguang
Copy link
Contributor

@wxiaoguang wxiaoguang commented Feb 2, 2024

Fix #28974

Add some new tests and fix some legacy unclear tests.

@wxiaoguang wxiaoguang added type/bug backport/v1.21 This PR should be backported to Gitea 1.21 labels Feb 2, 2024
@wxiaoguang wxiaoguang added this to the 1.22.0 milestone Feb 2, 2024
@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Feb 2, 2024
@pull-request-size pull-request-size bot added the size/S Denotes a PR that changes 10-29 lines, ignoring generated files. label Feb 2, 2024
@pull-request-size pull-request-size bot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Feb 2, 2024
@GiteaBot GiteaBot added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. labels Feb 2, 2024
@KN4CK3R
Copy link
Member

KN4CK3R commented Feb 2, 2024

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.

- [[docs/running.md][relative]]
- [[/docs/running.md][absolute]]
- [[file:docs/running.md][relative file]]
- [[file:/docs/running.md][absolute file]]

@wxiaoguang
Copy link
Contributor Author

I think this PR's main purpose is to "fix" the 500 error (the [[link][description]] syntax).

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.

@KN4CK3R
Copy link
Member

KN4CK3R commented Feb 2, 2024

If branch infos are present, the wrong url is generated:

Links: markup.Links{
	Base: "/relative-path",
	BranchPath: "branch/main",
},

test("[[WikiPage][The WikiPage Name]]", `<p><a href="/relative-path/src/branch/main/WikiPage">The WikiPage Name</a></p>`)
// <p><a href="/relative-path/media/branch/main/WikiPage">The WikiPage Name</a></p>

The generated path uses the media path but the link is no media.

@wxiaoguang
Copy link
Contributor Author

wxiaoguang commented Feb 3, 2024

If branch infos are present, the wrong url is generated:
The generated path uses the media path but the link is no media.

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 link Kind == regular for [[ImageLink.svg][The Image Desc]] , so it is not treated as "media" (no branch path).

So, either I revert the code to that old (but buggy) behavior, or add more code to "fix". What do you think ?

@wxiaoguang
Copy link
Contributor Author

Made some "fixes" in 6cd906b, are they right now?

	test("[[WikiPage][The WikiPage Desc]]",
		`<p><a href="/relative-path/WikiPage">The WikiPage Desc</a></p>`)
	test("[[ImageLink.svg][The Image Desc]]",
		`<p><a href="/relative-path/media/branch/main/ImageLink.svg">The Image Desc</a></p>`)

@KN4CK3R
Copy link
Member

KN4CK3R commented Feb 6, 2024

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="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.

@wxiaoguang
Copy link
Contributor Author

The test cases are really helpful, thank you very much.

The following may be a different PR.

So if I understand correctly, this PR doesn't need to introduce more changes at the moment?

@KN4CK3R
Copy link
Member

KN4CK3R commented Feb 7, 2024

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.

@GiteaBot GiteaBot added lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. and removed lgtm/need 1 This PR needs approval from one additional maintainer to be merged. labels Feb 7, 2024
@lunny lunny added the reviewed/wait-merge This pull request is part of the merge queue. It will be merged soon. label Feb 7, 2024
@lunny lunny enabled auto-merge (squash) February 7, 2024 08:07
@lunny lunny merged commit 2bac85d into go-gitea:main Feb 7, 2024
25 checks passed
@GiteaBot GiteaBot removed the reviewed/wait-merge This pull request is part of the merge queue. It will be merged soon. label Feb 7, 2024
@wxiaoguang wxiaoguang deleted the fix-orgmode-link branch February 7, 2024 08:33
@GiteaBot
Copy link
Contributor

GiteaBot commented Feb 7, 2024

I was unable to create a backport for 1.21. @wxiaoguang, please send one manually. 🍵

go run ./contrib/backport 29024
...  // fix git conflicts if any
go run ./contrib/backport --continue

@GiteaBot GiteaBot added the backport/manual No power to the bots! Create your backport yourself! label Feb 7, 2024
wxiaoguang added a commit to wxiaoguang/gitea that referenced this pull request Feb 7, 2024
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
wxiaoguang added a commit to wxiaoguang/gitea that referenced this pull request Feb 7, 2024
Fix go-gitea#28974

Add some new tests and fix some legacy unclear tests.
# Conflicts:
#	modules/markup/orgmode/orgmode_test.go
@wxiaoguang wxiaoguang added the backport/done All backports for this PR have been created label Feb 7, 2024
silverwind pushed a commit that referenced this pull request Feb 7, 2024
Backport #29024
Also backport #27968 (remove unnecessary titles)

Fix #28974

Add some new tests and fix some legacy unclear tests.
silverwind pushed a commit to silverwind/gitea that referenced this pull request Feb 20, 2024
Fix go-gitea#28974

Add some new tests and fix some legacy unclear tests.
wxiaoguang added a commit that referenced this pull request Feb 21, 2024
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)
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 2, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
backport/done All backports for this PR have been created backport/manual No power to the bots! Create your backport yourself! backport/v1.21 This PR should be backported to Gitea 1.21 lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. type/bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

README.org casuing 500 internal server error
5 participants