Skip to content

Commit 94fd668

Browse files
committed
Revert "Refactor render system (go-gitea#32492)"
This reverts commit 3f9c3e7.
1 parent 18e8117 commit 94fd668

32 files changed

+257
-237
lines changed

models/repo/repo.go

+2
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,7 @@ func (repo *Repository) ComposeMetas(ctx context.Context) map[string]string {
479479
metas := map[string]string{
480480
"user": repo.OwnerName,
481481
"repo": repo.Name,
482+
"mode": "comment",
482483
}
483484

484485
unit, err := repo.GetUnit(ctx, unit.TypeExternalTracker)
@@ -520,6 +521,7 @@ func (repo *Repository) ComposeDocumentMetas(ctx context.Context) map[string]str
520521
for k, v := range repo.ComposeMetas(ctx) {
521522
metas[k] = v
522523
}
524+
metas["mode"] = "document"
523525
repo.DocumentRenderingMetas = metas
524526
}
525527
return repo.DocumentRenderingMetas

modules/markup/console/console.go

+22-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"io"
99
"path/filepath"
1010
"regexp"
11+
"strings"
1112

1213
"code.gitea.io/gitea/modules/markup"
1314
"code.gitea.io/gitea/modules/setting"
@@ -16,6 +17,9 @@ import (
1617
"github.com/go-enry/go-enry/v2"
1718
)
1819

20+
// MarkupName describes markup's name
21+
var MarkupName = "console"
22+
1923
func init() {
2024
markup.RegisterRenderer(Renderer{})
2125
}
@@ -25,7 +29,7 @@ type Renderer struct{}
2529

2630
// Name implements markup.Renderer
2731
func (Renderer) Name() string {
28-
return "console"
32+
return MarkupName
2933
}
3034

3135
// Extensions implements markup.Renderer
@@ -63,3 +67,20 @@ func (Renderer) Render(ctx *markup.RenderContext, input io.Reader, output io.Wri
6367
_, err = output.Write(buf)
6468
return err
6569
}
70+
71+
// Render renders terminal colors to HTML with all specific handling stuff.
72+
func Render(ctx *markup.RenderContext, input io.Reader, output io.Writer) error {
73+
if ctx.Type == "" {
74+
ctx.Type = MarkupName
75+
}
76+
return markup.Render(ctx, input, output)
77+
}
78+
79+
// RenderString renders terminal colors in string to HTML with all specific handling stuff and return string
80+
func RenderString(ctx *markup.RenderContext, content string) (string, error) {
81+
var buf strings.Builder
82+
if err := Render(ctx, strings.NewReader(content), &buf); err != nil {
83+
return "", err
84+
}
85+
return buf.String(), nil
86+
}

modules/markup/html.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -442,11 +442,12 @@ func createLink(href, content, class string) *html.Node {
442442
a := &html.Node{
443443
Type: html.ElementNode,
444444
Data: atom.A.String(),
445-
Attr: []html.Attribute{{Key: "href", Val: href}},
446-
}
447-
if !RenderBehaviorForTesting.DisableInternalAttributes {
448-
a.Attr = append(a.Attr, html.Attribute{Key: "data-markdown-generated-content"})
445+
Attr: []html.Attribute{
446+
{Key: "href", Val: href},
447+
{Key: "data-markdown-generated-content"},
448+
},
449449
}
450+
450451
if class != "" {
451452
a.Attr = append(a.Attr, html.Attribute{Key: "class", Val: class})
452453
}

modules/markup/html_codepreview_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111

1212
"code.gitea.io/gitea/modules/git"
1313
"code.gitea.io/gitea/modules/markup"
14-
"code.gitea.io/gitea/modules/markup/markdown"
1514

1615
"github.com/stretchr/testify/assert"
1716
)
@@ -24,8 +23,8 @@ func TestRenderCodePreview(t *testing.T) {
2423
})
2524
test := func(input, expected string) {
2625
buffer, err := markup.RenderString(&markup.RenderContext{
27-
Ctx: git.DefaultContext,
28-
MarkupType: markdown.MarkupName,
26+
Ctx: git.DefaultContext,
27+
Type: "markdown",
2928
}, input)
3029
assert.NoError(t, err)
3130
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))

modules/markup/html_internal_test.go

+11-11
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111

1212
"code.gitea.io/gitea/modules/git"
1313
"code.gitea.io/gitea/modules/setting"
14-
testModule "code.gitea.io/gitea/modules/test"
1514
"code.gitea.io/gitea/modules/util"
1615

