code.gitea.io/gitea@v1.22.3/modules/markup/html_link.go (about)

     1  // Copyright 2024 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package markup
     5  
     6  import (
     7  	"code.gitea.io/gitea/modules/util"
     8  )
     9  
    10  func ResolveLink(ctx *RenderContext, link, userContentAnchorPrefix string) (result string, resolved bool) {
    11  	isAnchorFragment := link != "" && link[0] == '#'
    12  	if !isAnchorFragment && !IsFullURLString(link) {
    13  		linkBase := ctx.Links.Base
    14  		if ctx.IsWiki {
    15  			// no need to check if the link should be resolved as a wiki link or a wiki raw link
    16  			// just use wiki link here and it will be redirected to a wiki raw link if necessary
    17  			linkBase = ctx.Links.WikiLink()
    18  		} else if ctx.Links.BranchPath != "" || ctx.Links.TreePath != "" {
    19  			// if there is no BranchPath, then the link will be something like "/owner/repo/src/{the-file-path}"
    20  			// and then this link will be handled by the "legacy-ref" code and be redirected to the default branch like "/owner/repo/src/branch/main/{the-file-path}"
    21  			linkBase = ctx.Links.SrcLink()
    22  		}
    23  		link, resolved = util.URLJoin(linkBase, link), true
    24  	}
    25  	if isAnchorFragment && userContentAnchorPrefix != "" {
    26  		link, resolved = userContentAnchorPrefix+link[1:], true
    27  	}
    28  	return link, resolved
    29  }