1
1
mirror of https://github.com/go-gitea/gitea synced 2025-07-22 18:28:37 +00:00

Refactor markup render system (#32589)

This PR mainly moves some code and introduces `RenderContext.WithXxx`
functions
This commit is contained in:
wxiaoguang
2024-11-22 13:48:09 +08:00
committed by GitHub
parent 81ac8d914c
commit c4e27cb27b
49 changed files with 486 additions and 626 deletions

View File

@@ -51,16 +51,14 @@ func toReleaseLink(ctx *context.Context, act *activities_model.Action) string {
// 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 *activities_model.Action, content string) template.HTML {
markdownCtx := &markup.RenderContext{
Ctx: ctx,
Links: markup.Links{
markdownCtx := markup.NewRenderContext(ctx).
WithLinks(markup.Links{
Base: act.GetRepoLink(ctx),
},
Metas: map[string]string{ // FIXME: not right here, it should use issue to compose the metas
}).
WithMetas(map[string]string{ // FIXME: not right here, it should use issue to compose the metas
"user": act.GetRepoUserName(ctx),
"repo": act.GetRepoName(ctx),
},
}
})
markdown, err := markdown.RenderString(markdownCtx, content)
if err != nil {
return templates.SanitizeHTML(content) // old code did so: use SanitizeHTML to render in tmpl
@@ -296,14 +294,13 @@ func releasesToFeedItems(ctx *context.Context, releases []*repo_model.Release) (
}
link := &feeds.Link{Href: rel.HTMLURL()}
content, err = markdown.RenderString(&markup.RenderContext{
Ctx: ctx,
Repo: rel.Repo,
Links: markup.Links{
content, err = markdown.RenderString(markup.NewRenderContext(ctx).
WithRepoFacade(rel.Repo).
WithLinks(markup.Links{
Base: rel.Repo.Link(),
},
Metas: rel.Repo.ComposeMetas(ctx),
}, rel.Note)
}).
WithMetas(rel.Repo.ComposeMetas(ctx)),
rel.Note)
if err != nil {
return nil, err
}

View File

@@ -41,13 +41,10 @@ func showUserFeed(ctx *context.Context, formatType string) {
return
}
ctxUserDescription, err := markdown.RenderString(&markup.RenderContext{
Ctx: ctx,
Links: markup.Links{
Base: ctx.ContextUser.HTMLURL(),
},
Metas: markup.ComposeSimpleDocumentMetas(),
}, ctx.ContextUser.Description)
ctxUserDescription, err := markdown.RenderString(markup.NewRenderContext(ctx).
WithLinks(markup.Links{Base: ctx.ContextUser.HTMLURL()}).
WithMetas(markup.ComposeSimpleDocumentMetas()),
ctx.ContextUser.Description)
if err != nil {
ctx.ServerError("RenderString", err)
return

View File

@@ -180,17 +180,16 @@ func prepareOrgProfileReadme(ctx *context.Context, viewRepositories bool) bool {
if bytes, err := profileReadme.GetBlobContent(setting.UI.MaxDisplayFileSize); err != nil {
log.Error("failed to GetBlobContent: %v", err)
} else {
if profileContent, err := markdown.RenderString(&markup.RenderContext{
Ctx: ctx,
GitRepo: profileGitRepo,
Links: markup.Links{
if profileContent, err := markdown.RenderString(markup.NewRenderContext(ctx).
WithGitRepo(profileGitRepo).
WithLinks(markup.Links{
// Pass repo link to markdown render for the full link of media elements.
// The profile of default branch would be shown.
Base: profileDbRepo.Link(),
BranchPath: path.Join("branch", util.PathEscapeSegments(profileDbRepo.DefaultBranch)),
},
Metas: markup.ComposeSimpleDocumentMetas(),
}, bytes); err != nil {
}).
WithMetas(markup.ComposeSimpleDocumentMetas()),
bytes); err != nil {
log.Error("failed to RenderString: %v", err)
} else {
ctx.Data["ProfileReadme"] = profileContent

View File

@@ -392,16 +392,15 @@ func Diff(ctx *context.Context) {
if err == nil {
ctx.Data["NoteCommit"] = note.Commit
ctx.Data["NoteAuthor"] = user_model.ValidateCommitWithEmail(ctx, note.Commit)
ctx.Data["NoteRendered"], err = markup.RenderCommitMessage(&markup.RenderContext{
Links: markup.Links{
ctx.Data["NoteRendered"], err = markup.RenderCommitMessage(markup.NewRenderContext(ctx).
WithLinks(markup.Links{
Base: ctx.Repo.RepoLink,
BranchPath: path.Join("commit", util.PathEscapeSegments(commitID)),
},
Metas: ctx.Repo.Repository.ComposeMetas(ctx),
GitRepo: ctx.Repo.GitRepo,
Repo: ctx.Repo.Repository,
Ctx: ctx,
}, template.HTMLEscapeString(string(charset.ToUTF8WithFallback(note.Message, charset.ConvertOpts{}))))
}).
WithMetas(ctx.Repo.Repository.ComposeMetas(ctx)).
WithGitRepo(ctx.Repo.GitRepo).
WithRepoFacade(ctx.Repo.Repository),
template.HTMLEscapeString(string(charset.ToUTF8WithFallback(note.Message, charset.ConvertOpts{}))))
if err != nil {
ctx.ServerError("RenderCommitMessage", err)
return

View File

@@ -149,7 +149,7 @@ func setCsvCompareContext(ctx *context.Context) {
return csvReader, reader, err
}
baseReader, baseBlobCloser, err := csvReaderFromCommit(&markup.RenderContext{Ctx: ctx, RelativePath: diffFile.OldName}, baseBlob)
baseReader, baseBlobCloser, err := csvReaderFromCommit(markup.NewRenderContext(ctx).WithRelativePath(diffFile.OldName), baseBlob)
if baseBlobCloser != nil {
defer baseBlobCloser.Close()
}
@@ -161,7 +161,7 @@ func setCsvCompareContext(ctx *context.Context) {
return CsvDiffResult{nil, "unable to load file"}
}
headReader, headBlobCloser, err := csvReaderFromCommit(&markup.RenderContext{Ctx: ctx, RelativePath: diffFile.Name}, headBlob)
headReader, headBlobCloser, err := csvReaderFromCommit(markup.NewRenderContext(ctx).WithRelativePath(diffFile.Name), headBlob)
if headBlobCloser != nil {
defer headBlobCloser.Close()
}

View File

@@ -366,15 +366,12 @@ func UpdateIssueContent(ctx *context.Context) {
}
}
content, err := markdown.RenderString(&markup.RenderContext{
Links: markup.Links{
Base: ctx.FormString("context"), // FIXME: <- IS THIS SAFE ?
},
Metas: ctx.Repo.Repository.ComposeMetas(ctx),
GitRepo: ctx.Repo.GitRepo,
Repo: ctx.Repo.Repository,
Ctx: ctx,
}, issue.Content)
content, err := markdown.RenderString(markup.NewRenderContext(ctx).
WithLinks(markup.Links{Base: ctx.FormString("context")}).
WithMetas(ctx.Repo.Repository.ComposeMetas(ctx)).
WithGitRepo(ctx.Repo.GitRepo).
WithRepoFacade(ctx.Repo.Repository),
issue.Content)
if err != nil {
ctx.ServerError("RenderString", err)
return

View File

@@ -267,15 +267,12 @@ func UpdateCommentContent(ctx *context.Context) {
var renderedContent template.HTML
if comment.Content != "" {
renderedContent, err = markdown.RenderString(&markup.RenderContext{
Links: markup.Links{
Base: ctx.FormString("context"), // FIXME: <- IS THIS SAFE ?
},
Metas: ctx.Repo.Repository.ComposeMetas(ctx),
GitRepo: ctx.Repo.GitRepo,
Repo: ctx.Repo.Repository,
Ctx: ctx,
}, comment.Content)
renderedContent, err = markdown.RenderString(markup.NewRenderContext(ctx).
WithLinks(markup.Links{Base: ctx.FormString("context")}).
WithMetas(ctx.Repo.Repository.ComposeMetas(ctx)).
WithGitRepo(ctx.Repo.GitRepo).
WithRepoFacade(ctx.Repo.Repository),
comment.Content)
if err != nil {
ctx.ServerError("RenderString", err)
return

View File

@@ -359,15 +359,12 @@ func ViewIssue(ctx *context.Context) {
}
}
ctx.Data["IssueWatch"] = iw
issue.RenderedContent, err = markdown.RenderString(&markup.RenderContext{
Links: markup.Links{
Base: ctx.Repo.RepoLink,
},
Metas: ctx.Repo.Repository.ComposeMetas(ctx),
GitRepo: ctx.Repo.GitRepo,
Repo: ctx.Repo.Repository,
Ctx: ctx,
}, issue.Content)
issue.RenderedContent, err = markdown.RenderString(markup.NewRenderContext(ctx).
WithLinks(markup.Links{Base: ctx.Repo.RepoLink}).
WithMetas(ctx.Repo.Repository.ComposeMetas(ctx)).
WithGitRepo(ctx.Repo.GitRepo).
WithRepoFacade(ctx.Repo.Repository),
issue.Content)
if err != nil {
ctx.ServerError("RenderString", err)
return
@@ -467,15 +464,14 @@ func ViewIssue(ctx *context.Context) {
comment.Issue = issue
if comment.Type == issues_model.CommentTypeComment || comment.Type == issues_model.CommentTypeReview {
comment.RenderedContent, err = markdown.RenderString(&markup.RenderContext{
Links: markup.Links{
comment.RenderedContent, err = markdown.RenderString(markup.NewRenderContext(ctx).
WithLinks(markup.Links{
Base: ctx.Repo.RepoLink,
},
Metas: ctx.Repo.Repository.ComposeMetas(ctx),
GitRepo: ctx.Repo.GitRepo,
Repo: ctx.Repo.Repository,
Ctx: ctx,
}, comment.Content)
}).
WithMetas(ctx.Repo.Repository.ComposeMetas(ctx)).
WithGitRepo(ctx.Repo.GitRepo).
WithRepoFacade(ctx.Repo.Repository),
comment.Content)
if err != nil {
ctx.ServerError("RenderString", err)
return
@@ -550,15 +546,12 @@ func ViewIssue(ctx *context.Context) {
}
}
} else if comment.Type.HasContentSupport() {
comment.RenderedContent, err = markdown.RenderString(&markup.RenderContext{
Links: markup.Links{
Base: ctx.Repo.RepoLink,
},
Metas: ctx.Repo.Repository.ComposeMetas(ctx),
GitRepo: ctx.Repo.GitRepo,
Repo: ctx.Repo.Repository,
Ctx: ctx,
}, comment.Content)
comment.RenderedContent, err = markdown.RenderString(markup.NewRenderContext(ctx).
WithLinks(markup.Links{Base: ctx.Repo.RepoLink}).
WithMetas(ctx.Repo.Repository.ComposeMetas(ctx)).
WithGitRepo(ctx.Repo.GitRepo).
WithRepoFacade(ctx.Repo.Repository),
comment.Content)
if err != nil {
ctx.ServerError("RenderString", err)
return

View File

@@ -79,15 +79,12 @@ func Milestones(ctx *context.Context) {
}
}
for _, m := range miles {
m.RenderedContent, err = markdown.RenderString(&markup.RenderContext{
Links: markup.Links{
Base: ctx.Repo.RepoLink,
},
Metas: ctx.Repo.Repository.ComposeMetas(ctx),
GitRepo: ctx.Repo.GitRepo,
Repo: ctx.Repo.Repository,
Ctx: ctx,
}, m.Content)
m.RenderedContent, err = markdown.RenderString(markup.NewRenderContext(ctx).
WithLinks(markup.Links{Base: ctx.Repo.RepoLink}).
WithMetas(ctx.Repo.Repository.ComposeMetas(ctx)).
WithGitRepo(ctx.Repo.GitRepo).
WithRepoFacade(ctx.Repo.Repository),
m.Content)
if err != nil {
ctx.ServerError("RenderString", err)
return
@@ -268,15 +265,12 @@ func MilestoneIssuesAndPulls(ctx *context.Context) {
return
}
milestone.RenderedContent, err = markdown.RenderString(&markup.RenderContext{
Links: markup.Links{
Base: ctx.Repo.RepoLink,
},
Metas: ctx.Repo.Repository.ComposeMetas(ctx),
GitRepo: ctx.Repo.GitRepo,
Repo: ctx.Repo.Repository,
Ctx: ctx,
}, milestone.Content)
milestone.RenderedContent, err = markdown.RenderString(markup.NewRenderContext(ctx).
WithLinks(markup.Links{Base: ctx.Repo.RepoLink}).
WithMetas(ctx.Repo.Repository.ComposeMetas(ctx)).
WithGitRepo(ctx.Repo.GitRepo).
WithRepoFacade(ctx.Repo.Repository),
milestone.Content)
if err != nil {
ctx.ServerError("RenderString", err)
return

View File

@@ -92,15 +92,12 @@ func Projects(ctx *context.Context) {
}
for i := range projects {
projects[i].RenderedContent, err = markdown.RenderString(&markup.RenderContext{
Links: markup.Links{
Base: ctx.Repo.RepoLink,
},
Metas: ctx.Repo.Repository.ComposeMetas(ctx),
GitRepo: ctx.Repo.GitRepo,
Repo: ctx.Repo.Repository,
Ctx: ctx,
}, projects[i].Description)
projects[i].RenderedContent, err = markdown.RenderString(markup.NewRenderContext(ctx).
WithLinks(markup.Links{Base: ctx.Repo.RepoLink}).
WithMetas(ctx.Repo.Repository.ComposeMetas(ctx)).
WithGitRepo(ctx.Repo.GitRepo).
WithRepoFacade(ctx.Repo.Repository),
projects[i].Description)
if err != nil {
ctx.ServerError("RenderString", err)
return
@@ -425,15 +422,12 @@ func ViewProject(ctx *context.Context) {
ctx.Data["SelectLabels"] = selectLabels
ctx.Data["AssigneeID"] = assigneeID
project.RenderedContent, err = markdown.RenderString(&markup.RenderContext{
Links: markup.Links{
Base: ctx.Repo.RepoLink,
},
Metas: ctx.Repo.Repository.ComposeMetas(ctx),
GitRepo: ctx.Repo.GitRepo,
Repo: ctx.Repo.Repository,
Ctx: ctx,
}, project.Description)
project.RenderedContent, err = markdown.RenderString(markup.NewRenderContext(ctx).
WithLinks(markup.Links{Base: ctx.Repo.RepoLink}).
WithMetas(ctx.Repo.Repository.ComposeMetas(ctx)).
WithGitRepo(ctx.Repo.GitRepo).
WithRepoFacade(ctx.Repo.Repository),
project.Description)
if err != nil {
ctx.ServerError("RenderString", err)
return

View File

@@ -114,15 +114,12 @@ func getReleaseInfos(ctx *context.Context, opts *repo_model.FindReleasesOptions)
cacheUsers[r.PublisherID] = r.Publisher
}
r.RenderedNote, err = markdown.RenderString(&markup.RenderContext{
Links: markup.Links{
Base: ctx.Repo.RepoLink,
},
Metas: ctx.Repo.Repository.ComposeMetas(ctx),
GitRepo: ctx.Repo.GitRepo,
Repo: ctx.Repo.Repository,
Ctx: ctx,
}, r.Note)
r.RenderedNote, err = markdown.RenderString(markup.NewRenderContext(ctx).
WithLinks(markup.Links{Base: ctx.Repo.RepoLink}).
WithMetas(ctx.Repo.Repository.ComposeMetas(ctx)).
WithGitRepo(ctx.Repo.GitRepo).
WithRepoFacade(ctx.Repo.Repository),
r.Note)
if err != nil {
return nil, err
}

View File

@@ -56,18 +56,17 @@ func RenderFile(ctx *context.Context) {
return
}
err = markup.Render(&markup.RenderContext{
Ctx: ctx,
RelativePath: ctx.Repo.TreePath,
Links: markup.Links{
err = markup.Render(markup.NewRenderContext(ctx).
WithRelativePath(ctx.Repo.TreePath).
WithLinks(markup.Links{
Base: ctx.Repo.RepoLink,
BranchPath: ctx.Repo.BranchNameSubURL(),
TreePath: path.Dir(ctx.Repo.TreePath),
},
Metas: ctx.Repo.Repository.ComposeDocumentMetas(ctx),
GitRepo: ctx.Repo.GitRepo,
InStandalonePage: true,
}, rd, ctx.Resp)
}).
WithMetas(ctx.Repo.Repository.ComposeDocumentMetas(ctx)).
WithGitRepo(ctx.Repo.GitRepo).
WithInStandalonePage(true),
rd, ctx.Resp)
if err != nil {
log.Error("Failed to render file %q: %v", ctx.Repo.TreePath, err)
http.Error(ctx.Resp, "Failed to render file", http.StatusInternalServerError)

View File

@@ -310,18 +310,17 @@ func renderReadmeFile(ctx *context.Context, subfolder string, readmeFile *git.Tr
ctx.Data["IsMarkup"] = true
ctx.Data["MarkupType"] = markupType
ctx.Data["EscapeStatus"], ctx.Data["FileContent"], err = markupRender(ctx, &markup.RenderContext{
Ctx: ctx,
MarkupType: markupType,
RelativePath: path.Join(ctx.Repo.TreePath, readmeFile.Name()), // ctx.Repo.TreePath is the directory not the Readme so we must append the Readme filename (and path).
Links: markup.Links{
ctx.Data["EscapeStatus"], ctx.Data["FileContent"], err = markupRender(ctx, markup.NewRenderContext(ctx).
WithMarkupType(markupType).
WithRelativePath(path.Join(ctx.Repo.TreePath, readmeFile.Name())). // ctx.Repo.TreePath is the directory not the Readme so we must append the Readme filename (and path).
WithLinks(markup.Links{
Base: ctx.Repo.RepoLink,
BranchPath: ctx.Repo.BranchNameSubURL(),
TreePath: path.Join(ctx.Repo.TreePath, subfolder),
},
Metas: ctx.Repo.Repository.ComposeDocumentMetas(ctx),
GitRepo: ctx.Repo.GitRepo,
}, rd)
}).
WithMetas(ctx.Repo.Repository.ComposeDocumentMetas(ctx)).
WithGitRepo(ctx.Repo.GitRepo),
rd)
if err != nil {
log.Error("Render failed for %s in %-v: %v Falling back to rendering source", readmeFile.Name(), ctx.Repo.Repository, err)
delete(ctx.Data, "IsMarkup")
@@ -514,18 +513,17 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry) {
ctx.Data["MarkupType"] = markupType
metas := ctx.Repo.Repository.ComposeDocumentMetas(ctx)
metas["BranchNameSubURL"] = ctx.Repo.BranchNameSubURL()
ctx.Data["EscapeStatus"], ctx.Data["FileContent"], err = markupRender(ctx, &markup.RenderContext{
Ctx: ctx,
MarkupType: markupType,
RelativePath: ctx.Repo.TreePath,
Links: markup.Links{
ctx.Data["EscapeStatus"], ctx.Data["FileContent"], err = markupRender(ctx, markup.NewRenderContext(ctx).
WithMarkupType(markupType).
WithRelativePath(ctx.Repo.TreePath).
WithLinks(markup.Links{
Base: ctx.Repo.RepoLink,
BranchPath: ctx.Repo.BranchNameSubURL(),
TreePath: path.Dir(ctx.Repo.TreePath),
},
Metas: metas,
GitRepo: ctx.Repo.GitRepo,
}, rd)
}).
WithMetas(metas).
WithGitRepo(ctx.Repo.GitRepo),
rd)
if err != nil {
ctx.ServerError("Render", err)
return
@@ -606,18 +604,17 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry) {
rd := io.MultiReader(bytes.NewReader(buf), dataRc)
ctx.Data["IsMarkup"] = true
ctx.Data["MarkupType"] = markupType
ctx.Data["EscapeStatus"], ctx.Data["FileContent"], err = markupRender(ctx, &markup.RenderContext{
Ctx: ctx,
MarkupType: markupType,
RelativePath: ctx.Repo.TreePath,
Links: markup.Links{
ctx.Data["EscapeStatus"], ctx.Data["FileContent"], err = markupRender(ctx, markup.NewRenderContext(ctx).
WithMarkupType(markupType).
WithRelativePath(ctx.Repo.TreePath).
WithLinks(markup.Links{
Base: ctx.Repo.RepoLink,
BranchPath: ctx.Repo.BranchNameSubURL(),
TreePath: path.Dir(ctx.Repo.TreePath),
},
Metas: ctx.Repo.Repository.ComposeDocumentMetas(ctx),
GitRepo: ctx.Repo.GitRepo,
}, rd)
}).
WithMetas(ctx.Repo.Repository.ComposeDocumentMetas(ctx)).
WithGitRepo(ctx.Repo.GitRepo),
rd)
if err != nil {
ctx.ServerError("Render", err)
return

View File

@@ -288,13 +288,9 @@ func renderViewPage(ctx *context.Context) (*git.Repository, *git.TreeEntry) {
footerContent = data
}
rctx := &markup.RenderContext{
Ctx: ctx,
Metas: ctx.Repo.Repository.ComposeWikiMetas(ctx),
Links: markup.Links{
Base: ctx.Repo.RepoLink,
},
}
rctx := markup.NewRenderContext(ctx).
WithMetas(ctx.Repo.Repository.ComposeWikiMetas(ctx)).
WithLinks(markup.Links{Base: ctx.Repo.RepoLink})
buf := &strings.Builder{}
renderFn := func(data []byte) (escaped *charset.EscapeStatus, output string, err error) {

View File

@@ -49,10 +49,7 @@ func PrepareContextForProfileBigAvatar(ctx *context.Context) {
}
ctx.Data["OpenIDs"] = openIDs
if len(ctx.ContextUser.Description) != 0 {
content, err := markdown.RenderString(&markup.RenderContext{
Metas: markup.ComposeSimpleDocumentMetas(),
Ctx: ctx,
}, ctx.ContextUser.Description)
content, err := markdown.RenderString(markup.NewRenderContext(ctx).WithMetas(markup.ComposeSimpleDocumentMetas()), ctx.ContextUser.Description)
if err != nil {
ctx.ServerError("RenderString", err)
return

View File

@@ -257,14 +257,11 @@ func Milestones(ctx *context.Context) {
continue
}
milestones[i].RenderedContent, err = markdown.RenderString(&markup.RenderContext{
Links: markup.Links{
Base: milestones[i].Repo.Link(),
},
Metas: milestones[i].Repo.ComposeMetas(ctx),
Ctx: ctx,
Repo: milestones[i].Repo,
}, milestones[i].Content)
milestones[i].RenderedContent, err = markdown.RenderString(markup.NewRenderContext(ctx).
WithLinks(markup.Links{Base: milestones[i].Repo.Link()}).
WithMetas(milestones[i].Repo.ComposeMetas(ctx)).
WithRepoFacade(milestones[i].Repo),
milestones[i].Content)
if err != nil {
ctx.ServerError("RenderString", err)
return

View File

@@ -246,10 +246,9 @@ func prepareUserProfileTabData(ctx *context.Context, showPrivate bool, profileDb
if bytes, err := profileReadme.GetBlobContent(setting.UI.MaxDisplayFileSize); err != nil {
log.Error("failed to GetBlobContent: %v", err)
} else {
if profileContent, err := markdown.RenderString(&markup.RenderContext{
Ctx: ctx,
GitRepo: profileGitRepo,
Links: markup.Links{
if profileContent, err := markdown.RenderString(markup.NewRenderContext(ctx).
WithGitRepo(profileGitRepo).
WithLinks(markup.Links{
// Give the repo link to the markdown render for the full link of media element.
// the media link usually be like /[user]/[repoName]/media/branch/[branchName],
// Eg. /Tom/.profile/media/branch/main
@@ -257,8 +256,8 @@ func prepareUserProfileTabData(ctx *context.Context, showPrivate bool, profileDb
// https://docs.gitea.com/usage/profile-readme
Base: profileDbRepo.Link(),
BranchPath: path.Join("branch", util.PathEscapeSegments(profileDbRepo.DefaultBranch)),
},
}, bytes); err != nil {
}),
bytes); err != nil {
log.Error("failed to RenderString: %v", err)
} else {
ctx.Data["ProfileReadme"] = profileContent