1716
"github.com/stretchr/testify/assert"
@@ -124,9 +123,8 @@ func TestRender_IssueIndexPattern2(t *testing.T) {
124123
}
125124
expectedNil := fmt.Sprintf(expectedFmt, links...)
126125
testRenderIssueIndexPattern(t, s, expectedNil, &RenderContext{
127-
Ctx: git.DefaultContext,
128-
Metas: localMetas,
129-
ContentMode: RenderContentAsComment,
126+
Ctx: git.DefaultContext,
127+
Metas: localMetas,
130128
})
131129

132130
class := "ref-issue"
@@ -139,9 +137,8 @@ func TestRender_IssueIndexPattern2(t *testing.T) {
139137
}
140138
expectedNum := fmt.Sprintf(expectedFmt, links...)
141139
testRenderIssueIndexPattern(t, s, expectedNum, &RenderContext{
142-
Ctx: git.DefaultContext,
143-
Metas: numericMetas,
144-
ContentMode: RenderContentAsComment,
140+
Ctx: git.DefaultContext,
141+
Metas: numericMetas,
145142
})
146143
}
147144

@@ -269,6 +266,7 @@ func TestRender_IssueIndexPattern_Document(t *testing.T) {
269266
"user": "someUser",
270267
"repo": "someRepo",
271268
"style": IssueNameStyleNumeric,
269+
"mode": "document",
272270
}
273271

