Skip to content

Commit 70d39fc

Browse files
committed
fix
1 parent d2f6588 commit 70d39fc

32 files changed

+78
-50
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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func TestRender_StandardLinks(t *testing.T) {
5858
},
5959
}, input)
6060
assert.NoError(t, err)
61-
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
61+
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(buffer)))
6262

6363
buffer, err = markdown.RenderString(&markup.RenderContext{
6464
Ctx: git.DefaultContext,
@@ -68,7 +68,7 @@ func TestRender_StandardLinks(t *testing.T) {
6868
IsWiki: true,
6969
}, input)
7070
assert.NoError(t, err)
71-
assert.Equal(t, strings.TrimSpace(expectedWiki), strings.TrimSpace(buffer))
71+
assert.Equal(t, strings.TrimSpace(expectedWiki), strings.TrimSpace(string(buffer)))
7272
}
7373

7474
googleRendered := `<p><a href="https://google.com/" rel="nofollow">https://google.com/</a></p>`
@@ -93,7 +93,7 @@ func TestRender_Images(t *testing.T) {
9393
},
9494
}, input)
9595
assert.NoError(t, err)
96-
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
96+
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(buffer)))
9797
}
9898

9999
url := "../../.images/src/02/train.jpg"

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

routers/web/feed/profile.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func showUserFeed(ctx *context.Context, formatType string) {
5858
feed := &feeds.Feed{
5959
Title: ctx.Locale.TrString("home.feed_of", ctx.ContextUser.DisplayName()),
6060
Link: &feeds.Link{Href: ctx.ContextUser.HTMLURL()},
61-
Description: ctxUserDescription,
61+
Description: string(ctxUserDescription),
6262
Created: time.Now(),
6363
}
6464

routers/web/org/projects.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"code.gitea.io/gitea/modules/context"
2121
"code.gitea.io/gitea/modules/json"
2222
"code.gitea.io/gitea/modules/setting"
23+
"code.gitea.io/gitea/modules/templates"
2324
"code.gitea.io/gitea/modules/util"
2425
"code.gitea.io/gitea/modules/web"
2526
shared_user "code.gitea.io/gitea/routers/web/shared/user"
@@ -104,7 +105,7 @@ func Projects(ctx *context.Context) {
104105
}
105106

106107
for _, project := range projects {
107-
project.RenderedContent = project.Description
108+
project.RenderedContent = templates.Str2html(project.Description) // FIXME: is it right? why not render?
108109
}
109110

110111
err = shared_user.LoadHeaderCount(ctx)
@@ -395,7 +396,7 @@ func ViewProject(ctx *context.Context) {
395396
}
396397
}
397398

398-
project.RenderedContent = project.Description
399+
project.RenderedContent = templates.Str2html(project.Description) // FIXME: is it right? why not render?
399400
ctx.Data["LinkedPRs"] = linkedPrsMap
400401
ctx.Data["PageIsViewProjects"] = true
401402
ctx.Data["CanWriteProjects"] = canWriteProjects(ctx)

routers/web/repo/issue.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import (
4242
repo_module "code.gitea.io/gitea/modules/repository"
4343
"code.gitea.io/gitea/modules/setting"
4444
api "code.gitea.io/gitea/modules/structs"
45+
"code.gitea.io/gitea/modules/templates"
4546
"code.gitea.io/gitea/modules/templates/vars"
4647
"code.gitea.io/gitea/modules/timeutil"
4748
"code.gitea.io/gitea/modules/upload"
@@ -1759,7 +1760,7 @@ func ViewIssue(ctx *context.Context) {
17591760
// so "|" is used as delimeter to mark the new format
17601761
if comment.Content[0] != '|' {
17611762
// handle old time comments that have formatted text stored
1762-
comment.RenderedContent = comment.Content
1763+
comment.RenderedContent = templates.Str2html(comment.Content)
17631764
comment.Content = ""
17641765
} else {
17651766
// else it's just a duration in seconds to pass on to the frontend

routers/web/repo/release.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func getReleaseInfos(ctx *context.Context, opts *repo_model.FindReleasesOptions)
112112
cacheUsers[r.PublisherID] = r.Publisher
113113
}
114114

115-
r.Note, err = markdown.RenderString(&markup.RenderContext{
115+
r.RenderedNote, err = markdown.RenderString(&markup.RenderContext{
116116
Links: markup.Links{
117117
Base: ctx.Repo.RepoLink,
118118
},

templates/mail/release.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
{{.locale.Tr "mail.release.note"}}<br>
2323
{{- if eq .Release.RenderedNote ""}}
2424
{{else}}
25-
{{.Release.RenderedNote | Str2html}}
25+
{{.Release.RenderedNote}}
2626
{{end -}}
2727
</p>
2828
<br><br>

templates/org/header.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
{{end}}
1919
</span>
2020
</div>
21-
{{if .RenderedDescription}}<div class="render-content markup">{{.RenderedDescription | Str2html}}</div>{{end}}
21+
{{if .RenderedDescription}}<div class="render-content markup">{{.RenderedDescription}}</div>{{end}}
2222
<div class="text light meta gt-mt-2">
2323
{{if .Org.Location}}<div class="flex-text-block">{{svg "octicon-location"}} <span>{{.Org.Location}}</span></div>{{end}}
2424
{{if .Org.Website}}<div class="flex-text-block">{{svg "octicon-link"}} <a class="muted" target="_blank" rel="noopener noreferrer me" href="{{.Org.Website}}">{{.Org.Website}}</a></div>{{end}}

templates/org/home.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<div class="ui mobile reversed stackable grid">
77
<div class="ui {{if .ShowMemberAndTeamTab}}eleven wide{{end}} column">
88
{{if .ProfileReadme}}
9-
<div id="readme_profile" class="markup">{{.ProfileReadme | Str2html}}</div>
9+
<div id="readme_profile" class="markup">{{.ProfileReadme}}</div>
1010
{{end}}
1111
{{template "explore/repo_search" .}}
1212
{{template "explore/repo_list" .}}

templates/package/content/nuget.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<h4 class="ui top attached header">{{ctx.Locale.Tr "packages.about"}}</h4>
2121
<div class="ui attached segment">
2222
{{if .PackageDescriptor.Metadata.Description}}{{.PackageDescriptor.Metadata.Description}}{{end}}
23-
{{if .PackageDescriptor.Metadata.ReleaseNotes}}{{Str2html .PackageDescriptor.Metadata.ReleaseNotes}}{{end}}
23+
{{if .PackageDescriptor.Metadata.ReleaseNotes}}{{.PackageDescriptor.Metadata.ReleaseNotes}}{{end}}
2424
</div>
2525
{{end}}
2626

templates/projects/list.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
</div>
7676
{{if .Description}}
7777
<div class="content">
78-
{{.RenderedContent|Str2html}}
78+
{{.RenderedContent}}
7979
</div>
8080
{{end}}
8181
</li>

0 commit comments

Comments
 (0)