Skip to content

Commit 18c5653

Browse files
committed
fix
1 parent d2f6588 commit 18c5653

32 files changed

+91
-61
lines changed

models/issues/comment.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package issues
88
import (
99
"context"
1010
"fmt"
11+
"html/template"
1112
"strconv"
1213
"unicode/utf8"
1314

@@ -259,8 +260,8 @@ type Comment struct {
259260
CommitID int64
260261
Line int64 // - previous line / + proposed line
261262
TreePath string
262-
Content string `xorm:"LONGTEXT"`
263-
RenderedContent string `xorm:"-"`
263+
Content string `xorm:"LONGTEXT"`
264+
RenderedContent template.HTML `xorm:"-"`
264265

265266
// Path represents the 4 lines of code cemented by this comment
266267
Patch string `xorm:"-"`

models/issues/issue.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package issues
77
import (
88
"context"
99
"fmt"
10+
"html/template"
1011
"regexp"
1112
"slices"
1213

@@ -105,7 +106,7 @@ type Issue struct {
105106
OriginalAuthorID int64 `xorm:"index"`
106107
Title string `xorm:"name"`
107108
Content string `xorm:"LONGTEXT"`
108-
RenderedContent string `xorm:"-"`
109+
RenderedContent template.HTML `xorm:"-"`
109110
Labels []*Label `xorm:"-"`
110111
MilestoneID int64 `xorm:"INDEX"`
111112
Milestone *Milestone `xorm:"-"`

models/issues/milestone.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package issues
66
import (
77
"context"
88
"fmt"
9+
"html/template"
910
"strings"
1011

1112
"code.gitea.io/gitea/models/db"
@@ -47,8 +48,8 @@ type Milestone struct {
4748
RepoID int64 `xorm:"INDEX"`
4849
Repo *repo_model.Repository `xorm:"-"`
4950
Name string
50-
Content string `xorm:"TEXT"`
51-
RenderedContent string `xorm:"-"`
51+
Content string `xorm:"TEXT"`
52+
RenderedContent template.HTML `xorm:"-"`
5253
IsClosed bool
5354
NumIssues int
5455
NumClosedIssues int

models/project/project.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package project
66
import (
77
"context"
88
"fmt"
9+
"html/template"
910

1011
"code.gitea.io/gitea/models/db"
1112
repo_model "code.gitea.io/gitea/models/repo"
@@ -100,7 +101,7 @@ type Project struct {
100101
CardType CardType
101102
Type Type
102103

103-
RenderedContent string `xorm:"-"`
104+
RenderedContent template.HTML `xorm:"-"`
104105

105106
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
106107
UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`

models/repo/release.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package repo
77
import (
88
"context"
99
"fmt"
10+
"html/template"
1011
"net/url"
1112
"sort"
1213
"strconv"
@@ -79,7 +80,7 @@ type Release struct {
7980
NumCommits int64
8081
NumCommitsBehind int64 `xorm:"-"`
8182
Note string `xorm:"TEXT"`
82-
RenderedNote string `xorm:"-"`
83+
RenderedNote template.HTML `xorm:"-"`
8384
IsDraft bool `xorm:"NOT NULL DEFAULT false"`
8485
IsPrerelease bool `xorm:"NOT NULL DEFAULT false"`
8586
IsTag bool `xorm:"NOT NULL DEFAULT false"` // will be true only if the record is a tag and has no related releases

modules/markup/html_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ func TestRender_ShortLinks(t *testing.T) {
397397
},
398398
}, input)
399399
assert.NoError(t, err)
400-
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
400+
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(buffer)))
401401
buffer, err = markdown.RenderString(&markup.RenderContext{
402402
Ctx: git.DefaultContext,
403403
Links: markup.Links{
@@ -407,7 +407,7 @@ func TestRender_ShortLinks(t *testing.T) {
407407
IsWiki: true,
408408
}, input)
409409
assert.NoError(t, err)
410-
assert.Equal(t, strings.TrimSpace(expectedWiki), strings.TrimSpace(buffer))
410+
assert.Equal(t, strings.TrimSpace(expectedWiki), strings.TrimSpace(string(buffer)))
411411
}
412412

413413
mediatree := util.URLJoin(markup.TestRepoURL, "media", "master")
@@ -510,7 +510,7 @@ func TestRender_RelativeImages(t *testing.T) {
510510
Metas: localMetas,
511511
}, input)
512512
assert.NoError(t, err)
513-
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
513+
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(buffer)))
514514
buffer, err = markdown.RenderString(&markup.RenderContext{
515515
Ctx: git.DefaultContext,
516516
Links: markup.Links{
@@ -520,7 +520,7 @@ func TestRender_RelativeImages(t *testing.T) {
520520
IsWiki: true,
521521
}, input)
522522
assert.NoError(t, err)
523-
assert.Equal(t, strings.TrimSpace(expectedWiki), strings.TrimSpace(buffer))
523+
assert.Equal(t, strings.TrimSpace(expectedWiki), strings.TrimSpace(string(buffer)))
524524
}
525525

526526
rawwiki := util.URLJoin(markup.TestRepoURL, "wiki", "raw")

modules/markup/markdown/markdown.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package markdown
66

77
import (
88
"fmt"
9+
"html/template"
910
"io"
1011
"strings"
1112
"sync"
@@ -262,12 +263,12 @@ func Render(ctx *markup.RenderContext, input io.Reader, output io.Writer) error
262263
}
263264

264265
// RenderString renders Markdown string to HTML with all specific handling stuff and return string
265-
func RenderString(ctx *markup.RenderContext, content string) (string, error) {
266+
func RenderString(ctx *markup.RenderContext, content string) (template.HTML, error) {
266267
var buf strings.Builder
267268
if err := Render(ctx, strings.NewReader(content), &buf); err != nil {
268269
return "", err
269270
}
270-
return buf.String(), nil
271+
return template.HTML(buf.String()), nil
271272
}
272273

273274
// RenderRaw renders Markdown to HTML without handling special links.

modules/markup/markdown/markdown_test.go

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package markdown_test
55

66
import (
77
"context"
8+
"html/template"
89
"os"
910
"strings"
1011
"testing"
@@ -58,7 +59,7 @@ func TestRender_StandardLinks(t *testing.T) {
5859
},
5960
}, input)
6061
assert.NoError(t, err)
61-
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
62+
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(buffer)))
6263

6364
buffer, err = markdown.RenderString(&markup.RenderContext{
6465
Ctx: git.DefaultContext,
@@ -68,7 +69,7 @@ func TestRender_StandardLinks(t *testing.T) {
6869
IsWiki: true,
6970
}, input)
7071
assert.NoError(t, err)
71-
assert.Equal(t, strings.TrimSpace(expectedWiki), strings.TrimSpace(buffer))
72+
assert.Equal(t, strings.TrimSpace(expectedWiki), strings.TrimSpace(string(buffer)))
7273
}
7374

7475
googleRendered := `<p><a href="https://google.com/" rel="nofollow">https://google.com/</a></p>`
@@ -93,7 +94,7 @@ func TestRender_Images(t *testing.T) {
9394
},
9495
}, input)
9596
assert.NoError(t, err)
96-
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
97+
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(buffer)))
9798
}
9899

99100
url := "../../.images/src/02/train.jpg"
@@ -303,7 +304,7 @@ func TestTotal_RenderWiki(t *testing.T) {
303304
IsWiki: true,
304305
}, sameCases[i])
305306
assert.NoError(t, err)
306-
assert.Equal(t, answers[i], line)
307+
assert.Equal(t, template.HTML(answers[i]), line)
307308
}
308309

309310
testCases := []string{
@@ -328,7 +329,7 @@ func TestTotal_RenderWiki(t *testing.T) {
328329
IsWiki: true,
329330
}, testCases[i])
330331
assert.NoError(t, err)
331-
assert.Equal(t, testCases[i+1], line)
332+
assert.Equal(t, template.HTML(testCases[i+1]), line)
332333
}
333334
}
334335

