1
1
mirror of https://github.com/go-gitea/gitea synced 2025-07-09 20:17:21 +00:00

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

@ -1,7 +1,7 @@
// Copyright 2017 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package markup
package orgmode
import (
"fmt"
@ -125,27 +125,13 @@ type orgWriter struct {
var _ org.Writer = (*orgWriter)(nil)
func (r *orgWriter) resolveLink(kind, link string) string {
link = strings.TrimPrefix(link, "file:")
if !strings.HasPrefix(link, "#") && // not a URL fragment
!markup.IsFullURLString(link) {
if kind == "regular" {
// orgmode reports the link kind as "regular" for "[[ImageLink.svg][The Image Desc]]"
// so we need to try to guess the link kind again here
kind = org.RegularLink{URL: link}.Kind()
}
if kind == "image" || kind == "video" {
link = r.rctx.RenderHelper.ResolveLink(link, markup.LinkTypeMedia)
} else {
link = r.rctx.RenderHelper.ResolveLink(link, markup.LinkTypeDefault)
}
}
return link
func (r *orgWriter) resolveLink(link string) string {
return strings.TrimPrefix(link, "file:")
}
// WriteRegularLink renders images, links or videos
func (r *orgWriter) WriteRegularLink(l org.RegularLink) {
link := r.resolveLink(l.Kind(), l.URL)
link := r.resolveLink(l.URL)
printHTML := func(html template.HTML, a ...any) {
_, _ = fmt.Fprint(r, htmlutil.HTMLFormat(html, a...))
@ -156,14 +142,14 @@ func (r *orgWriter) WriteRegularLink(l org.RegularLink) {
if l.Description == nil {
printHTML(`<img src="%s" alt="%s">`, link, link)
} else {
imageSrc := r.resolveLink(l.Kind(), org.String(l.Description...))
imageSrc := r.resolveLink(org.String(l.Description...))
printHTML(`<a href="%s"><img src="%s" alt="%s"></a>`, link, imageSrc, imageSrc)
}
case "video":
if l.Description == nil {
printHTML(`<video src="%s">%s</video>`, link, link)
} else {
videoSrc := r.resolveLink(l.Kind(), org.String(l.Description...))
videoSrc := r.resolveLink(org.String(l.Description...))
printHTML(`<a href="%s"><video src="%s">%s</video></a>`, link, videoSrc, videoSrc)
}
default:

View File

@ -1,7 +1,7 @@
// Copyright 2017 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package markup
package orgmode_test
import (
"os"
@ -9,6 +9,7 @@ import (
"testing"
"code.gitea.io/gitea/modules/markup"
"code.gitea.io/gitea/modules/markup/orgmode"
"code.gitea.io/gitea/modules/setting"
"github.com/stretchr/testify/assert"
@ -22,7 +23,7 @@ func TestMain(m *testing.M) {
func TestRender_StandardLinks(t *testing.T) {
test := func(input, expected string) {
buffer, err := RenderString(markup.NewTestRenderContext("/relative-path/media/branch/main/"), input)
buffer, err := orgmode.RenderString(markup.NewTestRenderContext(), input)
assert.NoError(t, err)
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
}
@ -30,37 +31,37 @@ func TestRender_StandardLinks(t *testing.T) {
test("[[https://google.com/]]",
`<p><a href="https://google.com/">https://google.com/</a></p>`)
test("[[ImageLink.svg][The Image Desc]]",
`<p><a href="/relative-path/media/branch/main/ImageLink.svg">The Image Desc</a></p>`)
`<p><a href="ImageLink.svg">The Image Desc</a></p>`)
}
func TestRender_InternalLinks(t *testing.T) {
test := func(input, expected string) {
buffer, err := RenderString(markup.NewTestRenderContext("/relative-path/src/branch/main"), input)
buffer, err := orgmode.RenderString(markup.NewTestRenderContext(), input)
assert.NoError(t, err)
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
}
test("[[file:test.org][Test]]",
`<p><a href="/relative-path/src/branch/main/test.org">Test</a></p>`)
`<p><a href="test.org">Test</a></p>`)
test("[[./test.org][Test]]",
`<p><a href="/relative-path/src/branch/main/test.org">Test</a></p>`)
`<p><a href="./test.org">Test</a></p>`)
test("[[test.org][Test]]",
`<p><a href="/relative-path/src/branch/main/test.org">Test</a></p>`)
`<p><a href="test.org">Test</a></p>`)
test("[[path/to/test.org][Test]]",
`<p><a href="/relative-path/src/branch/main/path/to/test.org">Test</a></p>`)
`<p><a href="path/to/test.org">Test</a></p>`)
}
func TestRender_Media(t *testing.T) {
test := func(input, expected string) {
buffer, err := RenderString(markup.NewTestRenderContext("./relative-path"), input)
buffer, err := orgmode.RenderString(markup.NewTestRenderContext(), input)
assert.NoError(t, err)
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
}
test("[[file:../../.images/src/02/train.jpg]]",
`<p><img src=".images/src/02/train.jpg" alt=".images/src/02/train.jpg"></p>`)
`<p><img src="../../.images/src/02/train.jpg" alt="../../.images/src/02/train.jpg"></p>`)
test("[[file:train.jpg]]",
`<p><img src="relative-path/train.jpg" alt="relative-path/train.jpg"></p>`)
`<p><img src="train.jpg" alt="train.jpg"></p>`)
// With description.
test("[[https://example.com][https://example.com/example.svg]]",
@ -91,7 +92,7 @@ func TestRender_Media(t *testing.T) {
func TestRender_Source(t *testing.T) {
test := func(input, expected string) {
buffer, err := RenderString(markup.NewTestRenderContext(), input)
buffer, err := orgmode.RenderString(markup.NewTestRenderContext(), input)
assert.NoError(t, err)
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
}