Template
1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo synced 2024-11-26 03:36:10 +01:00

Feeds: render markdown to html (#19058)

* feeds: render markdown to html
This commit is contained in:
Norwin 2022-03-12 23:05:12 +01:00 committed by GitHub
parent a0c043f5c6
commit 9cca834aac
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -13,6 +13,8 @@ import (
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/context" "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/setting"
"code.gitea.io/gitea/modules/templates" "code.gitea.io/gitea/modules/templates"
"code.gitea.io/gitea/modules/util" "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()) 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 // feedActionsToFeedItems convert gitea's Action feed to feeds Item
func feedActionsToFeedItems(ctx *context.Context, actions []*models.Action) (items []*feeds.Item, err error) { func feedActionsToFeedItems(ctx *context.Context, actions []*models.Action) (items []*feeds.Item, err error) {
for _, act := range actions { for _, act := range actions {
@ -192,12 +213,12 @@ func feedActionsToFeedItems(ctx *context.Context, actions []*models.Action) (ite
case models.ActionCreateIssue, models.ActionCreatePullRequest: case models.ActionCreateIssue, models.ActionCreatePullRequest:
desc = strings.Join(act.GetIssueInfos(), "#") desc = strings.Join(act.GetIssueInfos(), "#")
content = act.GetIssueContent() content = renderMarkdown(ctx, act, act.GetIssueContent())
case models.ActionCommentIssue, models.ActionApprovePullRequest, models.ActionRejectPullRequest, models.ActionCommentPull: case models.ActionCommentIssue, models.ActionApprovePullRequest, models.ActionRejectPullRequest, models.ActionCommentPull:
desc = act.GetIssueTitle() desc = act.GetIssueTitle()
comment := act.GetIssueInfos()[1] comment := act.GetIssueInfos()[1]
if len(comment) != 0 { if len(comment) != 0 {
desc += "\n\n" + comment desc += "\n\n" + renderMarkdown(ctx, act, comment)
} }
case models.ActionMergePullRequest: case models.ActionMergePullRequest:
desc = act.GetIssueInfos()[1] desc = act.GetIssueInfos()[1]