Skip to content

Refactor render system #32492

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions models/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,6 @@ func (repo *Repository) ComposeMetas(ctx context.Context) map[string]string {
metas := map[string]string{
"user": repo.OwnerName,
"repo": repo.Name,
"mode": "comment",
}

unit, err := repo.GetUnit(ctx, unit.TypeExternalTracker)
Expand Down Expand Up @@ -521,7 +520,6 @@ func (repo *Repository) ComposeDocumentMetas(ctx context.Context) map[string]str
for k, v := range repo.ComposeMetas(ctx) {
metas[k] = v
}
metas["mode"] = "document"
repo.DocumentRenderingMetas = metas
}
return repo.DocumentRenderingMetas
Expand Down
23 changes: 1 addition & 22 deletions modules/markup/console/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"io"
"path/filepath"
"regexp"
"strings"

"code.gitea.io/gitea/modules/markup"
"code.gitea.io/gitea/modules/setting"
Expand All @@ -17,9 +16,6 @@ import (
"github.com/go-enry/go-enry/v2"
)

// MarkupName describes markup's name
var MarkupName = "console"

func init() {
markup.RegisterRenderer(Renderer{})
}
Expand All @@ -29,7 +25,7 @@ type Renderer struct{}

// Name implements markup.Renderer
func (Renderer) Name() string {
return MarkupName
return "console"
}

// Extensions implements markup.Renderer
Expand Down Expand Up @@ -67,20 +63,3 @@ func (Renderer) Render(ctx *markup.RenderContext, input io.Reader, output io.Wri
_, err = output.Write(buf)
return err
}

// Render renders terminal colors to HTML with all specific handling stuff.
func Render(ctx *markup.RenderContext, input io.Reader, output io.Writer) error {
if ctx.Type == "" {
ctx.Type = MarkupName
}
return markup.Render(ctx, input, output)
}

// RenderString renders terminal colors in string to HTML with all specific handling stuff and return string
func RenderString(ctx *markup.RenderContext, content string) (string, error) {
var buf strings.Builder
if err := Render(ctx, strings.NewReader(content), &buf); err != nil {
return "", err
}
return buf.String(), nil
}
5 changes: 3 additions & 2 deletions modules/markup/html_codepreview_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/markup"
"code.gitea.io/gitea/modules/markup/markdown"

"github.com/stretchr/testify/assert"
)
Expand All @@ -23,8 +24,8 @@ func TestRenderCodePreview(t *testing.T) {
})
test := func(input, expected string) {
buffer, err := markup.RenderString(&markup.RenderContext{
Ctx: git.DefaultContext,
Type: "markdown",
Ctx: git.DefaultContext,
MarkupType: markdown.MarkupName,
}, input)
assert.NoError(t, err)
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
Expand Down
15 changes: 8 additions & 7 deletions modules/markup/html_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,9 @@ func TestRender_IssueIndexPattern2(t *testing.T) {
}
expectedNil := fmt.Sprintf(expectedFmt, links...)
testRenderIssueIndexPattern(t, s, expectedNil, &RenderContext{
Ctx: git.DefaultContext,
Metas: localMetas,
Ctx: git.DefaultContext,
Metas: localMetas,
ContentMode: RenderContentAsComment,
})

class := "ref-issue"
Expand All @@ -137,8 +138,9 @@ func TestRender_IssueIndexPattern2(t *testing.T) {
}
expectedNum := fmt.Sprintf(expectedFmt, links...)
testRenderIssueIndexPattern(t, s, expectedNum, &RenderContext{
Ctx: git.DefaultContext,
Metas: numericMetas,
Ctx: git.DefaultContext,
Metas: numericMetas,
ContentMode: RenderContentAsComment,
})
}

Expand Down Expand Up @@ -266,7 +268,6 @@ func TestRender_IssueIndexPattern_Document(t *testing.T) {
"user": "someUser",
"repo": "someRepo",
"style": IssueNameStyleNumeric,
"mode": "document",
}

