Files
MokoGitea/modules/markup/render_link.go
Jonathan Miller c572fcfe04
PR RC Release / Build RC Release (pull_request) Failing after 0s
Branch Policy Check / Verify merge target (pull_request) Failing after 0s
chore(core): rename Go module from code.gitea.io/gitea to MokoGitea namespace
Rename the Go module path from code.gitea.io/gitea to
git.mokoconsulting.tech/MokoConsulting/MokoGitea across the entire
codebase.

Scope:
- go.mod module declaration
- 2,235 Go source files (import paths)
- Dockerfile WORKDIR and COPY paths
- Swagger API templates
- golangci.yml linter config

External dependencies (code.gitea.io/gitea-vet, code.gitea.io/sdk/gitea,
gitea.com/gitea/act, etc.) are intentionally NOT renamed — they are
separate upstream modules.

Closes #132

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-05-25 00:22:38 -05:00

76 lines
2.3 KiB
Go

// Copyright 2024 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package markup
import (
"context"
"net/url"
"path"
"strings"
"git.mokoconsulting.tech/MokoConsulting/MokoGitea/modules/httplib"
"git.mokoconsulting.tech/MokoConsulting/MokoGitea/modules/setting"
)
// resolveLinkRelative tries to resolve the link relative to the "{base}/{cur}", and returns the final link.
// It only resolves the link, doesn't do any sanitization or validation, invalid links will be returned as is.
func resolveLinkRelative(ctx context.Context, base, cur, link string, absolute bool) (finalLink string) {
linkURL, err := url.Parse(link)
if err != nil {
return link // invalid URL, return as is
}
if linkURL.Scheme != "" || linkURL.Host != "" {
return link // absolute URL, return as is
}
if strings.HasPrefix(link, "/") {
if strings.HasPrefix(link, base) && strings.Count(base, "/") >= 4 {
// a trick to tolerate that some users were using absolute paths (the old Gitea's behavior)
// if the link is likely "{base}/src/main" while "{base}" is something like "/owner/repo"
finalLink = link
} else {
// need to resolve the link relative to "{base}"
cur = ""
}
} // else: link is relative to "{base}/{cur}"
if finalLink == "" {
finalLink = strings.TrimSuffix(base, "/") + path.Join("/"+cur, "/"+linkURL.EscapedPath())
finalLink = strings.TrimSuffix(finalLink, "/")
if linkURL.RawQuery != "" {
finalLink += "?" + linkURL.RawQuery
}
if linkURL.Fragment != "" {
finalLink += "#" + linkURL.Fragment
}
}
if absolute {
finalLink = httplib.MakeAbsoluteURL(ctx, finalLink)
}
return finalLink
}
func (ctx *RenderContext) ResolveLinkRelative(base, cur, link string) string {
if strings.HasPrefix(link, "/:") {
setting.PanicInDevOrTesting("invalid link %q, forgot to cut?", link)
}
return resolveLinkRelative(ctx, base, cur, link, ctx.RenderOptions.UseAbsoluteLink)
}
func (ctx *RenderContext) ResolveLinkRoot(link string) string {
return ctx.ResolveLinkRelative(setting.AppSubURL+"/", "", link)
}
func ParseRenderedLink(s, preferLinkType string) (linkType, link string) {
if strings.HasPrefix(s, "/:") {
p := strings.IndexByte(s[1:], '/')
if p == -1 {
return s, ""
}
return s[:p+1], s[p+2:]
}
return preferLinkType, s
}