Refactor markup render to fix various path problems (#34114)

* Fix #33972
    * Use consistent path resolving for links and medias.
* No need to make the markup renders to resolve the paths, instead, the
paths are all correctly resolved in the "post process" step.
* Fix #33274
* Since 1.23, all paths starting with "/" are relative to current render
context (for example: the current repo branch)
* Introduce `/:root/path-relative-to-root`, then the path will be
rendered as relative to "ROOT_URL"
This commit is contained in:
wxiaoguang
2025-04-04 23:45:23 +08:00
committed by GitHub
parent e8b54d9e44
commit 6cee3bfa96
28 changed files with 239 additions and 286 deletions

View File

@ -10,13 +10,11 @@ import (
"code.gitea.io/gitea/modules/setting"
)
type LinkType string
const (
LinkTypeApp LinkType = "app" // the link is relative to the AppSubURL
LinkTypeDefault LinkType = "default" // the link is relative to the default base (eg: repo link, or current ref tree path)
LinkTypeMedia LinkType = "media" // the link should be used to access media files (images, videos)
LinkTypeRaw LinkType = "raw" // not really useful, mainly for environment GITEA_PREFIX_RAW for external renders
LinkTypeDefault = ""
LinkTypeRoot = "/:root" // the link is relative to the AppSubURL(ROOT_URL)
LinkTypeMedia = "/:media" // the link should be used to access media files (images, videos)
LinkTypeRaw = "/:raw" // not really useful, mainly for environment GITEA_PREFIX_RAW for external renders
)
type RenderHelper interface {
@ -27,7 +25,7 @@ type RenderHelper interface {
// but not make processors to guess "is it rendering a comment or a wiki?" or "does it need to check commit ID?"
IsCommitIDExisting(commitID string) bool
ResolveLink(link string, likeType LinkType) string
ResolveLink(link, preferLinkType string) string
}
// RenderHelperFuncs is used to decouple cycle-import
@ -51,7 +49,8 @@ func (r *SimpleRenderHelper) IsCommitIDExisting(commitID string) bool {
return false
}
func (r *SimpleRenderHelper) ResolveLink(link string, likeType LinkType) string {
func (r *SimpleRenderHelper) ResolveLink(link, preferLinkType string) string {
_, link = ParseRenderedLink(link, preferLinkType)
return resolveLinkRelative(context.Background(), setting.AppSubURL+"/", "", link, false)
}