Skip to content

Commit d41aee1

Browse files
authored
Make user-content-* consistent with github (#26388)
Fix #26367 Related #19745 Thanks @lazyky for providing test cases
1 parent 220f236 commit d41aee1

File tree

2 files changed

+64
-9
lines changed

2 files changed

+64
-9
lines changed

modules/markup/common/footnote.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,12 @@ func CleanValue(value []byte) []byte {
2929
value = bytes.TrimSpace(value)
3030
rs := bytes.Runes(value)
3131
result := make([]rune, 0, len(rs))
32-
needsDash := false
3332
for _, r := range rs {
34-
switch {
35-
case unicode.IsLetter(r) || unicode.IsNumber(r) || r == '_':
36-
if needsDash && len(result) > 0 {
37-
result = append(result, '-')
38-
}
39-
needsDash = false
33+
if unicode.IsLetter(r) || unicode.IsNumber(r) || r == '_' || r == '-' {
4034
result = append(result, unicode.ToLower(r))
41-
default:
42-
needsDash = true
35+
}
36+
if unicode.IsSpace(r) {
37+
result = append(result, '-')
4338
}
4439
}
4540
return []byte(string(result))
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright 2023 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
package common
4+
5+
import (
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
func TestCleanValue(t *testing.T) {
12+
tests := []struct {
13+
param string
14+
expect string
15+
}{
16+
// Github behavior test cases
17+
{"", ""},
18+
{"test(0)", "test0"},
19+
{"test!1", "test1"},
20+
{"test:2", "test2"},
21+
{"test*3", "test3"},
22+
{"test!4", "test4"},
23+
{"test:5", "test5"},
24+
{"test*6", "test6"},
25+
{"test:6 a", "test6-a"},
26+
{"test:6 !b", "test6-b"},
27+
{"test:ad # df", "testad--df"},
28+
{"test:ad #23 df 2*/*", "testad-23-df-2"},
29+
{"test:ad 23 df 2*/*", "testad-23-df-2"},
30+
{"test:ad # 23 df 2*/*", "testad--23-df-2"},
31+
{"Anchors in Markdown", "anchors-in-markdown"},
32+
{"a_b_c", "a_b_c"},
33+
{"a-b-c", "a-b-c"},
34+
{"a-b-c----", "a-b-c----"},
35+
{"test:6a", "test6a"},
36+
{"test:a6", "testa6"},
37+
{"tes a a a a", "tes-a-a---a--a"},
38+
{" tes a a a a ", "tes-a-a---a--a"},
39+
{"Header with \"double quotes\"", "header-with-double-quotes"},
40+
{"Placeholder to force scrolling on link's click", "placeholder-to-force-scrolling-on-links-click"},
41+
{"tes()", "tes"},
42+
{"tes(0)", "tes0"},
43+
{"tes{0}", "tes0"},
44+
{"tes[0]", "tes0"},
45+
{"test【0】", "test0"},
46+
{"tes…@a", "tesa"},
47+
{"tes¥& a", "tes-a"},
48+
{"tes= a", "tes-a"},
49+
{"tes|a", "tesa"},
50+
{"tes\\a", "tesa"},
51+
{"tes/a", "tesa"},
52+
{"a啊啊b", "a啊啊b"},
53+
{"c🤔️🤔️d", "cd"},
54+
{"a⚡a", "aa"},
55+
{"e.~f", "ef"},
56+
}
57+
for _, test := range tests {
58+
assert.Equal(t, []byte(test.expect), CleanValue([]byte(test.param)), test.param)
59+
}
60+
}

0 commit comments

Comments
 (0)