-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Only split the file into small ones (and rename AttentionTypes to attentionTypes)
- Loading branch information
1 parent
7fda109
commit 7170612
Showing
8 changed files
with
364 additions
and
257 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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]), | ||
} | ||
} |
Oops, something went wrong.