gitea源码

render_helper.go 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Copyright 2024 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package markup
  4. import (
  5. "context"
  6. "html/template"
  7. "code.gitea.io/gitea/modules/setting"
  8. )
  9. const (
  10. LinkTypeDefault = ""
  11. LinkTypeRoot = "/:root" // the link is relative to the AppSubURL(ROOT_URL)
  12. LinkTypeMedia = "/:media" // the link should be used to access media files (images, videos)
  13. LinkTypeRaw = "/:raw" // not really useful, mainly for environment GITEA_PREFIX_RAW for external renders
  14. )
  15. type RenderHelper interface {
  16. CleanUp()
  17. // TODO: such dependency is not ideal. We should decouple the processors step by step.
  18. // It should make the render choose different processors for different purposes,
  19. // but not make processors to guess "is it rendering a comment or a wiki?" or "does it need to check commit ID?"
  20. IsCommitIDExisting(commitID string) bool
  21. ResolveLink(link, preferLinkType string) string
  22. }
  23. // RenderHelperFuncs is used to decouple cycle-import
  24. // At the moment there are different packages:
  25. // modules/markup: basic markup rendering
  26. // models/renderhelper: need to access models and git repo, and models/issues needs it
  27. // services/markup: some real helper functions could only be provided here because it needs to access various services & templates
  28. type RenderHelperFuncs struct {
  29. IsUsernameMentionable func(ctx context.Context, username string) bool
  30. RenderRepoFileCodePreview func(ctx context.Context, options RenderCodePreviewOptions) (template.HTML, error)
  31. RenderRepoIssueIconTitle func(ctx context.Context, options RenderIssueIconTitleOptions) (template.HTML, error)
  32. }
  33. var DefaultRenderHelperFuncs *RenderHelperFuncs
  34. type SimpleRenderHelper struct{}
  35. func (r *SimpleRenderHelper) CleanUp() {}
  36. func (r *SimpleRenderHelper) IsCommitIDExisting(commitID string) bool {
  37. return false
  38. }
  39. func (r *SimpleRenderHelper) ResolveLink(link, preferLinkType string) string {
  40. _, link = ParseRenderedLink(link, preferLinkType)
  41. return resolveLinkRelative(context.Background(), setting.AppSubURL+"/", "", link, false)
  42. }
  43. var _ RenderHelper = (*SimpleRenderHelper)(nil)