Skip to content

Commit deca667

Browse files
committed
fix
1 parent 840ad7e commit deca667

30 files changed

+164
-193
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

+2-3
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,6 @@ func TestRender_IssueIndexPattern_Document(t *testing.T) {
266266
"user": "someUser",
267267
"repo": "someRepo",
268268
"style": IssueNameStyleNumeric,
269-
"mode": "document",
270269
}
271270

272271
testRenderIssueIndexPattern(t, "#1", "#1", &RenderContext{
@@ -316,8 +315,8 @@ func TestRender_AutoLink(t *testing.T) {
316315
Links: Links{
317316
Base: TestRepoURL,
318317
},
319-
Metas: localMetas,
320-
IsWiki: true,
318+
Metas: localMetas,
319+
ContentMode: RenderContentAsWiki,
321320
}, strings.NewReader(input), &buffer)
322321
assert.Equal(t, err, nil)
323322
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

+6-7
Original file line numberDiff line numberDiff line change
@@ -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: markup.RenderContentAsWiki,
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

+2-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ 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+
// keep the old behavior
76+
if ctx.ContentMode == markup.RenderContentAsComment || ctx.ContentMode == markup.RenderContentAsWiki {
7677
v.SetHardLineBreak(setting.Markdown.EnableHardLineBreakInComments)
7778
} else {
7879
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

+10-6
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func TestRender_StandardLinks(t *testing.T) {
7474
Links: markup.Links{
7575
Base: FullURL,
7676
},
77-
IsWiki: true,
77+
ContentMode: markup.RenderContentAsWiki,
7878
}, input)
7979
assert.NoError(t, err)
8080
assert.Equal(t, strings.TrimSpace(expectedWiki), strings.TrimSpace(string(buffer)))
@@ -306,9 +306,9 @@ func TestTotal_RenderWiki(t *testing.T) {
306306
Links: markup.Links{
307307
Base: FullURL,
308308
},
309-
Repo: newMockRepo(testRepoOwnerName, testRepoName),
310-
Metas: localMetas,
311-
IsWiki: true,
309+
Repo: newMockRepo(testRepoOwnerName, testRepoName),
310+
Metas: localMetas,
311+
ContentMode: markup.RenderContentAsWiki,
312312
}, sameCases[i])
313313
assert.NoError(t, err)
314314
actual := strings.ReplaceAll(string(line), ` data-markdown-generated-content=""`, "")
@@ -334,7 +334,7 @@ func TestTotal_RenderWiki(t *testing.T) {
334334
Links: markup.Links{
335335
Base: FullURL,
336336
},
337-
IsWiki: true,
337+
ContentMode: markup.RenderContentAsWiki,
338338
}, testCases[i])
339339
assert.NoError(t, err)
340340
actual := strings.ReplaceAll(string(line), ` data-markdown-generated-content=""`, "")
@@ -997,7 +997,11 @@ space</p>
997997
}
998998

999999
for i, c := range cases {
1000-
result, err := markdown.RenderString(&markup.RenderContext{Ctx: context.Background(), Links: c.Links, IsWiki: c.IsWiki}, input)
1000+
result, err := markdown.RenderString(&markup.RenderContext{
1001+
Ctx: context.Background(),
1002+
Links: c.Links,
1003+
ContentMode: util.Iif(c.IsWiki, markup.RenderContentAsWiki, markup.RenderContentAsDefault),
1004+
}, input)
10011005
assert.NoError(t, err, "Unexpected error in testcase: %v", i)
10021006
actual := strings.ReplaceAll(string(result), ` data-markdown-generated-content=""`, "")
10031007
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

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func TestRender_StandardLinks(t *testing.T) {
2626
Base: "/relative-path",
2727
BranchPath: "branch/main",
2828
},
29-
IsWiki: isWiki,
29+
ContentMode: markup.RenderContentAsWiki,
3030
}, input)
3131
assert.NoError(t, err)
3232
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))

0 commit comments

Comments
 (0)