summaryrefslogtreecommitdiffstats
path: root/modules/markup/markdown/transform_link.go
diff options
context:
space:
mode:
authorDaniel Baumann <daniel@debian.org>2024-10-18 20:33:49 +0200
committerDaniel Baumann <daniel@debian.org>2024-10-18 20:33:49 +0200
commitdd136858f1ea40ad3c94191d647487fa4f31926c (patch)
tree58fec94a7b2a12510c9664b21793f1ed560c6518 /modules/markup/markdown/transform_link.go
parentInitial commit. (diff)
downloadforgejo-upstream.tar.xz
forgejo-upstream.zip
Adding upstream version 9.0.0.upstream/9.0.0upstreamdebian
Signed-off-by: Daniel Baumann <daniel@debian.org>
Diffstat (limited to 'modules/markup/markdown/transform_link.go')
-rw-r--r--modules/markup/markdown/transform_link.go46
1 files changed, 46 insertions, 0 deletions
diff --git a/modules/markup/markdown/transform_link.go b/modules/markup/markdown/transform_link.go
new file mode 100644
index 0000000..e6f3836
--- /dev/null
+++ b/modules/markup/markdown/transform_link.go
@@ -0,0 +1,46 @@
+// Copyright 2024 The Gitea Authors. All rights reserved.
+// SPDX-License-Identifier: MIT
+
+package markdown
+
+import (
+ "bytes"
+ "slices"
+
+ "code.gitea.io/gitea/modules/markup"
+ "code.gitea.io/gitea/modules/setting"
+ giteautil "code.gitea.io/gitea/modules/util"
+
+ "github.com/yuin/goldmark/ast"
+)
+
+func (g *ASTTransformer) transformLink(ctx *markup.RenderContext, v *ast.Link) {
+ // Links need their href to munged to be a real value
+ link := v.Destination
+
+ // Do not process the link if it's not a link, starts with an hashtag
+ // (indicating it's an anchor link), starts with `mailto:` or any of the
+ // custom markdown URLs.
+ processLink := len(link) > 0 && !markup.IsLink(link) &&
+ link[0] != '#' && !bytes.HasPrefix(link, byteMailto) &&
+ !slices.ContainsFunc(setting.Markdown.CustomURLSchemes, func(s string) bool {
+ return bytes.HasPrefix(link, []byte(s+":"))
+ })
+
+ if processLink {
+ var base string
+ if ctx.IsWiki {
+ base = ctx.Links.WikiLink()
+ } else if ctx.Links.HasBranchInfo() {
+ base = ctx.Links.SrcLink()
+ } else {
+ base = ctx.Links.Base
+ }
+
+ link = []byte(giteautil.URLJoin(base, string(link)))
+ }
+ if len(link) > 0 && link[0] == '#' {
+ link = []byte("#user-content-" + string(link)[1:])
+ }
+ v.Destination = link
+}