diff options
author | wxiaoguang <wxiaoguang@gmail.com> | 2024-11-24 16:18:57 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-11-24 16:18:57 +0800 |
commit | 633785a5f3fe00789a6cba7cc0db1333de1e9c52 (patch) | |
tree | 2a2703ee9ed41c9caa508bd72c3fb5b52716af16 /routers/web | |
parent | fa175c16949f09757ae85db6697cec327c44cba9 (diff) | |
download | gitea-633785a5f3fe00789a6cba7cc0db1333de1e9c52.tar.gz gitea-633785a5f3fe00789a6cba7cc0db1333de1e9c52.zip |
Refactor markup render system (#32612)
This PR removes (almost) all path tricks, and introduces "renderhelper"
package.
Now we can clearly see the rendering behaviors for comment/file/wiki,
more details are in "renderhelper" tests.
Fix #31411 , fix #18592, fix #25632 and maybe more problems. (ps: fix
#32608 by the way)
Diffstat (limited to 'routers/web')
-rw-r--r-- | routers/web/feed/convert.go | 38 | ||||
-rw-r--r-- | routers/web/feed/profile.go | 7 | ||||
-rw-r--r-- | routers/web/org/home.go | 16 | ||||
-rw-r--r-- | routers/web/repo/commit.go | 12 | ||||
-rw-r--r-- | routers/web/repo/issue.go | 10 | ||||
-rw-r--r-- | routers/web/repo/issue_comment.go | 10 | ||||
-rw-r--r-- | routers/web/repo/issue_view.go | 27 | ||||
-rw-r--r-- | routers/web/repo/milestone.go | 18 | ||||
-rw-r--r-- | routers/web/repo/projects.go | 18 | ||||
-rw-r--r-- | routers/web/repo/release.go | 10 | ||||
-rw-r--r-- | routers/web/repo/render.go | 18 | ||||
-rw-r--r-- | routers/web/repo/view.go | 52 | ||||
-rw-r--r-- | routers/web/repo/wiki.go | 7 | ||||
-rw-r--r-- | routers/web/user/home.go | 9 | ||||
-rw-r--r-- | routers/web/user/profile.go | 24 |
15 files changed, 96 insertions, 180 deletions
diff --git a/routers/web/feed/convert.go b/routers/web/feed/convert.go index fad7dfdf5e..b7f849dc65 100644 --- a/routers/web/feed/convert.go +++ b/routers/web/feed/convert.go @@ -13,8 +13,8 @@ import ( "strings" activities_model "code.gitea.io/gitea/models/activities" + "code.gitea.io/gitea/models/renderhelper" repo_model "code.gitea.io/gitea/models/repo" - "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" @@ -48,22 +48,18 @@ func toReleaseLink(ctx *context.Context, act *activities_model.Action) string { return act.GetRepoAbsoluteLink(ctx) + "/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 *activities_model.Action, content string) template.HTML { - markdownCtx := markup.NewRenderContext(ctx). - WithLinks(markup.Links{ - Base: act.GetRepoLink(ctx), - }). - 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) +// renderCommentMarkdown renders the comment markdown to html +func renderCommentMarkdown(ctx *context.Context, act *activities_model.Action, content string) template.HTML { + act.LoadRepo(ctx) + if act.Repo == nil { + return "" + } + rctx := renderhelper.NewRenderContextRepoComment(ctx, act.Repo).WithUseAbsoluteLink(true) + rendered, err := markdown.RenderString(rctx, content) if err != nil { - return templates.SanitizeHTML(content) // old code did so: use SanitizeHTML to render in tmpl + return "" } - return markdown + return rendered } // feedActionsToFeedItems convert gitea's Action feed to feeds Item @@ -225,12 +221,12 @@ func feedActionsToFeedItems(ctx *context.Context, actions activities_model.Actio case activities_model.ActionCreateIssue, activities_model.ActionCreatePullRequest: desc = strings.Join(act.GetIssueInfos(), "#") - content = renderMarkdown(ctx, act, act.GetIssueContent(ctx)) + content = renderCommentMarkdown(ctx, act, act.GetIssueContent(ctx)) case activities_model.ActionCommentIssue, activities_model.ActionApprovePullRequest, activities_model.ActionRejectPullRequest, activities_model.ActionCommentPull: desc = act.GetIssueTitle(ctx) comment := act.GetIssueInfos()[1] if len(comment) != 0 { - desc += "\n\n" + string(renderMarkdown(ctx, act, comment)) + desc += "\n\n" + string(renderCommentMarkdown(ctx, act, comment)) } case activities_model.ActionMergePullRequest, activities_model.ActionAutoMergePullRequest: desc = act.GetIssueInfos()[1] @@ -294,12 +290,8 @@ func releasesToFeedItems(ctx *context.Context, releases []*repo_model.Release) ( } link := &feeds.Link{Href: rel.HTMLURL()} - content, err = markdown.RenderString(markup.NewRenderContext(ctx). - WithRepoFacade(rel.Repo). - WithLinks(markup.Links{ - Base: rel.Repo.Link(), - }). - WithMetas(rel.Repo.ComposeMetas(ctx)), + rctx := renderhelper.NewRenderContextRepoComment(ctx, rel.Repo).WithUseAbsoluteLink(true) + content, err = markdown.RenderString(rctx, rel.Note) if err != nil { return nil, err diff --git a/routers/web/feed/profile.go b/routers/web/feed/profile.go index 7c4864b45e..47de7c089d 100644 --- a/routers/web/feed/profile.go +++ b/routers/web/feed/profile.go @@ -7,7 +7,7 @@ import ( "time" activities_model "code.gitea.io/gitea/models/activities" - "code.gitea.io/gitea/modules/markup" + "code.gitea.io/gitea/models/renderhelper" "code.gitea.io/gitea/modules/markup/markdown" "code.gitea.io/gitea/services/context" @@ -41,9 +41,8 @@ func showUserFeed(ctx *context.Context, formatType string) { return } - ctxUserDescription, err := markdown.RenderString(markup.NewRenderContext(ctx). - WithLinks(markup.Links{Base: ctx.ContextUser.HTMLURL()}). - WithMetas(markup.ComposeSimpleDocumentMetas()), + rctx := renderhelper.NewRenderContextSimpleDocument(ctx, ctx.ContextUser.HTMLURL()) + ctxUserDescription, err := markdown.RenderString(rctx, ctx.ContextUser.Description) if err != nil { ctx.ServerError("RenderString", err) diff --git a/routers/web/org/home.go b/routers/web/org/home.go index d0ac82b1b0..f02c08ae76 100644 --- a/routers/web/org/home.go +++ b/routers/web/org/home.go @@ -11,10 +11,10 @@ import ( "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/organization" + "code.gitea.io/gitea/models/renderhelper" repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/log" - "code.gitea.io/gitea/modules/markup" "code.gitea.io/gitea/modules/markup/markdown" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/util" @@ -180,16 +180,10 @@ 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.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)), - }). - WithMetas(markup.ComposeSimpleDocumentMetas()), - bytes); err != nil { + rctx := renderhelper.NewRenderContextRepoFile(ctx, profileDbRepo, renderhelper.RepoFileOptions{ + CurrentRefPath: path.Join("branch", util.PathEscapeSegments(profileDbRepo.DefaultBranch)), + }) + if profileContent, err := markdown.RenderString(rctx, bytes); err != nil { log.Error("failed to RenderString: %v", err) } else { ctx.Data["ProfileReadme"] = profileContent diff --git a/routers/web/repo/commit.go b/routers/web/repo/commit.go index 87b1f9019a..0be9689c3f 100644 --- a/routers/web/repo/commit.go +++ b/routers/web/repo/commit.go @@ -15,6 +15,7 @@ import ( asymkey_model "code.gitea.io/gitea/models/asymkey" "code.gitea.io/gitea/models/db" git_model "code.gitea.io/gitea/models/git" + "code.gitea.io/gitea/models/renderhelper" repo_model "code.gitea.io/gitea/models/repo" unit_model "code.gitea.io/gitea/models/unit" user_model "code.gitea.io/gitea/models/user" @@ -392,15 +393,8 @@ 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.NewRenderContext(ctx). - WithLinks(markup.Links{ - Base: ctx.Repo.RepoLink, - BranchPath: path.Join("commit", util.PathEscapeSegments(commitID)), - }). - WithMetas(ctx.Repo.Repository.ComposeMetas(ctx)). - WithGitRepo(ctx.Repo.GitRepo). - WithRepoFacade(ctx.Repo.Repository), - template.HTMLEscapeString(string(charset.ToUTF8WithFallback(note.Message, charset.ConvertOpts{})))) + rctx := renderhelper.NewRenderContextRepoComment(ctx, ctx.Repo.Repository, renderhelper.RepoCommentOptions{CurrentRefPath: path.Join("commit", util.PathEscapeSegments(commitID))}) + ctx.Data["NoteRendered"], err = markup.RenderCommitMessage(rctx, template.HTMLEscapeString(string(charset.ToUTF8WithFallback(note.Message, charset.ConvertOpts{})))) if err != nil { ctx.ServerError("RenderCommitMessage", err) return diff --git a/routers/web/repo/issue.go b/routers/web/repo/issue.go index d52dbf3939..415f34d1fb 100644 --- a/routers/web/repo/issue.go +++ b/routers/web/repo/issue.go @@ -18,12 +18,12 @@ import ( "code.gitea.io/gitea/models/organization" access_model "code.gitea.io/gitea/models/perm/access" project_model "code.gitea.io/gitea/models/project" + "code.gitea.io/gitea/models/renderhelper" repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/models/unit" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/log" - "code.gitea.io/gitea/modules/markup" "code.gitea.io/gitea/modules/markup/markdown" "code.gitea.io/gitea/modules/optional" api "code.gitea.io/gitea/modules/structs" @@ -366,12 +366,8 @@ func UpdateIssueContent(ctx *context.Context) { } } - 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) + rctx := renderhelper.NewRenderContextRepoComment(ctx, ctx.Repo.Repository) + content, err := markdown.RenderString(rctx, issue.Content) if err != nil { ctx.ServerError("RenderString", err) return diff --git a/routers/web/repo/issue_comment.go b/routers/web/repo/issue_comment.go index 33105d67ca..6b7b29d9d7 100644 --- a/routers/web/repo/issue_comment.go +++ b/routers/web/repo/issue_comment.go @@ -10,10 +10,10 @@ import ( "net/http" issues_model "code.gitea.io/gitea/models/issues" + "code.gitea.io/gitea/models/renderhelper" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" - "code.gitea.io/gitea/modules/markup" "code.gitea.io/gitea/modules/markup/markdown" repo_module "code.gitea.io/gitea/modules/repository" "code.gitea.io/gitea/modules/setting" @@ -267,12 +267,8 @@ func UpdateCommentContent(ctx *context.Context) { var renderedContent template.HTML if 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) + rctx := renderhelper.NewRenderContextRepoComment(ctx, ctx.Repo.Repository) + renderedContent, err = markdown.RenderString(rctx, comment.Content) if err != nil { ctx.ServerError("RenderString", err) return diff --git a/routers/web/repo/issue_view.go b/routers/web/repo/issue_view.go index 55d36cfefa..54ff36db49 100644 --- a/routers/web/repo/issue_view.go +++ b/routers/web/repo/issue_view.go @@ -19,6 +19,7 @@ import ( access_model "code.gitea.io/gitea/models/perm/access" project_model "code.gitea.io/gitea/models/project" pull_model "code.gitea.io/gitea/models/pull" + "code.gitea.io/gitea/models/renderhelper" repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/models/unit" user_model "code.gitea.io/gitea/models/user" @@ -359,12 +360,8 @@ func ViewIssue(ctx *context.Context) { } } ctx.Data["IssueWatch"] = iw - 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) + rctx := renderhelper.NewRenderContextRepoComment(ctx, ctx.Repo.Repository) + issue.RenderedContent, err = markdown.RenderString(rctx, issue.Content) if err != nil { ctx.ServerError("RenderString", err) return @@ -464,14 +461,8 @@ 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.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) + rctx = renderhelper.NewRenderContextRepoComment(ctx, repo) + comment.RenderedContent, err = markdown.RenderString(rctx, comment.Content) if err != nil { ctx.ServerError("RenderString", err) return @@ -546,12 +537,8 @@ func ViewIssue(ctx *context.Context) { } } } else if comment.Type.HasContentSupport() { - 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) + rctx = renderhelper.NewRenderContextRepoComment(ctx, repo) + comment.RenderedContent, err = markdown.RenderString(rctx, comment.Content) if err != nil { ctx.ServerError("RenderString", err) return diff --git a/routers/web/repo/milestone.go b/routers/web/repo/milestone.go index 7361fe66bc..3afdcfad8b 100644 --- a/routers/web/repo/milestone.go +++ b/routers/web/repo/milestone.go @@ -10,8 +10,8 @@ import ( "code.gitea.io/gitea/models/db" issues_model "code.gitea.io/gitea/models/issues" + "code.gitea.io/gitea/models/renderhelper" "code.gitea.io/gitea/modules/base" - "code.gitea.io/gitea/modules/markup" "code.gitea.io/gitea/modules/markup/markdown" "code.gitea.io/gitea/modules/optional" "code.gitea.io/gitea/modules/setting" @@ -79,12 +79,8 @@ func Milestones(ctx *context.Context) { } } for _, m := range miles { - 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) + rctx := renderhelper.NewRenderContextRepoComment(ctx, ctx.Repo.Repository) + m.RenderedContent, err = markdown.RenderString(rctx, m.Content) if err != nil { ctx.ServerError("RenderString", err) return @@ -265,12 +261,8 @@ func MilestoneIssuesAndPulls(ctx *context.Context) { return } - 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) + rctx := renderhelper.NewRenderContextRepoComment(ctx, ctx.Repo.Repository) + milestone.RenderedContent, err = markdown.RenderString(rctx, milestone.Content) if err != nil { ctx.ServerError("RenderString", err) return diff --git a/routers/web/repo/projects.go b/routers/web/repo/projects.go index cce13df3be..799ce3ad80 100644 --- a/routers/web/repo/projects.go +++ b/routers/web/repo/projects.go @@ -13,11 +13,11 @@ import ( issues_model "code.gitea.io/gitea/models/issues" "code.gitea.io/gitea/models/perm" project_model "code.gitea.io/gitea/models/project" + "code.gitea.io/gitea/models/renderhelper" repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/models/unit" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/json" - "code.gitea.io/gitea/modules/markup" "code.gitea.io/gitea/modules/markup/markdown" "code.gitea.io/gitea/modules/optional" "code.gitea.io/gitea/modules/setting" @@ -92,12 +92,8 @@ func Projects(ctx *context.Context) { } for i := range projects { - 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) + rctx := renderhelper.NewRenderContextRepoComment(ctx, repo) + projects[i].RenderedContent, err = markdown.RenderString(rctx, projects[i].Description) if err != nil { ctx.ServerError("RenderString", err) return @@ -422,12 +418,8 @@ func ViewProject(ctx *context.Context) { ctx.Data["SelectLabels"] = selectLabels ctx.Data["AssigneeID"] = assigneeID - 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) + rctx := renderhelper.NewRenderContextRepoComment(ctx, ctx.Repo.Repository) + project.RenderedContent, err = markdown.RenderString(rctx, project.Description) if err != nil { ctx.ServerError("RenderString", err) return diff --git a/routers/web/repo/release.go b/routers/web/repo/release.go index 1b5305a90d..96c512dd3d 100644 --- a/routers/web/repo/release.go +++ b/routers/web/repo/release.go @@ -13,13 +13,13 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" git_model "code.gitea.io/gitea/models/git" + "code.gitea.io/gitea/models/renderhelper" repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/models/unit" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" - "code.gitea.io/gitea/modules/markup" "code.gitea.io/gitea/modules/markup/markdown" "code.gitea.io/gitea/modules/optional" "code.gitea.io/gitea/modules/setting" @@ -114,12 +114,8 @@ func getReleaseInfos(ctx *context.Context, opts *repo_model.FindReleasesOptions) cacheUsers[r.PublisherID] = r.Publisher } - 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) + rctx := renderhelper.NewRenderContextRepoComment(ctx, r.Repo) + r.RenderedNote, err = markdown.RenderString(rctx, r.Note) if err != nil { return nil, err } diff --git a/routers/web/repo/render.go b/routers/web/repo/render.go index c551e44f46..856425ae35 100644 --- a/routers/web/repo/render.go +++ b/routers/web/repo/render.go @@ -9,6 +9,7 @@ import ( "net/http" "path" + "code.gitea.io/gitea/models/renderhelper" "code.gitea.io/gitea/modules/charset" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" @@ -56,17 +57,12 @@ func RenderFile(ctx *context.Context) { return } - 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), - }). - WithMetas(ctx.Repo.Repository.ComposeDocumentMetas(ctx)). - WithGitRepo(ctx.Repo.GitRepo). - WithInStandalonePage(true), - rd, ctx.Resp) + rctx := renderhelper.NewRenderContextRepoFile(ctx, ctx.Repo.Repository, renderhelper.RepoFileOptions{ + CurrentRefPath: ctx.Repo.BranchNameSubURL(), + CurrentTreePath: path.Dir(ctx.Repo.TreePath), + }).WithRelativePath(ctx.Repo.TreePath).WithInStandalonePage(true) + + err = markup.Render(rctx, 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) diff --git a/routers/web/repo/view.go b/routers/web/repo/view.go index ec2ddfd79f..e6c25d75e9 100644 --- a/routers/web/repo/view.go +++ b/routers/web/repo/view.go @@ -31,6 +31,7 @@ import ( git_model "code.gitea.io/gitea/models/git" issue_model "code.gitea.io/gitea/models/issues" access_model "code.gitea.io/gitea/models/perm/access" + "code.gitea.io/gitea/models/renderhelper" repo_model "code.gitea.io/gitea/models/repo" unit_model "code.gitea.io/gitea/models/unit" user_model "code.gitea.io/gitea/models/user" @@ -310,17 +311,14 @@ 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.NewRenderContext(ctx). + rctx := renderhelper.NewRenderContextRepoFile(ctx, ctx.Repo.Repository, renderhelper.RepoFileOptions{ + CurrentRefPath: ctx.Repo.BranchNameSubURL(), + CurrentTreePath: path.Join(ctx.Repo.TreePath, subfolder), + }). 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), - }). - WithMetas(ctx.Repo.Repository.ComposeDocumentMetas(ctx)). - WithGitRepo(ctx.Repo.GitRepo), - rd) + WithRelativePath(path.Join(ctx.Repo.TreePath, subfolder, readmeFile.Name())) // ctx.Repo.TreePath is the directory not the Readme so we must append the Readme filename (and path). + + ctx.Data["EscapeStatus"], ctx.Data["FileContent"], err = markupRender(ctx, rctx, 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") @@ -513,17 +511,15 @@ 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.NewRenderContext(ctx). + rctx := renderhelper.NewRenderContextRepoFile(ctx, ctx.Repo.Repository, renderhelper.RepoFileOptions{ + CurrentRefPath: ctx.Repo.BranchNameSubURL(), + CurrentTreePath: path.Dir(ctx.Repo.TreePath), + }). WithMarkupType(markupType). WithRelativePath(ctx.Repo.TreePath). - WithLinks(markup.Links{ - Base: ctx.Repo.RepoLink, - BranchPath: ctx.Repo.BranchNameSubURL(), - TreePath: path.Dir(ctx.Repo.TreePath), - }). - WithMetas(metas). - WithGitRepo(ctx.Repo.GitRepo), - rd) + WithMetas(metas) + + ctx.Data["EscapeStatus"], ctx.Data["FileContent"], err = markupRender(ctx, rctx, rd) if err != nil { ctx.ServerError("Render", err) return @@ -604,17 +600,15 @@ 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.NewRenderContext(ctx). + + rctx := renderhelper.NewRenderContextRepoFile(ctx, ctx.Repo.Repository, renderhelper.RepoFileOptions{ + CurrentRefPath: ctx.Repo.BranchNameSubURL(), + CurrentTreePath: path.Dir(ctx.Repo.TreePath), + }). WithMarkupType(markupType). - WithRelativePath(ctx.Repo.TreePath). - WithLinks(markup.Links{ - Base: ctx.Repo.RepoLink, - BranchPath: ctx.Repo.BranchNameSubURL(), - TreePath: path.Dir(ctx.Repo.TreePath), - }). - WithMetas(ctx.Repo.Repository.ComposeDocumentMetas(ctx)). - WithGitRepo(ctx.Repo.GitRepo), - rd) + WithRelativePath(ctx.Repo.TreePath) + + ctx.Data["EscapeStatus"], ctx.Data["FileContent"], err = markupRender(ctx, rctx, rd) if err != nil { ctx.ServerError("Render", err) return diff --git a/routers/web/repo/wiki.go b/routers/web/repo/wiki.go index eda3320ff0..b2dd846faf 100644 --- a/routers/web/repo/wiki.go +++ b/routers/web/repo/wiki.go @@ -14,6 +14,7 @@ import ( "strings" git_model "code.gitea.io/gitea/models/git" + "code.gitea.io/gitea/models/renderhelper" repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/models/unit" "code.gitea.io/gitea/modules/base" @@ -288,11 +289,9 @@ func renderViewPage(ctx *context.Context) (*git.Repository, *git.TreeEntry) { footerContent = data } - rctx := markup.NewRenderContext(ctx). - WithMetas(ctx.Repo.Repository.ComposeWikiMetas(ctx)). - WithLinks(markup.Links{Base: ctx.Repo.RepoLink}) - buf := &strings.Builder{} + rctx := renderhelper.NewRenderContextRepoWiki(ctx, ctx.Repo.Repository) + buf := &strings.Builder{} renderFn := func(data []byte) (escaped *charset.EscapeStatus, output string, err error) { markupRd, markupWr := io.Pipe() defer markupWr.Close() diff --git a/routers/web/user/home.go b/routers/web/user/home.go index 0bd0371f14..6149ccb08d 100644 --- a/routers/web/user/home.go +++ b/routers/web/user/home.go @@ -20,6 +20,7 @@ import ( git_model "code.gitea.io/gitea/models/git" issues_model "code.gitea.io/gitea/models/issues" "code.gitea.io/gitea/models/organization" + "code.gitea.io/gitea/models/renderhelper" repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/models/unit" user_model "code.gitea.io/gitea/models/user" @@ -27,7 +28,6 @@ import ( "code.gitea.io/gitea/modules/container" issue_indexer "code.gitea.io/gitea/modules/indexer/issues" "code.gitea.io/gitea/modules/log" - "code.gitea.io/gitea/modules/markup" "code.gitea.io/gitea/modules/markup/markdown" "code.gitea.io/gitea/modules/optional" "code.gitea.io/gitea/modules/setting" @@ -257,11 +257,8 @@ func Milestones(ctx *context.Context) { continue } - 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) + rctx := renderhelper.NewRenderContextRepoComment(ctx, milestones[i].Repo) + milestones[i].RenderedContent, err = markdown.RenderString(rctx, milestones[i].Content) if err != nil { ctx.ServerError("RenderString", err) return diff --git a/routers/web/user/profile.go b/routers/web/user/profile.go index 2c9487bbc0..931af0a283 100644 --- a/routers/web/user/profile.go +++ b/routers/web/user/profile.go @@ -12,12 +12,12 @@ import ( activities_model "code.gitea.io/gitea/models/activities" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/renderhelper" repo_model "code.gitea.io/gitea/models/repo" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" - "code.gitea.io/gitea/modules/markup" "code.gitea.io/gitea/modules/markup/markdown" "code.gitea.io/gitea/modules/optional" "code.gitea.io/gitea/modules/setting" @@ -72,17 +72,17 @@ func userProfile(ctx *context.Context) { ctx.Data["HeatmapTotalContributions"] = activities_model.GetTotalContributionsInHeatmap(data) } - profileDbRepo, profileGitRepo, profileReadmeBlob, profileClose := shared_user.FindUserProfileReadme(ctx, ctx.Doer) + profileDbRepo, _ /*profileGitRepo*/, profileReadmeBlob, profileClose := shared_user.FindUserProfileReadme(ctx, ctx.Doer) defer profileClose() showPrivate := ctx.IsSigned && (ctx.Doer.IsAdmin || ctx.Doer.ID == ctx.ContextUser.ID) - prepareUserProfileTabData(ctx, showPrivate, profileDbRepo, profileGitRepo, profileReadmeBlob) + prepareUserProfileTabData(ctx, showPrivate, profileDbRepo, profileReadmeBlob) // call PrepareContextForProfileBigAvatar later to avoid re-querying the NumFollowers & NumFollowing shared_user.PrepareContextForProfileBigAvatar(ctx) ctx.HTML(http.StatusOK, tplProfile) } -func prepareUserProfileTabData(ctx *context.Context, showPrivate bool, profileDbRepo *repo_model.Repository, profileGitRepo *git.Repository, profileReadme *git.Blob) { +func prepareUserProfileTabData(ctx *context.Context, showPrivate bool, profileDbRepo *repo_model.Repository, profileReadme *git.Blob) { // if there is a profile readme, default to "overview" page, otherwise, default to "repositories" page // if there is not a profile readme, the overview tab should be treated as the repositories tab tab := ctx.FormString("tab") @@ -246,18 +246,10 @@ 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.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 - // The branch shown on the profile page is the default branch, this need to be in sync with doc, see: - // https://docs.gitea.com/usage/profile-readme - Base: profileDbRepo.Link(), - BranchPath: path.Join("branch", util.PathEscapeSegments(profileDbRepo.DefaultBranch)), - }), - bytes); err != nil { + rctx := renderhelper.NewRenderContextRepoFile(ctx, profileDbRepo, renderhelper.RepoFileOptions{ + CurrentRefPath: path.Join("branch", util.PathEscapeSegments(profileDbRepo.DefaultBranch)), + }) + if profileContent, err := markdown.RenderString(rctx, bytes); err != nil { log.Error("failed to RenderString: %v", err) } else { ctx.Data["ProfileReadme"] = profileContent |