mirror of
				https://github.com/go-gitea/gitea.git
				synced 2025-11-03 20:36:07 +01:00 
			
		
		
		
	Close #34511 Close #34590 Add comment ID to the footnote item's id attribute to ensure uniqueness. --------- Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
		
			
				
	
	
		
			77 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			77 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
// Copyright 2024 The Gitea Authors. All rights reserved.
 | 
						|
// SPDX-License-Identifier: MIT
 | 
						|
 | 
						|
package renderhelper
 | 
						|
 | 
						|
import (
 | 
						|
	"context"
 | 
						|
	"fmt"
 | 
						|
 | 
						|
	repo_model "code.gitea.io/gitea/models/repo"
 | 
						|
	"code.gitea.io/gitea/modules/markup"
 | 
						|
	"code.gitea.io/gitea/modules/util"
 | 
						|
)
 | 
						|
 | 
						|
type RepoComment struct {
 | 
						|
	ctx  *markup.RenderContext
 | 
						|
	opts RepoCommentOptions
 | 
						|
 | 
						|
	commitChecker *commitChecker
 | 
						|
	repoLink      string
 | 
						|
}
 | 
						|
 | 
						|
func (r *RepoComment) CleanUp() {
 | 
						|
	_ = r.commitChecker.Close()
 | 
						|
}
 | 
						|
 | 
						|
func (r *RepoComment) IsCommitIDExisting(commitID string) bool {
 | 
						|
	return r.commitChecker.IsCommitIDExisting(commitID)
 | 
						|
}
 | 
						|
 | 
						|
func (r *RepoComment) ResolveLink(link, preferLinkType string) string {
 | 
						|
	linkType, link := markup.ParseRenderedLink(link, preferLinkType)
 | 
						|
	switch linkType {
 | 
						|
	case markup.LinkTypeRoot:
 | 
						|
		return r.ctx.ResolveLinkRoot(link)
 | 
						|
	default:
 | 
						|
		return r.ctx.ResolveLinkRelative(r.repoLink, r.opts.CurrentRefPath, link)
 | 
						|
	}
 | 
						|
}
 | 
						|
 | 
						|
var _ markup.RenderHelper = (*RepoComment)(nil)
 | 
						|
 | 
						|
type RepoCommentOptions struct {
 | 
						|
	DeprecatedRepoName  string // it is only a patch for the non-standard "markup" api
 | 
						|
	DeprecatedOwnerName string // it is only a patch for the non-standard "markup" api
 | 
						|
	CurrentRefPath      string // eg: "branch/main" or "commit/11223344"
 | 
						|
	FootnoteContextID   string // the extra context ID for footnotes, used to avoid conflicts with other footnotes in the same page
 | 
						|
}
 | 
						|
 | 
						|
func NewRenderContextRepoComment(ctx context.Context, repo *repo_model.Repository, opts ...RepoCommentOptions) *markup.RenderContext {
 | 
						|
	helper := &RepoComment{
 | 
						|
		repoLink: repo.Link(),
 | 
						|
		opts:     util.OptionalArg(opts),
 | 
						|
	}
 | 
						|
	rctx := markup.NewRenderContext(ctx)
 | 
						|
	helper.ctx = rctx
 | 
						|
	var metas map[string]string
 | 
						|
	if repo != nil {
 | 
						|
		helper.repoLink = repo.Link()
 | 
						|
		helper.commitChecker = newCommitChecker(ctx, repo)
 | 
						|
		metas = repo.ComposeCommentMetas(ctx)
 | 
						|
	} else {
 | 
						|
		// this is almost dead code, only to pass the incorrect tests
 | 
						|
		helper.repoLink = fmt.Sprintf("%s/%s", helper.opts.DeprecatedOwnerName, helper.opts.DeprecatedRepoName)
 | 
						|
		rctx = rctx.WithMetas(map[string]string{
 | 
						|
			"user": helper.opts.DeprecatedOwnerName,
 | 
						|
			"repo": helper.opts.DeprecatedRepoName,
 | 
						|
 | 
						|
			"markdownNewLineHardBreak":     "true",
 | 
						|
			"markupAllowShortIssuePattern": "true",
 | 
						|
		})
 | 
						|
	}
 | 
						|
	metas["footnoteContextId"] = helper.opts.FootnoteContextID
 | 
						|
	rctx = rctx.WithMetas(metas).WithHelper(helper)
 | 
						|
	return rctx
 | 
						|
}
 |