Skip to content

Commit c9d4cc6

Browse files
committed
feeds: render markdown to html
1 parent a223bc8 commit c9d4cc6

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

routers/web/feed/convert.go

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313

1414
"code.gitea.io/gitea/models"
1515
"code.gitea.io/gitea/modules/context"
16+
"code.gitea.io/gitea/modules/markup"
17+
"code.gitea.io/gitea/modules/markup/markdown"
1618
"code.gitea.io/gitea/modules/setting"
1719
"code.gitea.io/gitea/modules/templates"
1820
"code.gitea.io/gitea/modules/util"
@@ -44,6 +46,29 @@ func toReleaseLink(act *models.Action) string {
4446
return act.GetRepoLink() + "/releases/tag/" + util.PathEscapeSegments(act.GetBranch())
4547
}
4648

49+
func toIssueContent(ctx *context.Context, act *models.Action) string {
50+
return renderMarkdown(ctx, act, act.GetIssueContent())
51+
}
52+
53+
// renderMarkdown creates a minimal markdown render context from an action.
54+
// If rendering fails, the original markdown text is returned
55+
func renderMarkdown(ctx *context.Context, act *models.Action, content string) string {
56+
markdownCtx := &markup.RenderContext{
57+
Ctx: ctx,
58+
URLPrefix: act.GetRepoLink(),
59+
Type: markdown.MarkupName,
60+
Metas: map[string]string{
61+
"user": act.GetRepoUserName(),
62+
"repo": act.GetRepoName(),
63+
},
64+
}
65+
markdown, err := markdown.RenderString(markdownCtx, content)
66+
if err != nil {
67+
return content
68+
}
69+
return markdown
70+
}
71+
4772
// feedActionsToFeedItems convert gitea's Action feed to feeds Item
4873
func feedActionsToFeedItems(ctx *context.Context, actions []*models.Action) (items []*feeds.Item, err error) {
4974
for _, act := range actions {
@@ -192,12 +217,12 @@ func feedActionsToFeedItems(ctx *context.Context, actions []*models.Action) (ite
192217

193218
case models.ActionCreateIssue, models.ActionCreatePullRequest:
194219
desc = strings.Join(act.GetIssueInfos(), "#")
195-
content = act.GetIssueContent()
220+
content = renderMarkdown(ctx, act, act.GetIssueContent())
196221
case models.ActionCommentIssue, models.ActionApprovePullRequest, models.ActionRejectPullRequest, models.ActionCommentPull:
197222
desc = act.GetIssueTitle()
198223
comment := act.GetIssueInfos()[1]
199224
if len(comment) != 0 {
200-
desc += "\n\n" + comment
225+
desc += "\n\n" + renderMarkdown(ctx, act, comment)
201226
}
202227
case models.ActionMergePullRequest:
203228
desc = act.GetIssueInfos()[1]

0 commit comments

Comments
 (0)