274272
testRenderIssueIndexPattern(t, "#1", "#1", &RenderContext{
@@ -318,8 +316,8 @@ func TestRender_AutoLink(t *testing.T) {
318316
Links: Links{
319317
Base: TestRepoURL,
320318
},
321-
Metas: localMetas,
322-
ContentMode: RenderContentAsWiki,
319+
Metas: localMetas,
320+
IsWiki: true,
323321
}, strings.NewReader(input), &buffer)
324322
assert.Equal(t, err, nil)
325323
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer.String()))
@@ -342,7 +340,7 @@ func TestRender_AutoLink(t *testing.T) {
342340

343341
func TestRender_FullIssueURLs(t *testing.T) {
344342
setting.AppURL = TestAppURL
345-
defer testModule.MockVariableValue(&RenderBehaviorForTesting.DisableInternalAttributes, true)()
343+
346344
test := func(input, expected string) {
347345
var result strings.Builder
348346
err := postProcess(&RenderContext{
@@ -353,7 +351,9 @@ func TestRender_FullIssueURLs(t *testing.T) {
353351
Metas: localMetas,
354352
}, []processor{fullIssuePatternProcessor}, strings.NewReader(input), &result)
355353
assert.NoError(t, err)
356-
assert.Equal(t, expected, result.String())
354+
actual := result.String()
355+
actual = strings.ReplaceAll(actual, ` data-markdown-generated-content=""`, "")
356+
assert.Equal(t, expected, actual)
357357
}
358358
test("Here is a link https://git.osgeo.org/gogs/postgis/postgis/pulls/6",
359359
"Here is a link https://git.osgeo.org/gogs/postgis/postgis/pulls/6")

modules/markup/html_issue.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,9 @@ func issueIndexPatternProcessor(ctx *RenderContext, node *html.Node) {
6767
return
6868
}
6969

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

7374
var (
7475
found bool

modules/markup/html_link.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func ResolveLink(ctx *RenderContext, link, userContentAnchorPrefix string) (resu
2020
isAnchorFragment := link != "" && link[0] == '#'
2121
if !isAnchorFragment && !IsFullURLString(link) {
2222
linkBase := ctx.Links.Base
23-
if ctx.ContentMode == RenderContentAsWiki {
23+
if ctx.IsWiki {
2424
// no need to check if the link should be resolved as a wiki link or a wiki raw link
2525
// just use wiki link here and it will be redirected to a wiki raw link if necessary
2626
linkBase = ctx.Links.WikiLink()
@@ -147,7 +147,7 @@ func shortLinkProcessor(ctx *RenderContext, node *html.Node) {
147147
}
148148
if image {
149149
if !absoluteLink {
150-
link = util.URLJoin(ctx.Links.ResolveMediaLink(ctx.ContentMode == RenderContentAsWiki), link)
150+
link = util.URLJoin(ctx.Links.ResolveMediaLink(ctx.IsWiki), link)
151151
}
152152
title := props["title"]
153153
if title == "" {

modules/markup/html_node.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func visitNodeImg(ctx *RenderContext, img *html.Node) (next *html.Node) {
1717
}
1818

1919
if IsNonEmptyRelativePath(attr.Val) {
20-
attr.Val = util.URLJoin(ctx.Links.ResolveMediaLink(ctx.ContentMode == RenderContentAsWiki), attr.Val)
20+
attr.Val = util.URLJoin(ctx.Links.ResolveMediaLink(ctx.IsWiki), attr.Val)
2121

2222
// By default, the "<img>" tag should also be clickable,
2323
// because frontend use `<img>` to paste the re-scaled image into the markdown,
@@ -53,7 +53,7 @@ func visitNodeVideo(ctx *RenderContext, node *html.Node) (next *html.Node) {
5353
continue
5454
}
5555
if IsNonEmptyRelativePath(attr.Val) {
56-
attr.Val = util.URLJoin(ctx.Links.ResolveMediaLink(ctx.ContentMode == RenderContentAsWiki), attr.Val)
56+
attr.Val = util.URLJoin(ctx.Links.ResolveMediaLink(ctx.IsWiki), attr.Val)
5757
}
5858
attr.Val = camoHandleLink(attr.Val)
5959
node.Attr[i] = attr

modules/markup/html_test.go

+32-20
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"code.gitea.io/gitea/modules/markup"
1515
"code.gitea.io/gitea/modules/markup/markdown"
1616
"code.gitea.io/gitea/modules/setting"
17-
testModule "code.gitea.io/gitea/modules/test"
1817
"code.gitea.io/gitea/modules/util"
1918

2019
"github.com/stretchr/testify/assert"
@@ -105,7 +104,7 @@ func TestRender_Commits(t *testing.T) {
105104

106105
func TestRender_CrossReferences(t *testing.T) {
107106
setting.AppURL = markup.TestAppURL
108-
defer testModule.MockVariableValue(&markup.RenderBehaviorForTesting.DisableInternalAttributes, true)()
107+
109108
test := func(input, expected string) {
110109
buffer, err := markup.RenderString(&markup.RenderContext{
111110
Ctx: git.DefaultContext,
@@ -117,7 +116,9 @@ func TestRender_CrossReferences(t *testing.T) {
117116
Metas: localMetas,
118117
}, input)
119118
assert.NoError(t, err)
120-
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
119+
actual := strings.TrimSpace(buffer)
120+
actual = strings.ReplaceAll(actual, ` data-markdown-generated-content=""`, "")
121+
assert.Equal(t, strings.TrimSpace(expected), actual)
121122
}
122123

123124
test(
@@ -147,7 +148,7 @@ func TestRender_CrossReferences(t *testing.T) {
147148

148149
func TestRender_links(t *testing.T) {
149150
setting.AppURL = markup.TestAppURL
150-
defer testModule.MockVariableValue(&markup.RenderBehaviorForTesting.DisableInternalAttributes, true)()
151+
151152
test := func(input, expected string) {
152153
buffer, err := markup.RenderString(&markup.RenderContext{
153154
Ctx: git.DefaultContext,
@@ -157,7 +158,9 @@ func TestRender_links(t *testing.T) {
157158
},
158159
}, input)
159160
assert.NoError(t, err)
160-
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
161+
actual := strings.TrimSpace(buffer)
162+
actual = strings.ReplaceAll(actual, ` data-markdown-generated-content=""`, "")
163+
assert.Equal(t, strings.TrimSpace(expected), actual)
161164
}
162165

163166
oldCustomURLSchemes := setting.Markdown.CustomURLSchemes
@@ -258,7 +261,7 @@ func TestRender_links(t *testing.T) {
258261

259262
func TestRender_email(t *testing.T) {
260263
setting.AppURL = markup.TestAppURL
261-
defer testModule.MockVariableValue(&markup.RenderBehaviorForTesting.DisableInternalAttributes, true)()
264+
262265
test := func(input, expected string) {
263266
res, err := markup.RenderString(&markup.RenderContext{
264267
Ctx: git.DefaultContext,
@@ -268,7 +271,9 @@ func TestRender_email(t *testing.T) {
268271
},
269272
}, input)
270273
assert.NoError(t, err)
271-
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(res))
274+
actual := strings.TrimSpace(res)
275+
actual = strings.ReplaceAll(actual, ` data-markdown-generated-content=""`, "")
276+
assert.Equal(t, strings.TrimSpace(expected), actual)
272277
}
273278
// Text that should be turned into email link
274279

@@ -297,10 +302,10 @@ func TestRender_email(t *testing.T) {
297302
j.doe@example.com;
298303
j.doe@example.com?
299304
j.doe@example.com!`,
300-
`<p><a href="mailto:j.doe@example.com" rel="nofollow">j.doe@example.com</a>,
301-
<a href="mailto:j.doe@example.com" rel="nofollow">j.doe@example.com</a>.
302-
<a href="mailto:j.doe@example.com" rel="nofollow">j.doe@example.com</a>;
303-
<a href="mailto:j.doe@example.com" rel="nofollow">j.doe@example.com</a>?
305+
`<p><a href="mailto:j.doe@example.com" rel="nofollow">j.doe@example.com</a>,<br/>
306+
<a href="mailto:j.doe@example.com" rel="nofollow">j.doe@example.com</a>.<br/>
307+
<a href="mailto:j.doe@example.com" rel="nofollow">j.doe@example.com</a>;<br/>
308+
<a href="mailto:j.doe@example.com" rel="nofollow">j.doe@example.com</a>?<br/>
304309
<a href="mailto:j.doe@example.com" rel="nofollow">j.doe@example.com</a>!</p>`)
305310

306311
// Test that should *not* be turned into email links
@@ -413,8 +418,8 @@ func TestRender_ShortLinks(t *testing.T) {
413418
Links: markup.Links{
414419
Base: markup.TestRepoURL,
415420
},
416-
Metas: localMetas,
417-
ContentMode: markup.RenderContentAsWiki,
421+
Metas: localMetas,
422+
IsWiki: true,
418423
}, input)
419424
assert.NoError(t, err)
420425
assert.Equal(t, strings.TrimSpace(expectedWiki), strings.TrimSpace(string(buffer)))
@@ -526,10 +531,10 @@ func TestRender_ShortLinks(t *testing.T) {
526531
func TestRender_RelativeMedias(t *testing.T) {
527532
render := func(input string, isWiki bool, links markup.Links) string {
528533
buffer, err := markdown.RenderString(&markup.RenderContext{
529-
Ctx: git.DefaultContext,
530-
Links: links,
531-
Metas: localMetas,
532-
ContentMode: util.Iif(isWiki, markup.RenderContentAsWiki, markup.RenderContentAsComment),
534+
Ctx: git.DefaultContext,
535+
Links: links,
536+
Metas: localMetas,
537+
IsWiki: isWiki,
533538
}, input)
534539
assert.NoError(t, err)
535540
return strings.TrimSpace(string(buffer))
@@ -599,7 +604,12 @@ func Test_ParseClusterFuzz(t *testing.T) {
599604
func TestPostProcess_RenderDocument(t *testing.T) {
600605
setting.AppURL = markup.TestAppURL
601606
setting.StaticURLPrefix = markup.TestAppURL // can't run standalone
602-
defer testModule.MockVariableValue(&markup.RenderBehaviorForTesting.DisableInternalAttributes, true)()
607+
608+
localMetas := map[string]string{
609+
"user": "go-gitea",
610+
"repo": "gitea",
611+
"mode": "document",
612+
}
603613

604614
test := func(input, expected string) {
605615
var res strings.Builder
@@ -609,10 +619,12 @@ func TestPostProcess_RenderDocument(t *testing.T) {
609619
AbsolutePrefix: true,
610620
Base: "https://example.com",
611621
},
612-
Metas: map[string]string{"user": "go-gitea", "repo": "gitea"},
622+
Metas: localMetas,
613623
}, strings.NewReader(input), &res)
614624
assert.NoError(t, err)
615-
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(res.String()))
625+
actual := strings.TrimSpace(res.String())
626+
actual = strings.ReplaceAll(actual, ` data-markdown-generated-content=""`, "")
627+
assert.Equal(t, strings.TrimSpace(expected), actual)
616628
}
617629

618630
// Issue index shouldn't be post processing in a document.

modules/markup/markdown/goldmark.go

+1-6
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,7 @@ func (g *ASTTransformer) Transform(node *ast.Document, reader text.Reader, pc pa
7272
g.transformList(ctx, v, rc)
7373
case *ast.Text:
7474
if v.SoftLineBreak() && !v.HardLineBreak() {
75-
// TODO: this was a quite unclear part, old code: `if metas["mode"] != "document" { use comment link break setting }`
76-
// many places render non-comment contents with no mode=document, then these contents also use comment's hard line break setting
77-
// especially in many tests.
78-
if markup.RenderBehaviorForTesting.ForceHardLineBreak {
79-
v.SetHardLineBreak(true)
80-
} else if ctx.ContentMode == markup.RenderContentAsComment {
75+
if ctx.Metas["mode"] != "document" {
8176
v.SetHardLineBreak(setting.Markdown.EnableHardLineBreakInComments)
8277
} else {
8378
v.SetHardLineBreak(setting.Markdown.EnableHardLineBreakInDocuments)

modules/markup/markdown/markdown.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,9 @@ func (Renderer) Render(ctx *markup.RenderContext, input io.Reader, output io.Wri
257257

258258
// Render renders Markdown to HTML with all specific handling stuff.
259259
func Render(ctx *markup.RenderContext, input io.Reader, output io.Writer) error {
260-
ctx.MarkupType = MarkupName
260+
if ctx.Type == "" {
261+
ctx.Type = MarkupName
262+
}
261263
return markup.Render(ctx, input, output)
262264
}
263265

0 commit comments

Comments
 (0)