|  | 
|  | 1 | +// Copyright 2024 The Gitea Authors. All rights reserved. | 
|  | 2 | +// SPDX-License-Identifier: MIT | 
|  | 3 | + | 
|  | 4 | +package renderhelper | 
|  | 5 | + | 
|  | 6 | +import ( | 
|  | 7 | +	"context" | 
|  | 8 | +	"fmt" | 
|  | 9 | +	"path" | 
|  | 10 | + | 
|  | 11 | +	repo_model "code.gitea.io/gitea/models/repo" | 
|  | 12 | +	"code.gitea.io/gitea/modules/markup" | 
|  | 13 | +	"code.gitea.io/gitea/modules/util" | 
|  | 14 | +) | 
|  | 15 | + | 
|  | 16 | +type RepoFile struct { | 
|  | 17 | +	ctx  *markup.RenderContext | 
|  | 18 | +	opts RepoFileOptions | 
|  | 19 | + | 
|  | 20 | +	commitChecker *commitChecker | 
|  | 21 | +	repoLink      string | 
|  | 22 | +} | 
|  | 23 | + | 
|  | 24 | +func (r *RepoFile) CleanUp() { | 
|  | 25 | +	_ = r.commitChecker.Close() | 
|  | 26 | +} | 
|  | 27 | + | 
|  | 28 | +func (r *RepoFile) IsCommitIDExisting(commitID string) bool { | 
|  | 29 | +	return r.commitChecker.IsCommitIDExisting(commitID) | 
|  | 30 | +} | 
|  | 31 | + | 
|  | 32 | +func (r *RepoFile) ResolveLink(link string, likeType markup.LinkType) string { | 
|  | 33 | +	finalLink := link | 
|  | 34 | +	switch likeType { | 
|  | 35 | +	case markup.LinkTypeApp: | 
|  | 36 | +		finalLink = r.ctx.ResolveLinkApp(link) | 
|  | 37 | +	case markup.LinkTypeDefault: | 
|  | 38 | +		finalLink = r.ctx.ResolveLinkRelative(path.Join(r.repoLink, "src", r.opts.CurrentRefPath), r.opts.CurrentTreePath, link) | 
|  | 39 | +	case markup.LinkTypeRaw: | 
|  | 40 | +		finalLink = r.ctx.ResolveLinkRelative(path.Join(r.repoLink, "raw", r.opts.CurrentRefPath), r.opts.CurrentTreePath, link) | 
|  | 41 | +	case markup.LinkTypeMedia: | 
|  | 42 | +		finalLink = r.ctx.ResolveLinkRelative(path.Join(r.repoLink, "media", r.opts.CurrentRefPath), r.opts.CurrentTreePath, link) | 
|  | 43 | +	} | 
|  | 44 | +	return finalLink | 
|  | 45 | +} | 
|  | 46 | + | 
|  | 47 | +var _ markup.RenderHelper = (*RepoFile)(nil) | 
|  | 48 | + | 
|  | 49 | +type RepoFileOptions struct { | 
|  | 50 | +	DeprecatedRepoName  string // it is only a patch for the non-standard "markup" api | 
|  | 51 | +	DeprecatedOwnerName string // it is only a patch for the non-standard "markup" api | 
|  | 52 | + | 
|  | 53 | +	CurrentRefPath  string // eg: "branch/main" | 
|  | 54 | +	CurrentTreePath string // eg: "path/to/file" in the repo | 
|  | 55 | +} | 
|  | 56 | + | 
|  | 57 | +func NewRenderContextRepoFile(ctx context.Context, repo *repo_model.Repository, opts ...RepoFileOptions) *markup.RenderContext { | 
|  | 58 | +	helper := &RepoFile{opts: util.OptionalArg(opts)} | 
|  | 59 | +	rctx := markup.NewRenderContext(ctx) | 
|  | 60 | +	helper.ctx = rctx | 
|  | 61 | +	if repo != nil { | 
|  | 62 | +		helper.repoLink = repo.Link() | 
|  | 63 | +		helper.commitChecker = newCommitChecker(ctx, repo) | 
|  | 64 | +		rctx = rctx.WithMetas(repo.ComposeDocumentMetas(ctx)) | 
|  | 65 | +	} else { | 
|  | 66 | +		// this is almost dead code, only to pass the incorrect tests | 
|  | 67 | +		helper.repoLink = fmt.Sprintf("%s/%s", helper.opts.DeprecatedOwnerName, helper.opts.DeprecatedRepoName) | 
|  | 68 | +		rctx = rctx.WithMetas(map[string]string{ | 
|  | 69 | +			"user": helper.opts.DeprecatedOwnerName, | 
|  | 70 | +			"repo": helper.opts.DeprecatedRepoName, | 
|  | 71 | + | 
|  | 72 | +			"markdownLineBreakStyle": "document", | 
|  | 73 | +		}) | 
|  | 74 | +	} | 
|  | 75 | +	rctx = rctx.WithHelper(helper) | 
|  | 76 | +	return rctx | 
|  | 77 | +} | 
0 commit comments