mirror of
https://codeberg.org/davrot/forgejo.git
synced 2025-05-16 02:00:02 +02: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
59
modules/markup/markdown/prefixed_id.go
Normal file
59
modules/markup/markdown/prefixed_id.go
Normal file
|
@ -0,0 +1,59 @@
|
|||
// Copyright 2024 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package markdown
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
|
||||
"code.gitea.io/gitea/modules/container"
|
||||
"code.gitea.io/gitea/modules/markup/common"
|
||||
|
||||
"github.com/yuin/goldmark/ast"
|
||||
"github.com/yuin/goldmark/util"
|
||||
)
|
||||
|
||||
type prefixedIDs struct {
|
||||
values container.Set[string]
|
||||
}
|
||||
|
||||
// Generate generates a new element id.
|
||||
func (p *prefixedIDs) Generate(value []byte, kind ast.NodeKind) []byte {
|
||||
dft := []byte("id")
|
||||
if kind == ast.KindHeading {
|
||||
dft = []byte("heading")
|
||||
}
|
||||
return p.GenerateWithDefault(value, dft)
|
||||
}
|
||||
|
||||
// GenerateWithDefault generates a new element id.
|
||||
func (p *prefixedIDs) GenerateWithDefault(value, dft []byte) []byte {
|
||||
result := common.CleanValue(value)
|
||||
if len(result) == 0 {
|
||||
result = dft
|
||||
}
|
||||
if !bytes.HasPrefix(result, []byte("user-content-")) {
|
||||
result = append([]byte("user-content-"), result...)
|
||||
}
|
||||
if p.values.Add(util.BytesToReadOnlyString(result)) {
|
||||
return result
|
||||
}
|
||||
for i := 1; ; i++ {
|
||||
newResult := fmt.Sprintf("%s-%d", result, i)
|
||||
if p.values.Add(newResult) {
|
||||
return []byte(newResult)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Put puts a given element id to the used ids table.
|
||||
func (p *prefixedIDs) Put(value []byte) {
|
||||
p.values.Add(util.BytesToReadOnlyString(value))
|
||||
}
|
||||
|
||||
func newPrefixedIDs() *prefixedIDs {
|
||||
return &prefixedIDs{
|
||||
values: make(container.Set[string]),
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue