Skip to content

Commit a1765f7

Browse files
committed
fix
1 parent 840ad7e commit a1765f7

30 files changed

+192
-206
lines changed

models/repo/repo.go

-2
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,6 @@ 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",
483482
}
484483

485484
unit, err := repo.GetUnit(ctx, unit.TypeExternalTracker)
@@ -521,7 +520,6 @@ func (repo *Repository) ComposeDocumentMetas(ctx context.Context) map[string]str
521520
for k, v := range repo.ComposeMetas(ctx) {
522521
metas[k] = v
523522
}
524-
metas["mode"] = "document"
525523
repo.DocumentRenderingMetas = metas
526524
}
527525
return repo.DocumentRenderingMetas

modules/markup/console/console.go

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

1312
"code.gitea.io/gitea/modules/markup"
1413
"code.gitea.io/gitea/modules/setting"
@@ -17,9 +16,6 @@ import (
1716
"github.com/go-enry/go-enry/v2"
1817
)
1918

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

3026
// Name implements markup.Renderer
3127
func (Renderer) Name() string {
32-
return MarkupName
28+
return "console"
3329
}
3430

3531
// Extensions implements markup.Renderer
@@ -67,20 +63,3 @@ func (Renderer) Render(ctx *markup.RenderContext, input io.Reader, output io.Wri
6763
_, err = output.Write(buf)
6864
return err
6965
}
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_codepreview_test.go

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

1212
"code.gitea.io/gitea/modules/git"
1313
"code.gitea.io/gitea/modules/markup"
14+
"code.gitea.io/gitea/modules/markup/markdown"
1415

1516
"github.com/stretchr/testify/assert"
1617
)
@@ -23,8 +24,8 @@ func TestRenderCodePreview(t *testing.T) {
2324
})
2425
test := func(input, expected string) {
2526
buffer, err := markup.RenderString(&markup.RenderContext{
26-
Ctx: git.DefaultContext,
27-
Type: "markdown",
27+
Ctx: git.DefaultContext,
28+
MarkupType: markdown.MarkupName,
2829
}, input)
2930
assert.NoError(t, err)
3031
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))

modules/markup/html_internal_test.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,9 @@ func TestRender_IssueIndexPattern2(t *testing.T) {
123123
}
124124
expectedNil := fmt.Sprintf(expectedFmt, links...)
125125
testRenderIssueIndexPattern(t, s, expectedNil, &RenderContext{
126-
Ctx: git.DefaultContext,
127-
Metas: localMetas,
126+
Ctx: git.DefaultContext,
127+
Metas: localMetas,
128+
ContentMode: RenderContentAsComment,
128129
})
129130

130131
class := "ref-issue"
@@ -137,8 +138,9 @@ func TestRender_IssueIndexPattern2(t *testing.T) {
137138
}
138139
expectedNum := fmt.Sprintf(expectedFmt, links...)
139140
testRenderIssueIndexPattern(t, s, expectedNum, &RenderContext{
140-
Ctx: git.DefaultContext,
141-
Metas: numericMetas,
141+
Ctx: git.DefaultContext,
142+
Metas: numericMetas,
143+
ContentMode: RenderContentAsComment,
142144
})
143145
}
144146

@@ -266,7 +268,6 @@ func TestRender_IssueIndexPattern_Document(t *testing.T) {
266268
"user": "someUser",
267269
"repo": "someRepo",
268270
"style": IssueNameStyleNumeric,
269-
"mode": "document",
270271
}
271272

272273
testRenderIssueIndexPattern(t, "#1", "#1", &RenderContext{
@@ -316,8 +317,8 @@ func TestRender_AutoLink(t *testing.T) {
316317
Links: Links{
317318
Base: TestRepoURL,
318319
},
319-
Metas: localMetas,
320-
IsWiki: true,
320+
Metas: localMetas,
321+
ContentMode: RenderContentAsWiki,
321322
}, strings.NewReader(input), &buffer)
322323
assert.Equal(t, err, nil)
323324
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer.String()))

modules/markup/html_issue.go

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

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
70+
// crossLinkOnly if not comment and not wiki
71+
crossLinkOnly := ctx.ContentMode != RenderContentAsTitle && ctx.ContentMode != RenderContentAsComment && ctx.ContentMode != RenderContentAsWiki
7372

7473
var (
7574
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.IsWiki {
23+
if ctx.ContentMode == RenderContentAsWiki {
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.IsWiki), link)
150+
link = util.URLJoin(ctx.Links.ResolveMediaLink(ctx.ContentMode == RenderContentAsWiki), 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.IsWiki), attr.Val)
20+
attr.Val = util.URLJoin(ctx.Links.ResolveMediaLink(ctx.ContentMode == RenderContentAsWiki), 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.IsWiki), attr.Val)
56+
attr.Val = util.URLJoin(ctx.Links.ResolveMediaLink(ctx.ContentMode == RenderContentAsWiki), attr.Val)
5757
}
5858
attr.Val = camoHandleLink(attr.Val)
5959
node.Attr[i] = attr

