mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-05-16 06:52:54 +00:00
Refactor markdown render (#30139)
Only split the file into small ones (and rename AttentionTypes to attentionTypes) (cherry picked from commit 71706126b56616750a65290460fd211b9b8449da) Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu> Conflicts: - modules/markup/markdown/goldmark.go - modules/markup/markdown/transform_blockquote.go Conflicts were resolved by favouring the Forgejo implementation: I copied the Forgejo code to the same place Gitea copied them to, and adjusted the imports accordingly. Apart from conflict resolution, this also moves `applyElementDir` from a local func in `goldmark.Transform` to a method on `*ASTTransformer`, to make it callable from the extracted functions.
This commit is contained in:
parent
7d0ea92da4
commit
f692069616
7 changed files with 358 additions and 251 deletions
47
modules/markup/markdown/transform_link.go
Normal file
47
modules/markup/markdown/transform_link.go
Normal file
|
@ -0,0 +1,47 @@
|
|||
// 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"
|
||||
"github.com/yuin/goldmark/text"
|
||||
)
|
||||
|
||||
func (g *ASTTransformer) transformLink(ctx *markup.RenderContext, v *ast.Link, reader text.Reader) {
|
||||
// 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
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue