mirror of
				https://github.com/go-gitea/gitea
				synced 2025-11-04 13:28:25 +00:00 
			
		
		
		
	* Fix #33972 * Use consistent path resolving for links and medias. * No need to make the markup renders to resolve the paths, instead, the paths are all correctly resolved in the "post process" step. * Fix #33274 * Since 1.23, all paths starting with "/" are relative to current render context (for example: the current repo branch) * Introduce `/:root/path-relative-to-root`, then the path will be rendered as relative to "ROOT_URL"
		
			
				
	
	
		
			58 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			58 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
// Copyright 2024 The Gitea Authors. All rights reserved.
 | 
						|
// SPDX-License-Identifier: MIT
 | 
						|
 | 
						|
package markup
 | 
						|
 | 
						|
import (
 | 
						|
	"context"
 | 
						|
	"html/template"
 | 
						|
 | 
						|
	"code.gitea.io/gitea/modules/setting"
 | 
						|
)
 | 
						|
 | 
						|
const (
 | 
						|
	LinkTypeDefault = ""
 | 
						|
	LinkTypeRoot    = "/:root"  // the link is relative to the AppSubURL(ROOT_URL)
 | 
						|
	LinkTypeMedia   = "/:media" // the link should be used to access media files (images, videos)
 | 
						|
	LinkTypeRaw     = "/: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, preferLinkType string) 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)
 | 
						|
	RenderRepoIssueIconTitle  func(ctx context.Context, options RenderIssueIconTitleOptions) (template.HTML, error)
 | 
						|
}
 | 
						|
 | 
						|
var DefaultRenderHelperFuncs *RenderHelperFuncs
 | 
						|
 | 
						|
type SimpleRenderHelper struct{}
 | 
						|
 | 
						|
func (r *SimpleRenderHelper) CleanUp() {}
 | 
						|
 | 
						|
func (r *SimpleRenderHelper) IsCommitIDExisting(commitID string) bool {
 | 
						|
	return false
 | 
						|
}
 | 
						|
 | 
						|
func (r *SimpleRenderHelper) ResolveLink(link, preferLinkType string) string {
 | 
						|
	_, link = ParseRenderedLink(link, preferLinkType)
 | 
						|
	return resolveLinkRelative(context.Background(), setting.AppSubURL+"/", "", link, false)
 | 
						|
}
 | 
						|
 | 
						|
var _ RenderHelper = (*SimpleRenderHelper)(nil)
 |