modules/markup/html_test.go

+10-11
Original file line numberDiff line numberDiff line change
@@ -302,10 +302,10 @@ func TestRender_email(t *testing.T) {
302302
j.doe@example.com;
303303
j.doe@example.com?
304304
j.doe@example.com!`,
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/>
305+
`<p><a href="mailto:j.doe@example.com" rel="nofollow">j.doe@example.com</a>,
306+
<a href="mailto:j.doe@example.com" rel="nofollow">j.doe@example.com</a>.
307+
<a href="mailto:j.doe@example.com" rel="nofollow">j.doe@example.com</a>;
308+
<a href="mailto:j.doe@example.com" rel="nofollow">j.doe@example.com</a>?
309309
<a href="mailto:j.doe@example.com" rel="nofollow">j.doe@example.com</a>!</p>`)
310310

311311
// Test that should *not* be turned into email links
@@ -418,8 +418,8 @@ func TestRender_ShortLinks(t *testing.T) {
418418
Links: markup.Links{
419419
Base: markup.TestRepoURL,
420420
},
421-
Metas: localMetas,
422-
IsWiki: true,
421+
Metas: localMetas,
422+
ContentMode: markup.RenderContentAsWiki,
423423
}, input)
424424
assert.NoError(t, err)
425425
assert.Equal(t, strings.TrimSpace(expectedWiki), strings.TrimSpace(string(buffer)))
@@ -531,10 +531,10 @@ func TestRender_ShortLinks(t *testing.T) {
531531
func TestRender_RelativeMedias(t *testing.T) {
532532
render := func(input string, isWiki bool, links markup.Links) string {
533533
buffer, err := markdown.RenderString(&markup.RenderContext{
534-
Ctx: git.DefaultContext,
535-
Links: links,
536-
Metas: localMetas,
537-
IsWiki: isWiki,
534+
Ctx: git.DefaultContext,
535+
Links: links,
536+
Metas: localMetas,
537+
ContentMode: util.Iif(isWiki, markup.RenderContentAsWiki, markup.RenderContentAsComment),
538538
}, input)
539539
assert.NoError(t, err)
540540
return strings.TrimSpace(string(buffer))
@@ -608,7 +608,6 @@ func TestPostProcess_RenderDocument(t *testing.T) {
608608
localMetas := map[string]string{
609609
"user": "go-gitea",
610610
"repo": "gitea",
611-
"mode": "document",
612611
}
613612

614613
test := func(input, expected string) {

modules/markup/markdown/goldmark.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,12 @@ 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-
if ctx.Metas["mode"] != "document" {
75+
// TODO: this was a quite unclear part
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 ctx.UseHardLineBreak.Has() {
79+
v.SetHardLineBreak(ctx.UseHardLineBreak.Value())
80+
} else if ctx.ContentMode == markup.RenderContentAsComment {
7681
v.SetHardLineBreak(setting.Markdown.EnableHardLineBreakInComments)
7782
} else {
7883
v.SetHardLineBreak(setting.Markdown.EnableHardLineBreakInDocuments)

modules/markup/markdown/markdown.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -257,9 +257,7 @@ 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-
if ctx.Type == "" {
261-
ctx.Type = MarkupName
262-
}
260+
ctx.MarkupType = MarkupName
263261
return markup.Render(ctx, input, output)
264262
}
265263

modules/markup/markdown/markdown_test.go

+17-9
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"code.gitea.io/gitea/modules/log"
1515
"code.gitea.io/gitea/modules/markup"
1616
"code.gitea.io/gitea/modules/markup/markdown"
17+
"code.gitea.io/gitea/modules/optional"
1718
"code.gitea.io/gitea/modules/setting"
1819
"code.gitea.io/gitea/modules/svg"
1920
"code.gitea.io/gitea/modules/util"
@@ -74,7 +75,7 @@ func TestRender_StandardLinks(t *testing.T) {
7475
Links: markup.Links{
7576
Base: FullURL,
7677
},
77-
IsWiki: true,
78+
ContentMode: markup.RenderContentAsWiki,
7879
}, input)
7980
assert.NoError(t, err)
8081
assert.Equal(t, strings.TrimSpace(expectedWiki), strings.TrimSpace(string(buffer)))
@@ -306,9 +307,10 @@ func TestTotal_RenderWiki(t *testing.T) {
306307
Links: markup.Links{
307308
Base: FullURL,
308309
},
309-
Repo: newMockRepo(testRepoOwnerName, testRepoName),
310-
Metas: localMetas,
311-
IsWiki: true,
310+
Repo: newMockRepo(testRepoOwnerName, testRepoName),
311+
Metas: localMetas,
312+
ContentMode: markup.RenderContentAsWiki,
313+
UseHardLineBreak: optional.Some(true),
312314
}, sameCases[i])
313315
assert.NoError(t, err)
314316
actual := strings.ReplaceAll(string(line), ` data-markdown-generated-content=""`, "")
@@ -334,7 +336,7 @@ func TestTotal_RenderWiki(t *testing.T) {
334336
Links: markup.Links{
335337
Base: FullURL,
336338
},
337-
IsWiki: true,
339+
ContentMode: markup.RenderContentAsWiki,
338340
}, testCases[i])
339341
assert.NoError(t, err)
340342
actual := strings.ReplaceAll(string(line), ` data-markdown-generated-content=""`, "")
@@ -354,8 +356,9 @@ func TestTotal_RenderString(t *testing.T) {
354356
Base: FullURL,
355357
BranchPath: "master",
356358
},
357-
Repo: newMockRepo(testRepoOwnerName, testRepoName),
358-
Metas: localMetas,
359+
Repo: newMockRepo(testRepoOwnerName, testRepoName),
360+
Metas: localMetas,
361+
UseHardLineBreak: optional.Some(true),
359362
}, sameCases[i])
360363
assert.NoError(t, err)
361364
actual := strings.ReplaceAll(string(line), ` data-markdown-generated-content=""`, "")
@@ -428,7 +431,7 @@ func TestRenderSiblingImages_Issue12925(t *testing.T) {
428431
expected := `<p><a href="/image1" target="_blank" rel="nofollow noopener"><img src="/image1" alt="image1"></a><br>
429432
<a href="/image2" target="_blank" rel="nofollow noopener"><img src="/image2" alt="image2"></a></p>
430433
`
431-
res, err := markdown.RenderRawString(&markup.RenderContext{Ctx: git.DefaultContext}, testcase)
434+
res, err := markdown.RenderRawString(&markup.RenderContext{Ctx: git.DefaultContext, UseHardLineBreak: optional.Some(true)}, testcase)
432435
assert.NoError(t, err)
433436
assert.Equal(t, expected, res)
434437
}
@@ -997,7 +1000,12 @@ space</p>
9971000
}
9981001

9991002
for i, c := range cases {
1000-
result, err := markdown.RenderString(&markup.RenderContext{Ctx: context.Background(), Links: c.Links, IsWiki: c.IsWiki}, input)
1003+
result, err := markdown.RenderString(&markup.RenderContext{
1004+
Ctx: context.Background(),
1005+
Links: c.Links,
1006+
ContentMode: util.Iif(c.IsWiki, markup.RenderContentAsWiki, markup.RenderContentAsDefault),
1007+
UseHardLineBreak: optional.Some(true),
1008+
}, input)
10011009
assert.NoError(t, err, "Unexpected error in testcase: %v", i)
10021010
actual := strings.ReplaceAll(string(result), ` data-markdown-generated-content=""`, "")
10031011
assert.Equal(t, c.Expected, actual, "Unexpected result in testcase %v", i)

modules/markup/markdown/transform_image.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func (g *ASTTransformer) transformImage(ctx *markup.RenderContext, v *ast.Image)
2121
// Check if the destination is a real link
2222
if len(v.Destination) > 0 && !markup.IsFullURLBytes(v.Destination) {
2323
v.Destination = []byte(giteautil.URLJoin(
24-
ctx.Links.ResolveMediaLink(ctx.IsWiki),
24+
ctx.Links.ResolveMediaLink(ctx.ContentMode == markup.RenderContentAsWiki),
2525
strings.TrimLeft(string(v.Destination), "/"),
2626
))
2727
}

modules/markup/orgmode/orgmode.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -144,14 +144,15 @@ func (r *Writer) resolveLink(kind, link string) string {
144144
}
145145

146146
base := r.Ctx.Links.Base
147-
if r.Ctx.IsWiki {
147+
isWiki := r.Ctx.ContentMode == markup.RenderContentAsWiki
148+
if isWiki {
148149
base = r.Ctx.Links.WikiLink()
149150
} else if r.Ctx.Links.HasBranchInfo() {
150151
base = r.Ctx.Links.SrcLink()
151152
}
152153

153154
if kind == "image" || kind == "video" {
154-
base = r.Ctx.Links.ResolveMediaLink(r.Ctx.IsWiki)
155+
base = r.Ctx.Links.ResolveMediaLink(isWiki)
155156
}
156157

157158
link = util.URLJoin(base, link)

modules/markup/orgmode/orgmode_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"code.gitea.io/gitea/modules/git"
1111
"code.gitea.io/gitea/modules/markup"
1212
"code.gitea.io/gitea/modules/setting"
13+
"code.gitea.io/gitea/modules/util"
1314

1415
"github.com/stretchr/testify/assert"
1516
)
@@ -26,7 +27,7 @@ func TestRender_StandardLinks(t *testing.T) {
2627
Base: "/relative-path",
2728
BranchPath: "branch/main",
2829
},
29-
IsWiki: isWiki,
30+
ContentMode: util.Iif(isWiki, markup.RenderContentAsWiki, markup.RenderContentAsDefault),
3031
}, input)
3132
assert.NoError(t, err)
3233
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))

0 commit comments

Comments
 (0)