diff options
author | Norwin <noerw@users.noreply.github.com> | 2022-03-12 23:05:12 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-03-12 23:05:12 +0100 |
commit | 9cca834aac3831ea64b38c81a1cbbcaff2f41898 (patch) | |
tree | 554bd917149cea5b7ca9de2c2d29c9d4f4b93914 /routers | |
parent | a0c043f5c655aff5c54532d108cc25695ecba49e (diff) | |
download | gitea-9cca834aac3831ea64b38c81a1cbbcaff2f41898.tar.gz gitea-9cca834aac3831ea64b38c81a1cbbcaff2f41898.zip |
Feeds: render markdown to html (#19058)
* feeds: render markdown to html
Diffstat (limited to 'routers')
-rw-r--r-- | routers/web/feed/convert.go | 25 |
1 files 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] |