Skip to content

Commit

Permalink
Feeds: render markdown to html (go-gitea#19058)
Browse files Browse the repository at this point in the history
* feeds: render markdown to html
  • Loading branch information
noerw authored and Stelios Malathouras committed Mar 28, 2022
1 parent 439beed commit bb19203
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions routers/web/feed/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/markup"
"code.gitea.io/gitea/modules/markup/markdown"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/templates"
"code.gitea.io/gitea/modules/util"
Expand Down Expand Up @@ -44,6 +46,25 @@ func toReleaseLink(act *models.Action) string {
return act.GetRepoLink() + "/releases/tag/" + util.PathEscapeSegments(act.GetBranch())
}

// renderMarkdown creates a minimal markdown render context from an action.
// If rendering fails, the original markdown text is returned
func renderMarkdown(ctx *context.Context, act *models.Action, content string) string {
markdownCtx := &markup.RenderContext{
Ctx: ctx,
URLPrefix: act.GetRepoLink(),
Type: markdown.MarkupName,
Metas: map[string]string{
"user": act.GetRepoUserName(),
"repo": act.GetRepoName(),
},
}
markdown, err := markdown.RenderString(markdownCtx, content)
if err != nil {
return content
}
return markdown
}

// feedActionsToFeedItems convert gitea's Action feed to feeds Item
func feedActionsToFeedItems(ctx *context.Context, actions []*models.Action) (items []*feeds.Item, err error) {
for _, act := range actions {
Expand Down Expand Up @@ -192,12 +213,12 @@ func feedActionsToFeedItems(ctx *context.Context, actions []*models.Action) (ite

case models.ActionCreateIssue, models.ActionCreatePullRequest:
desc = strings.Join(act.GetIssueInfos(), "#")
content = act.GetIssueContent()
content = renderMarkdown(ctx, act, act.GetIssueContent())
case models.ActionCommentIssue, models.ActionApprovePullRequest, models.ActionRejectPullRequest, models.ActionCommentPull:
desc = act.GetIssueTitle()
comment := act.GetIssueInfos()[1]
if len(comment) != 0 {
desc += "\n\n" + comment
desc += "\n\n" + renderMarkdown(ctx, act, comment)
}
case models.ActionMergePullRequest:
desc = act.GetIssueInfos()[1]
Expand Down

0 comments on commit bb19203

Please sign in to comment.