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 incorrect replacement in markdown image links #26562

Conversation

CaiCandong
Copy link
Member

@CaiCandong CaiCandong commented Aug 17, 2023

Fix #26548
When the repo name is src:

  • /<user>/src/src/branch/ => /<user>/media/src/branch/

it should be:

  • /<user>/src/src/branch/ => /<user>/src/media/branch/

@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Aug 17, 2023
@pull-request-size pull-request-size bot added the size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. label Aug 17, 2023
@CaiCandong CaiCandong changed the title fix incorrect replacement in markdown image links Fix incorrect replacement in markdown image links Aug 17, 2023
@puni9869
Copy link
Member

puni9869 commented Aug 17, 2023

Please also check: if we are not inserting the image link into DB. <--- Less probable situation.

@CaiCandong
Copy link
Member Author

Please also check: if we are not inserting the image link into DB. <--- Less probable situation.

ok , but I'm puzzled about this unit test case that didn't pass

@puni9869
Copy link
Member

Please also check: if we are not inserting the image link into DB. <--- Less probable situation.

ok , but I'm puzzled about this unit test case that didn't pass

Your unit test cases are failing, you changed the path of image so you need to change in the unit test cases.
I am wondering, it should not affect in older versions of gitea.

https://github.com/go-gitea/gitea/actions/runs/5887628224/job/15967326590?pr=26562#step:8:112

@CaiCandong
Copy link
Member Author

Your unit test cases are failing, you changed the path of image so you need to change in the unit test cases.
I am wondering, it should not affect in older versions of gitea.

I am confused about this test case. For both test cases, the rendering before and after my changes in the page display is consistent:

  • <img src="Link"> => http://localhost:3000/caicandong/wire-examples/media/branch/main/Link
  • <img src="./icon.png">=>http://localhost:3000/caicandong/wire-examples/media/branch/main/icon.png

@puni9869
Copy link
Member

Your unit test cases are failing, you changed the path of image so you need to change in the unit test cases.
I am wondering, it should not affect in older versions of gitea.

I am confused about this test case. For both test cases, the rendering before and after my changes in the page display is consistent:

  • <img src="Link"> => http://localhost:3000/caicandong/wire-examples/media/branch/main/Link
  • <img src="./icon.png">=>http://localhost:3000/caicandong/wire-examples/media/branch/main/icon.png

Can you push your code. I can take a look.

@yp05327
Copy link
Contributor

yp05327 commented Aug 17, 2023

At first, I also considered to add /branch/, but it seems that we do not only have /branch behind /media/:

gitea/routers/web/web.go

Lines 1312 to 1319 in c6b92c8

m.Group("/media", func() {
m.Get("/branch/*", context.RepoRefByType(context.RepoRefBranch), repo.SingleDownloadOrLFS)
m.Get("/tag/*", context.RepoRefByType(context.RepoRefTag), repo.SingleDownloadOrLFS)
m.Get("/commit/*", context.RepoRefByType(context.RepoRefCommit), repo.SingleDownloadOrLFS)
m.Get("/blob/{sha}", context.RepoRefByType(context.RepoRefBlob), repo.DownloadByIDOrLFS)
// "/*" route is deprecated, and kept for backward compatibility
m.Get("/*", context.RepoRefByType(context.RepoRefLegacy), repo.SingleDownloadOrLFS)
}, repo.MustBeNotEmpty, reqRepoCodeReader)

Is it ok that just support /branch here?
Maybe CI failure is related to this problem.

So maybe we need to combine the url instead of replace /src/?

@CaiCandong
Copy link
Member Author

Is it ok that just support /branch here?

That's not my concern, I do think there's something wrong with this test case. For the current gitea it is actually the rendered result below.

  • <img src="Link"> => http://localhost:3000/caicandong/wire-examples/media/branch/main/Link
  • <img src="./icon.png">=>http://localhost:3000/caicandong/wire-examples/media/branch/main/icon.png
    And the test case is:
  • <img src="Link"> => http://localhost:3000/caicandong/wire-examples/media/main/Link
  • <img src="./icon.png">=>http://localhost:3000/caicandong/wire-examples/media/main/icon.png