testRenderIssueIndexPattern(t, "#1", "#1", &RenderContext{
Expand Down Expand Up @@ -316,8 +317,8 @@ func TestRender_AutoLink(t *testing.T) {
Links: Links{
Base: TestRepoURL,
},
Metas: localMetas,
IsWiki: true,
Metas: localMetas,
ContentMode: RenderContentAsWiki,
}, strings.NewReader(input), &buffer)
assert.Equal(t, err, nil)
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer.String()))
Expand Down
5 changes: 2 additions & 3 deletions modules/markup/html_issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,8 @@ func issueIndexPatternProcessor(ctx *RenderContext, node *html.Node) {
return
}

// FIXME: the use of "mode" is quite dirty and hacky, for example: what is a "document"? how should it be rendered?
// The "mode" approach should be refactored to some other more clear&reliable way.
crossLinkOnly := ctx.Metas["mode"] == "document" && !ctx.IsWiki
// crossLinkOnly if not comment and not wiki
crossLinkOnly := ctx.ContentMode != RenderContentAsTitle && ctx.ContentMode != RenderContentAsComment && ctx.ContentMode != RenderContentAsWiki

var (
found bool
Expand Down
4 changes: 2 additions & 2 deletions modules/markup/html_link.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func ResolveLink(ctx *RenderContext, link, userContentAnchorPrefix string) (resu
isAnchorFragment := link != "" && link[0] == '#'
if !isAnchorFragment && !IsFullURLString(link) {
linkBase := ctx.Links.Base
if ctx.IsWiki {
if ctx.ContentMode == RenderContentAsWiki {
// no need to check if the link should be resolved as a wiki link or a wiki raw link
// just use wiki link here and it will be redirected to a wiki raw link if necessary
linkBase = ctx.Links.WikiLink()
Expand Down Expand Up @@ -147,7 +147,7 @@ func shortLinkProcessor(ctx *RenderContext, node *html.Node) {
}
if image {
if !absoluteLink {
link = util.URLJoin(ctx.Links.ResolveMediaLink(ctx.IsWiki), link)
link = util.URLJoin(ctx.Links.ResolveMediaLink(ctx.ContentMode == RenderContentAsWiki), link)
}
title := props["title"]
if title == "" {
Expand Down
4 changes: 2 additions & 2 deletions modules/markup/html_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func visitNodeImg(ctx *RenderContext, img *html.Node) (next *html.Node) {
}

if IsNonEmptyRelativePath(attr.Val) {
attr.Val = util.URLJoin(ctx.Links.ResolveMediaLink(ctx.IsWiki), attr.Val)
attr.Val = util.URLJoin(ctx.Links.ResolveMediaLink(ctx.ContentMode == RenderContentAsWiki), attr.Val)

// By default, the "<img>" tag should also be clickable,
// because frontend use `<img>` to paste the re-scaled image into the markdown,
Expand Down Expand Up @@ -53,7 +53,7 @@ func visitNodeVideo(ctx *RenderContext, node *html.Node) (next *html.Node) {
continue
}
if IsNonEmptyRelativePath(attr.Val) {
attr.Val = util.URLJoin(ctx.Links.ResolveMediaLink(ctx.IsWiki), attr.Val)
attr.Val = util.URLJoin(ctx.Links.ResolveMediaLink(ctx.ContentMode == RenderContentAsWiki), attr.Val)
}
attr.Val = camoHandleLink(attr.Val)
node.Attr[i] = attr
Expand Down
21 changes: 10 additions & 11 deletions modules/markup/html_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,10 +302,10 @@ func TestRender_email(t *testing.T) {
[email protected];
[email protected]?
[email protected]!`,
`<p><a href="mailto:[email protected]" rel="nofollow">[email protected]</a>,<br/>
<a href="mailto:[email protected]" rel="nofollow">[email protected]</a>.<br/>
<a href="mailto:[email protected]" rel="nofollow">[email protected]</a>;<br/>
<a href="mailto:[email protected]" rel="nofollow">[email protected]</a>?<br/>
`<p><a href="mailto:[email protected]" rel="nofollow">[email protected]</a>,
<a href="mailto:[email protected]" rel="nofollow">[email protected]</a>.
<a href="mailto:[email protected]" rel="nofollow">[email protected]</a>;
<a href="mailto:[email protected]" rel="nofollow">[email protected]</a>?
<a href="mailto:[email protected]" rel="nofollow">[email protected]</a>!</p>`)

// Test that should *not* be turned into email links
Expand Down Expand Up @@ -418,8 +418,8 @@ func TestRender_ShortLinks(t *testing.T) {
Links: markup.Links{
Base: markup.TestRepoURL,
},
Metas: localMetas,
IsWiki: true,
Metas: localMetas,
ContentMode: markup.RenderContentAsWiki,
}, input)
assert.NoError(t, err)
assert.Equal(t, strings.TrimSpace(expectedWiki), strings.TrimSpace(string(buffer)))
Expand Down Expand Up @@ -531,10 +531,10 @@ func TestRender_ShortLinks(t *testing.T) {
func TestRender_RelativeMedias(t *testing.T) {
render := func(input string, isWiki bool, links markup.Links) string {
buffer, err := markdown.RenderString(&markup.RenderContext{
Ctx: git.DefaultContext,
Links: links,
Metas: localMetas,
IsWiki: isWiki,
Ctx: git.DefaultContext,
Links: links,
Metas: localMetas,
ContentMode: util.Iif(isWiki, markup.RenderContentAsWiki, markup.RenderContentAsComment),
}, input)
assert.NoError(t, err)
return strings.TrimSpace(string(buffer))
Expand Down Expand Up @@ -608,7 +608,6 @@ func TestPostProcess_RenderDocument(t *testing.T) {
localMetas := map[string]string{
"user": "go-gitea",
"repo": "gitea",
"mode": "document",
}

test := func(input, expected string) {
Expand Down
7 changes: 6 additions & 1 deletion modules/markup/markdown/goldmark.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,12 @@ func (g *ASTTransformer) Transform(node *ast.Document, reader text.Reader, pc pa
g.transformList(ctx, v, rc)
case *ast.Text:
if v.SoftLineBreak() && !v.HardLineBreak() {
if ctx.Metas["mode"] != "document" {
// TODO: this was a quite unclear part
// many places render non-comment contents with no mode=document, then these contents also use comment's hard line break setting
// especially in many tests.
if ctx.UseHardLineBreak.Has() {
v.SetHardLineBreak(ctx.UseHardLineBreak.Value())
} else if ctx.ContentMode == markup.RenderContentAsComment {
v.SetHardLineBreak(setting.Markdown.EnableHardLineBreakInComments)
} else {
v.SetHardLineBreak(setting.Markdown.EnableHardLineBreakInDocuments)
Expand Down
4 changes: 1 addition & 3 deletions modules/markup/markdown/markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,9 +257,7 @@ func (Renderer) Render(ctx *markup.RenderContext, input io.Reader, output io.Wri

// Render renders Markdown to HTML with all specific handling stuff.
func Render(ctx *markup.RenderContext, input io.Reader, output io.Writer) error {
if ctx.Type == "" {
ctx.Type = MarkupName
}
ctx.MarkupType = MarkupName
return markup.Render(ctx, input, output)
}

Expand Down
26 changes: 17 additions & 9 deletions modules/markup/markdown/markdown_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/markup"
"code.gitea.io/gitea/modules/markup/markdown"
"code.gitea.io/gitea/modules/optional"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/svg"
"code.gitea.io/gitea/modules/util"
Expand Down Expand Up @@ -74,7 +75,7 @@ func TestRender_StandardLinks(t *testing.T) {
Links: markup.Links{
Base: FullURL,
},
IsWiki: true,
ContentMode: markup.RenderContentAsWiki,
}, input)
assert.NoError(t, err)
assert.Equal(t, strings.TrimSpace(expectedWiki), strings.TrimSpace(string(buffer)))
Expand Down Expand Up @@ -306,9 +307,10 @@ func TestTotal_RenderWiki(t *testing.T) {
Links: markup.Links{
Base: FullURL,
},
Repo: newMockRepo(testRepoOwnerName, testRepoName),
Metas: localMetas,
IsWiki: true,
Repo: newMockRepo(testRepoOwnerName, testRepoName),
Metas: localMetas,
ContentMode: markup.RenderContentAsWiki,
UseHardLineBreak: optional.Some(true),
}, sameCases[i])
assert.NoError(t, err)
actual := strings.ReplaceAll(string(line), ` data-markdown-generated-content=""`, "")
Expand All @@ -334,7 +336,7 @@ func TestTotal_RenderWiki(t *testing.T) {
Links: markup.Links{
Base: FullURL,
},
IsWiki: true,
ContentMode: markup.RenderContentAsWiki,
}, testCases[i])
assert.NoError(t, err)
actual := strings.ReplaceAll(string(line), ` data-markdown-generated-content=""`, "")
Expand All @@ -354,8 +356,9 @@ func TestTotal_RenderString(t *testing.T) {
Base: FullURL,
BranchPath: "master",
},
Repo: newMockRepo(testRepoOwnerName, testRepoName),
Metas: localMetas,
Repo: newMockRepo(testRepoOwnerName, testRepoName),
Metas: localMetas,
UseHardLineBreak: optional.Some(true),
}, sameCases[i])
assert.NoError(t, err)
actual := strings.ReplaceAll(string(line), ` data-markdown-generated-content=""`, "")
Expand Down Expand Up @@ -428,7 +431,7 @@ func TestRenderSiblingImages_Issue12925(t *testing.T) {
expected := `<p><a href="/image1" target="_blank" rel="nofollow noopener"><img src="/image1" alt="image1"></a><br>
<a href="/image2" target="_blank" rel="nofollow noopener"><img src="/image2" alt="image2"></a></p>
`
res, err := markdown.RenderRawString(&markup.RenderContext{Ctx: git.DefaultContext}, testcase)
res, err := markdown.RenderRawString(&markup.RenderContext{Ctx: git.DefaultContext, UseHardLineBreak: optional.Some(true)}, testcase)
assert.NoError(t, err)
assert.Equal(t, expected, res)
}
Expand Down Expand Up @@ -997,7 +1000,12 @@ space</p>
}

for i, c := range cases {
result, err := markdown.RenderString(&markup.RenderContext{Ctx: context.Background(), Links: c.Links, IsWiki: c.IsWiki}, input)
result, err := markdown.RenderString(&markup.RenderContext{
Ctx: context.Background(),
Links: c.Links,
ContentMode: util.Iif(c.IsWiki, markup.RenderContentAsWiki, markup.RenderContentAsDefault),
UseHardLineBreak: optional.Some(true),
}, input)
assert.NoError(t, err, "Unexpected error in testcase: %v", i)
actual := strings.ReplaceAll(string(result), ` data-markdown-generated-content=""`, "")
assert.Equal(t, c.Expected, actual, "Unexpected result in testcase %v", i)
Expand Down
2 changes: 1 addition & 1 deletion modules/markup/markdown/transform_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (g *ASTTransformer) transformImage(ctx *markup.RenderContext, v *ast.Image)
// Check if the destination is a real link
if len(v.Destination) > 0 && !markup.IsFullURLBytes(v.Destination) {
v.Destination = []byte(giteautil.URLJoin(
ctx.Links.ResolveMediaLink(ctx.IsWiki),
ctx.Links.ResolveMediaLink(ctx.ContentMode == markup.RenderContentAsWiki),
strings.TrimLeft(string(v.Destination), "/"),
))
}
Expand Down
5 changes: 3 additions & 2 deletions modules/markup/orgmode/orgmode.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,15 @@ func (r *Writer) resolveLink(kind, link string) string {
}

base := r.Ctx.Links.Base
if r.Ctx.IsWiki {
isWiki := r.Ctx.ContentMode == markup.RenderContentAsWiki
if isWiki {
base = r.Ctx.Links.WikiLink()
} else if r.Ctx.Links.HasBranchInfo() {
base = r.Ctx.Links.SrcLink()
}

if kind == "image" || kind == "video" {
base = r.Ctx.Links.ResolveMediaLink(r.Ctx.IsWiki)
base = r.Ctx.Links.ResolveMediaLink(isWiki)
}

link = util.URLJoin(base, link)
Expand Down
3 changes: 2 additions & 1 deletion modules/markup/orgmode/orgmode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/markup"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"

"github.com/stretchr/testify/assert"
)
Expand All @@ -26,7 +27,7 @@ func TestRender_StandardLinks(t *testing.T) {
Base: "/relative-path",
BranchPath: "branch/main",
},
IsWiki: isWiki,
ContentMode: util.Iif(isWiki, markup.RenderContentAsWiki, markup.RenderContentAsDefault),
}, input)
assert.NoError(t, err)
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
Expand Down
Loading
Loading