From 9cca834aac3831ea64b38c81a1cbbcaff2f41898 Mon Sep 17 00:00:00 2001 From: Norwin Date: Sat, 12 Mar 2022 23:05:12 +0100 Subject: [PATCH] Feeds: render markdown to html (#19058) * feeds: render markdown to html --- routers/web/feed/convert.go | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/routers/web/feed/convert.go b/routers/web/feed/convert.go index 4dbd9c9d0e..51df014f1d 100644 --- a/routers/web/feed/convert.go +++ b/routers/web/feed/convert.go @@ -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" @@ -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 { @@ -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]