@puni9869
Copy link
Member

expected: "<img src=\"http://localhost:3000/gogits/gogs/media/master/Link\"/>"
        	            	actual  : "<img src=\"http://localhost:3000/gogits/gogs/src/master/Link\"/>"
        	            	
        	            	Diff:
        	            	--- Expected
        	            	+++ Actual
        	            	@@ -1 +1 @@
        	            	-<img src="http://localhost:3000/gogits/gogs/media/master/Link"/>
        	            	+<img src="http://localhost:3000/gogits/gogs/src/master/Link"/>
        	Test:       	TestRender_RelativeImages
    html_test.go:475: 
        	Error Trace:	/home/runner/work/gitea/gitea/modules/markup/html_test.go:475
        	            				/home/runner/work/gitea/gitea/modules/markup/html_test.go:494
        	Error:      	Not equal: 
        	            	expected: "<img src=\"http://localhost:3000/gogits/gogs/media/master/icon.png\"/>"
        	            	actual  : "<img src=\"http://localhost:3000/gogits/gogs/src/master/icon.png\"/>"
        	            	
        	            	Diff:
        	            	--- Expected
        	            	+++ Actual
        	            	@@ -1 +1 @@
        	            	-<img src="http://localhost:3000/gogits/gogs/media/master/icon.png"/>
        	            	+<img src="http://localhost:3000/gogits/gogs/src/master/icon.png"/>
        	Test:       	TestRender_RelativeImages

The path that is forming in the test case is wrong. Still src is coming in the path.

@puni9869
Copy link
Member

puni9869 commented Aug 17, 2023

Check line
image
in html_test.go
Similarly at other failing test cases too.

image

@CaiCandong
Copy link
Member Author

CaiCandong commented Aug 17, 2023

What I mean is that there is something wrong with this test case, the expected results are not the same as what I see on the gitea page. @puni9869 @yp05327
https://try.gitea.io/CaiCandong/abc/src/branch/main/README.md

@yp05327
Copy link
Contributor

yp05327 commented Aug 17, 2023

// "/*" route is deprecated, and kept for backward compatibility

It seems that in old version the media link is /media/<branchname>
but now it is /media/branch/<branchname>

@yp05327
Copy link
Contributor

yp05327 commented Aug 17, 2023

I see why this fix is not good.
As I have mentioned in #26562 (comment)
If you select to a tag or commit, the media link will become /media/tag/<TagName>

for example:
https://try.gitea.io/yp05327/testrepo/src/tag/testtag222

image

@CaiCandong
Copy link
Member Author

I see why this fix is not good.

I'm not saying that my current solution is right, I think we need to address this outdated test case first and then consider a good solution.

@KN4CK3R
Copy link
Member

KN4CK3R commented Aug 17, 2023

There are other places which replace /src/ with /raw/ for example which will fail too. The whole string replacing solution is buggy.

@silverwind
Copy link
Member

Maybe split string on /, replace at n-th position, then re-join.

@CaiCandong CaiCandong marked this pull request as draft August 17, 2023 13:16
@CaiCandong
Copy link
Member Author

No good solution for now, this bug only affects users or repositories with the name src.

@CaiCandong CaiCandong closed this Aug 17, 2023
@puni9869
Copy link
Member

No worries, You have tried , Appreciated for the great efforts.

@KN4CK3R
Copy link
Member

KN4CK3R commented Aug 17, 2023

I will post another solution.

@KN4CK3R
Copy link
Member

KN4CK3R commented Aug 26, 2023

Added a fix with #26745

@CaiCandong CaiCandong deleted the fix-incorrect-replace-in-markdown-image-links branch September 4, 2023 12:40
@go-gitea go-gitea locked as resolved and limited conversation to collaborators Nov 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. size/XS Denotes a PR that changes 0-9 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

markdown image links are wrong if repo is called 'src'
6 participants