Only turn links to current instance into hash links#36237
Only turn links to current instance into hash links#36237silverwind merged 13 commits intogo-gitea:mainfrom
Conversation
modules/markup/html_commit.go
Outdated
| // only turn commit links to the current instance into hash link | ||
| if !strings.HasPrefix(strings.ToLower(ret.FullURL), strings.ToLower(setting.AppURL)) { |
There was a problem hiding this comment.
So what do we check instead of setting.AppURL? Is there a slice of "current app urls" somewhere?
There was a problem hiding this comment.
There is no "current app urls" at the moment.
There was a problem hiding this comment.
We could make server.ROOT_URL accept multiple URLs comma-separated, and then check against all of those.
There was a problem hiding this comment.
Or is there a way to obtain the current URL/Origin in this function? Do we need to pass *http.Request or some context down all the way to this function?
There was a problem hiding this comment.
I guess the only way to make it work based on request URL is to determine the current HTTP request origin (from HTTP headers Host, X-Forwarded-Host, X-Forwarded-Protocol and more) into markup.RenderContext, which will be quite complicated.
It'll certainly be easier to let the user configure multiple server.ROOT_URL.
There was a problem hiding this comment.
Now it's using IsCurrentGiteaSiteURL
Use the shared IsCurrentGiteaSiteURL helper instead of a manual string prefix check against setting.AppURL, enabling multidomain support and consistency with other markup processors. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
This PR updates markup post-processing so that commit-ish URLs are only rewritten into shortened “hash link” renderings when the URL points to the current Gitea instance (as identified via setting.AppURL), instead of rewriting external hosts too.
Changes:
- Gate commit URL “hash link” rewriting behind
httplib.IsCurrentGiteaSiteURL. - Update/add tests to ensure external commit URLs remain plain links while current-instance URLs still shorten.
- Adjust markdown link rendering tests to use localhost URLs.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| modules/markup/html_commit.go | Adds current-instance check before rewriting full-hash URLs into shortened commit links. |
| modules/markup/markdown/markdown_test.go | Updates link rendering expectations and inputs (now using localhost URLs). |
| modules/markup/html_test.go | Adds coverage for external vs current-instance commit URL rendering behavior. |
| modules/markup/html_internal_test.go | Updates expected output so external commit URLs remain unshortened. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…tests - Add IsCurrentGiteaSiteURL check to comparePatternProcessor so external compare URLs are no longer shortened into hash links, matching the existing behavior in fullHashPatternProcessor - Use MockVariableValue for setting.AppURL in tests to avoid leaking global state between tests - Update test expectations for external URLs that should no longer be shortened - Add test cases for external commit and compare URLs in markdown tests Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Only the commit and compare URLs need to use localhost:3000 to match AppURL for the IsCurrentGiteaSiteURL check. Other URLs (plain links, images, remote links) are unaffected by the hash-link shortening and don't need to change. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Use setting.AppURL prefix instead of hardcoded external URLs in tests that verify hash link shortening. This keeps expected output identical to main where possible, and removes redundant external URL test cases that are already covered elsewhere. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Given the following markdown: ``` http://localhost:3500/silverwind/symlink-test/commit/a832c723cd116df44cce6271c4a89afa4d8ec670 http://localhost:3500/silverwind/remap-css/commit/19fe6cdf81f7ec50b8cac2d6c28fe7c42c1ffe14 https://github.com/silverwind/symlink-test/commit/a832c723cd116df44cce6271c4a89afa4d8ec670 ``` Previously, all links would turn into hash link, even ones to external sites: <img width="849" height="89" alt="Screenshot 2025-12-23 at 19 19 13" src="https://github.com/user-attachments/assets/2ad35a18-4542-40a4-a838-7ab8ac8bc047" /> After this change, only links to the current instance, as identified by `setting.AppURL` are turned into hash links: <img width="850" height="87" alt="Screenshot 2025-12-23 at 19 18 56" src="https://github.com/user-attachments/assets/2c49a5b2-426c-4a82-a610-9b9da8dcfff9" /> There is still one notable [difference with GitHub](silverwind/symlink-test#20 (comment)) where the second link should render like `user/repo@<hash>`, not `<hash>` as currently, I would like to fix that here as well. --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
Given the following markdown:
Previously, all links would turn into hash link, even ones to external sites:
After this change, only links to the current instance, as identified by
setting.AppURLare turned into hash links:There is still one notable difference with GitHub where the second link should render like
user/repo@<hash>, not<hash>as currently, I would like to fix that here as well.