Skip to content

Only turn links to current instance into hash links#36237

Merged
silverwind merged 13 commits intogo-gitea:mainfrom
silverwind:hashlinks
Feb 16, 2026
Merged

Only turn links to current instance into hash links#36237
silverwind merged 13 commits intogo-gitea:mainfrom
silverwind:hashlinks

Conversation

@silverwind
Copy link
Member

@silverwind silverwind commented Dec 23, 2025

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:

Screenshot 2025-12-23 at 19 19 13

After this change, only links to the current instance, as identified by setting.AppURL are turned into hash links:

Screenshot 2025-12-23 at 19 18 56

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.

@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Dec 23, 2025
@silverwind silverwind marked this pull request as draft December 23, 2025 18:24
@github-actions github-actions bot added the modifies/go Pull requests that update Go code label Dec 23, 2025
Comment on lines +106 to +107
// only turn commit links to the current instance into hash link
if !strings.HasPrefix(strings.ToLower(ret.FullURL), strings.ToLower(setting.AppURL)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Multidomain support #8697

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So what do we check instead of setting.AppURL? Is there a slice of "current app urls" somewhere?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no "current app urls" at the moment.

Copy link
Member Author

@silverwind silverwind Dec 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could make server.ROOT_URL accept multiple URLs comma-separated, and then check against all of those.

Copy link
Member Author

@silverwind silverwind Dec 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Member Author

@silverwind silverwind Dec 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this can be used:

func GuessCurrentAppURL(ctx context.Context) string {

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now it's using IsCurrentGiteaSiteURL

silverwind and others added 2 commits February 12, 2026 19:25
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>
@silverwind silverwind marked this pull request as ready for review February 12, 2026 18:29
Copilot AI review requested due to automatic review settings February 12, 2026 18:29
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

silverwind and others added 3 commits February 12, 2026 19:52
…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>
@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 12, 2026
@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 16, 2026
@silverwind silverwind added the reviewed/wait-merge This pull request is part of the merge queue. It will be merged soon. label Feb 16, 2026
@silverwind silverwind enabled auto-merge (squash) February 16, 2026 09:38
@silverwind silverwind merged commit 0e99932 into go-gitea:main Feb 16, 2026
24 checks passed
@GiteaBot GiteaBot added this to the 1.26.0 milestone Feb 16, 2026
@GiteaBot GiteaBot removed the reviewed/wait-merge This pull request is part of the merge queue. It will be merged soon. label Feb 16, 2026
@wxiaoguang wxiaoguang deleted the hashlinks branch February 16, 2026 10:59
silverwind added a commit to Excellencedev/gitea that referenced this pull request Feb 17, 2026
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. modifies/go Pull requests that update Go code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants