Skip to content

Commit 9cca834

Browse files
authored
Feeds: render markdown to html (#19058)
* feeds: render markdown to html
1 parent a0c043f commit 9cca834

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

routers/web/feed/convert.go

Lines changed: 23 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,25 @@ func toReleaseLink(act *models.Action) string {
4446
return act.GetRepoLink() + "/releases/tag/" + util.PathEscapeSegments(act.GetBranch())
4547
}
4648

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

193214
case models.ActionCreateIssue, models.ActionCreatePullRequest:
194215
desc = strings.Join(act.GetIssueInfos(), "#")
195-
content = act.GetIssueContent()
216+
content = renderMarkdown(ctx, act, act.GetIssueContent())
196217
case models.ActionCommentIssue, models.ActionApprovePullRequest, models.ActionRejectPullRequest, models.ActionCommentPull:
197218
desc = act.GetIssueTitle()
198219
comment := act.GetIssueInfos()[1]
199220
if len(comment) != 0 {
200-
desc += "\n\n" + comment
221+
desc += "\n\n" + renderMarkdown(ctx, act, comment)
201222
}
202223
case models.ActionMergePullRequest:
203224
desc = act.GetIssueInfos()[1]

0 commit comments

Comments
 (0)