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)
This commit is contained in:
parent
fa175c1694
commit
633785a5f3
65 changed files with 1096 additions and 1194 deletions
|
@ -6,16 +6,52 @@ package markup
|
|||
import (
|
||||
"context"
|
||||
"html/template"
|
||||
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
)
|
||||
|
||||
// ProcessorHelper is a helper for the rendering processors (it could be renamed to RenderHelper in the future).
|
||||
// The main purpose of this helper is to decouple some functions which are not directly available in this package.
|
||||
type ProcessorHelper struct {
|
||||
IsUsernameMentionable func(ctx context.Context, username string) bool
|
||||
type LinkType string
|
||||
|
||||
ElementDir string // the direction of the elements, eg: "ltr", "rtl", "auto", default to no direction attribute
|
||||
const (
|
||||
LinkTypeApp LinkType = "app" // the link is relative to the AppSubURL
|
||||
LinkTypeDefault LinkType = "default" // the link is relative to the default base (eg: repo link, or current ref tree path)
|
||||
LinkTypeMedia LinkType = "media" // the link should be used to access media files (images, videos)
|
||||
LinkTypeRaw LinkType = "raw" // not really useful, mainly for environment GITEA_PREFIX_RAW for external renders
|
||||
)
|
||||
|
||||
type RenderHelper interface {
|
||||
CleanUp()
|
||||
|
||||
// TODO: such dependency is not ideal. We should decouple the processors step by step.
|
||||
// It should make the render choose different processors for different purposes,
|
||||
// but not make processors to guess "is it rendering a comment or a wiki?" or "does it need to check commit ID?"
|
||||
|
||||
IsCommitIDExisting(commitID string) bool
|
||||
ResolveLink(link string, likeType LinkType) string
|
||||
}
|
||||
|
||||
// RenderHelperFuncs is used to decouple cycle-import
|
||||
// At the moment there are different packages:
|
||||
// modules/markup: basic markup rendering
|
||||
// models/renderhelper: need to access models and git repo, and models/issues needs it
|
||||
// services/markup: some real helper functions could only be provided here because it needs to access various services & templates
|
||||
type RenderHelperFuncs struct {
|
||||
IsUsernameMentionable func(ctx context.Context, username string) bool
|
||||
RenderRepoFileCodePreview func(ctx context.Context, options RenderCodePreviewOptions) (template.HTML, error)
|
||||
}
|
||||
|
||||
var DefaultProcessorHelper ProcessorHelper
|
||||
var DefaultRenderHelperFuncs *RenderHelperFuncs
|
||||
|
||||
type SimpleRenderHelper struct{}
|
||||
|
||||
func (r *SimpleRenderHelper) CleanUp() {}
|
||||
|
||||
func (r *SimpleRenderHelper) IsCommitIDExisting(commitID string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (r *SimpleRenderHelper) ResolveLink(link string, likeType LinkType) string {
|
||||
return resolveLinkRelative(context.Background(), setting.AppSubURL+"/", "", link, false)
|
||||
}
|
||||
|
||||
var _ RenderHelper = (*SimpleRenderHelper)(nil)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue