mirror of
https://github.com/hanzoai/forge.git
synced 2026-10-30 15:00:39 +00:00
Source files keep their SPDX-License-Identifier and Hanzo copyright line; the Gitea, Gogs and LFS Test Server copyrights they used to repeat now live once in NOTICE, alongside LICENSE. The header lint accepts any copyright line above the SPDX line, so it no longer demands an upstream one. Co-authored-by: Hanzo Dev <[email protected]>
79 lines
2.7 KiB
Go
79 lines
2.7 KiB
Go
// Copyright 2026 Hanzo AI, Inc. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package renderhelper
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"path"
|
|
|
|
repo_model "github.com/hanzoai/git/models/repo"
|
|
"github.com/hanzoai/git/modules/markup"
|
|
"github.com/hanzoai/git/modules/util"
|
|
)
|
|
|
|
type RepoFile struct {
|
|
ctx *markup.RenderContext
|
|
opts RepoFileOptions
|
|
|
|
commitChecker *commitChecker
|
|
repoLink string
|
|
}
|
|
|
|
func (r *RepoFile) CleanUp() {
|
|
_ = r.commitChecker.Close()
|
|
}
|
|
|
|
func (r *RepoFile) IsCommitIDExisting(commitID string) bool {
|
|
return r.commitChecker.IsCommitIDExisting(commitID)
|
|
}
|
|
|
|
func (r *RepoFile) ResolveLink(link, preferLinkType string) (finalLink string) {
|
|
linkType, link := markup.ParseRenderedLink(link, preferLinkType)
|
|
switch linkType {
|
|
case markup.LinkTypeRoot:
|
|
finalLink = r.ctx.ResolveLinkRoot(link)
|
|
case markup.LinkTypeRaw:
|
|
finalLink = r.ctx.ResolveLinkRelative(path.Join(r.repoLink, "raw", r.opts.CurrentRefSubURL), r.opts.CurrentTreePath, link)
|
|
case markup.LinkTypeMedia:
|
|
finalLink = r.ctx.ResolveLinkRelative(path.Join(r.repoLink, "media", r.opts.CurrentRefSubURL), r.opts.CurrentTreePath, link)
|
|
default:
|
|
finalLink = r.ctx.ResolveLinkRelative(path.Join(r.repoLink, "src", r.opts.CurrentRefSubURL), r.opts.CurrentTreePath, link)
|
|
}
|
|
return finalLink
|
|
}
|
|
|
|
var _ markup.RenderHelper = (*RepoFile)(nil)
|
|
|
|
type RepoFileOptions 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
|
|
|
|
CurrentRefSubURL string // eg: "branch/main" or "branch/my%20branch", it is a sub URL path escaped by callers
|
|
CurrentTreePath string // eg: "path/to/file" in the repo, it is the tree path without URL path escaping
|
|
}
|
|
|
|
func NewRenderContextRepoFile(ctx context.Context, repo *repo_model.Repository, opts ...RepoFileOptions) *markup.RenderContext {
|
|
helper := &RepoFile{opts: util.OptionalArg(opts)}
|
|
rctx := markup.NewRenderContext(ctx)
|
|
helper.ctx = rctx
|
|
if repo != nil {
|
|
helper.repoLink = repo.Link()
|
|
helper.commitChecker = newCommitChecker(ctx, repo)
|
|
rctx = rctx.WithMetas(repo.ComposeRepoFileMetas(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,
|
|
})
|
|
}
|
|
// External render's iframe needs this to generate correct links
|
|
// TODO: maybe need to make it access "CurrentRefSubURL" directly (but impossible at the moment due to cycle-import)
|
|
rctx.RenderOptions.Metas["RefTypeNameSubURL"] = helper.opts.CurrentRefSubURL
|
|
rctx = rctx.WithHelper(helper).WithEnableHeadingIDGeneration(true)
|
|
return rctx
|
|
}
|