@@ -348,7 +349,7 @@ func TestTotal_RenderString(t *testing.T) {
348349
Metas: localMetas,
349350
}, sameCases[i])
350351
assert.NoError(t, err)
351-
assert.Equal(t, answers[i], line)
352+
assert.Equal(t, template.HTML(answers[i]), line)
352353
}
353354

354355
testCases := []string{}
@@ -361,7 +362,7 @@ func TestTotal_RenderString(t *testing.T) {
361362
},
362363
}, testCases[i])
363364
assert.NoError(t, err)
364-
assert.Equal(t, testCases[i+1], line)
365+
assert.Equal(t, template.HTML(testCases[i+1]), line)
365366
}
366367
}
367368

@@ -428,7 +429,7 @@ func TestRenderEmojiInLinks_Issue12331(t *testing.T) {
428429
`
429430
res, err := markdown.RenderString(&markup.RenderContext{Ctx: git.DefaultContext}, testcase)
430431
assert.NoError(t, err)
431-
assert.Equal(t, expected, res)
432+
assert.Equal(t, template.HTML(expected), res)
432433
}
433434

434435
func TestColorPreview(t *testing.T) {
@@ -462,7 +463,7 @@ func TestColorPreview(t *testing.T) {
462463
for _, test := range positiveTests {
463464
res, err := markdown.RenderString(&markup.RenderContext{Ctx: git.DefaultContext}, test.testcase)
464465
assert.NoError(t, err, "Unexpected error in testcase: %q", test.testcase)
465-
assert.Equal(t, test.expected, res, "Unexpected result in testcase %q", test.testcase)
466+
assert.Equal(t, template.HTML(test.expected), res, "Unexpected result in testcase %q", test.testcase)
466467

467468
}
468469

@@ -529,7 +530,7 @@ func TestMathBlock(t *testing.T) {
529530
for _, test := range testcases {
530531
res, err := markdown.RenderString(&markup.RenderContext{Ctx: git.DefaultContext}, test.testcase)
531532
assert.NoError(t, err, "Unexpected error in testcase: %q", test.testcase)
532-
assert.Equal(t, test.expected, res, "Unexpected result in testcase %q", test.testcase)
533+
assert.Equal(t, template.HTML(test.expected), res, "Unexpected result in testcase %q", test.testcase)
533534

534535
}
535536
}
@@ -567,12 +568,12 @@ foo: bar
567568
for _, test := range testcases {
568569
res, err := markdown.RenderString(&markup.RenderContext{Ctx: git.DefaultContext}, test.testcase)
569570
assert.NoError(t, err, "Unexpected error in testcase: %q", test.testcase)
570-
assert.Equal(t, test.expected, res, "Unexpected result in testcase %q", test.testcase)
571+
assert.Equal(t, template.HTML(test.expected), res, "Unexpected result in testcase %q", test.testcase)
571572
}
572573
}
573574

574575
func TestRenderLinks(t *testing.T) {
575-
input := ` space @mention-user
576+
input := ` space @mention-user${SPACE}${SPACE}
576577
/just/a/path.bin
577578
https://example.com/file.bin
578579
[local link](file.bin)
@@ -593,8 +594,9 @@ com 88fc37a3c0a4dda553bdcfc80c178a58247f42fb mit
593594
594595
@mention-user test
595596
#123
596-
space
597+
space${SPACE}${SPACE}
597598
`
599+
input = strings.ReplaceAll(input, "${SPACE}", " ") // replace ${SPACE} with " ", to avoid some editor's auto-trimming
598600
cases := []struct {
599601
Links markup.Links
600602
IsWiki bool
@@ -957,6 +959,6 @@ space</p>
957959
for i, c := range cases {
958960
result, err := markdown.RenderString(&markup.RenderContext{Ctx: context.Background(), Links: c.Links, IsWiki: c.IsWiki}, input)
959961
assert.NoError(t, err, "Unexpected error in testcase: %v", i)
960-
assert.Equal(t, c.Expected, result, "Unexpected result in testcase %v", i)
962+
assert.Equal(t, template.HTML(c.Expected), result, "Unexpected result in testcase %v", i)
961963
}
962964
}

modules/templates/util_render.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ func RenderMarkdownToHtml(ctx context.Context, input string) template.HTML { //n
208208
if err != nil {
209209
log.Error("RenderString: %v", err)
210210
}
211-
return template.HTML(output)
211+
return output
212212
}
213213

214214
func RenderLabels(ctx context.Context, labels []*issues_model.Label, repoLink string) template.HTML {

modules/templates/util_string.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
package templates
55

66
import (
7+
"fmt"
8+
"html/template"
79
"strings"
810

911
"code.gitea.io/gitea/modules/base"
@@ -17,6 +19,19 @@ func NewStringUtils() *StringUtils {
1719
return &stringUtils
1820
}
1921

22+
func (su *StringUtils) ToString(v any) string {
23+
switch v := v.(type) {
24+
case string:
25+
return v
26+
case template.HTML:
27+
return string(v)
28+
case fmt.Stringer:
29+
return v.String()
30+
default:
31+
return fmt.Sprint(v)
32+
}
33+
}
34+
2035
func (su *StringUtils) HasPrefix(s, prefix string) bool {
2136
return strings.HasPrefix(s, prefix)
2237
}

routers/web/feed/convert.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func toReleaseLink(ctx *context.Context, act *activities_model.Action) string {
5050

5151
// renderMarkdown creates a minimal markdown render context from an action.
5252
// If rendering fails, the original markdown text is returned
53-
func renderMarkdown(ctx *context.Context, act *activities_model.Action, content string) string {
53+
func renderMarkdown(ctx *context.Context, act *activities_model.Action, content string) template.HTML {
5454
markdownCtx := &markup.RenderContext{
5555
Ctx: ctx,
5656
Links: markup.Links{
@@ -64,7 +64,7 @@ func renderMarkdown(ctx *context.Context, act *activities_model.Action, content
6464
}
6565
markdown, err := markdown.RenderString(markdownCtx, content)
6666
if err != nil {
67-
return content
67+
return templates.Str2html(content)
6868
}
6969
return markdown
7070
}
@@ -74,7 +74,11 @@ func feedActionsToFeedItems(ctx *context.Context, actions activities_model.Actio
7474
for _, act := range actions {
7575
act.LoadActUser(ctx)
7676

77-
var content, desc, title string
77+
// TODO: the code seems quite strange (maybe not right)
78+
// sometimes it uses text content but sometimes it uses HTML content
79+
// it should clearly defines which kind of content it should use for the feed items: plan text or rich HTML
80+
var title, desc string
81+
var content template.HTML
7882

7983
link := &feeds.Link{Href: act.GetCommentHTMLURL(ctx)}
8084

@@ -228,7 +232,7 @@ func feedActionsToFeedItems(ctx *context.Context, actions activities_model.Actio
228232
desc = act.GetIssueTitle(ctx)
229233
comment := act.GetIssueInfos()[1]
230234
if len(comment) != 0 {
231-
desc += "\n\n" + renderMarkdown(ctx, act, comment)
235+
desc += "\n\n" + string(renderMarkdown(ctx, act, comment))
232236
}
233237
case activities_model.ActionMergePullRequest, activities_model.ActionAutoMergePullRequest:
234238
desc = act.GetIssueInfos()[1]
@@ -239,7 +243,7 @@ func feedActionsToFeedItems(ctx *context.Context, actions activities_model.Actio
239243
}
240244
}
241245
if len(content) == 0 {
242-
content = desc
246+
content = templates.Str2html(desc)
243247
}
244248

245249
items = append(items, &feeds.Item{
@@ -253,7 +257,7 @@ func feedActionsToFeedItems(ctx *context.Context, actions activities_model.Actio
253257
},
254258
Id: fmt.Sprintf("%v: %v", strconv.FormatInt(act.ID, 10), link.Href),
255259
Created: act.CreatedUnix.AsTime(),
256-
Content: content,
260+
Content: string(content),
257261
})
258262
}
259263
return items, err
@@ -282,7 +286,8 @@ func releasesToFeedItems(ctx *context.Context, releases []*repo_model.Release, i
282286
return nil, err
283287
}
284288

285-
var title, content string
289+
var title string
290+
var content template.HTML
286291

287292
if rel.IsTag {
288293
title = rel.TagName
@@ -311,7 +316,7 @@ func releasesToFeedItems(ctx *context.Context, releases []*repo_model.Release, i
311316
Email: rel.Publisher.GetEmail(),
312317
},
313318
Id: fmt.Sprintf("%v: %v", strconv.FormatInt(rel.ID, 10), link.Href),
314-
Content: content,
319+
Content: string(content),
315320
})
316321
}
317322

0 commit comments

Comments
 (0)