Merge pull request '[GITEA] Revert "Make user-content-* consistent with github (#26388)"' (#1945) from earl-warren/forgejo:wip-v1.21-markdown-anchors into v1.21/forgejo

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/1945
Reviewed-by: Gusted <gusted@noreply.codeberg.org>
This commit is contained in:
Earl Warren 2023-12-13 17:47:29 +00:00
commit a86fa739dc
2 changed files with 41 additions and 34 deletions

View file

@ -29,13 +29,18 @@ func CleanValue(value []byte) []byte {
value = bytes.TrimSpace(value) value = bytes.TrimSpace(value)
rs := bytes.Runes(value) rs := bytes.Runes(value)
result := make([]rune, 0, len(rs)) result := make([]rune, 0, len(rs))
needsDash := false
for _, r := range rs { for _, r := range rs {
if unicode.IsLetter(r) || unicode.IsNumber(r) || r == '_' || r == '-' { switch {
result = append(result, unicode.ToLower(r)) case unicode.IsLetter(r) || unicode.IsNumber(r) || r == '_':
} if needsDash && len(result) > 0 {
if unicode.IsSpace(r) {
result = append(result, '-') result = append(result, '-')
} }
needsDash = false
result = append(result, unicode.ToLower(r))
default:
needsDash = true
}
} }
return []byte(string(result)) return []byte(string(result))
} }

View file

@ -1,4 +1,5 @@
// Copyright 2023 The Gitea Authors. All rights reserved. // Copyright 2023 The Gitea Authors. All rights reserved.
// Copyright 2023 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: MIT // SPDX-License-Identifier: MIT
package common package common
@ -15,44 +16,45 @@ func TestCleanValue(t *testing.T) {
}{ }{
// Github behavior test cases // Github behavior test cases
{"", ""}, {"", ""},
{"test(0)", "test0"}, {"test.0.1", "test-0-1"},
{"test!1", "test1"}, {"test(0)", "test-0"},
{"test:2", "test2"}, {"test!1", "test-1"},
{"test*3", "test3"}, {"test:2", "test-2"},
{"test4", "test4"}, {"test*3", "test-3"},
{"test5", "test5"}, {"test4", "test-4"},
{"test*6", "test6"}, {"test5", "test-5"},
{"test6 a", "test6-a"}, {"test*6", "test-6"},
{"test6 !b", "test6-b"}, {"test6 a", "test-6-a"},
{"testad # df", "testad--df"}, {"test6 !b", "test-6-b"},
{"testad #23 df 2*/*", "testad-23-df-2"}, {"testad # df", "test-ad-df"},
{"testad 23 df 2*/*", "testad-23-df-2"}, {"testad #23 df 2*/*", "test-ad-23-df-2"},
{"testad # 23 df 2*/*", "testad--23-df-2"}, {"testad 23 df 2*/*", "test-ad-23-df-2"},
{"testad # 23 df 2*/*", "test-ad-23-df-2"},
{"Anchors in Markdown", "anchors-in-markdown"}, {"Anchors in Markdown", "anchors-in-markdown"},
{"a_b_c", "a_b_c"}, {"a_b_c", "a_b_c"},
{"a-b-c", "a-b-c"}, {"a-b-c", "a-b-c"},
{"a-b-c----", "a-b-c----"}, {"a-b-c----", "a-b-c"},
{"test6a", "test6a"}, {"test6a", "test-6a"},
{"testa6", "testa6"}, {"testa6", "test-a6"},
{"tes a a a a", "tes-a-a---a--a"}, {"tes a a a a", "tes-a-a-a-a"},
{" tes a a a a ", "tes-a-a---a--a"}, {" tes a a a a ", "tes-a-a-a-a"},
{"Header with \"double quotes\"", "header-with-double-quotes"}, {"Header with \"double quotes\"", "header-with-double-quotes"},
{"Placeholder to force scrolling on link's click", "placeholder-to-force-scrolling-on-links-click"}, {"Placeholder to force scrolling on link's click", "placeholder-to-force-scrolling-on-link-s-click"},
{"tes", "tes"}, {"tes", "tes"},
{"tes0", "tes0"}, {"tes0", "tes-0"},
{"tes{0}", "tes0"}, {"tes{0}", "tes-0"},
{"tes[0]", "tes0"}, {"tes[0]", "tes-0"},
{"test【0】", "test0"}, {"test【0】", "test-0"},
{"tes…@a", "tesa"}, {"tes…@a", "tes-a"},
{"tes¥& a", "tes-a"}, {"tes¥& a", "tes-a"},
{"tes= a", "tes-a"}, {"tes= a", "tes-a"},
{"tes|a", "tesa"}, {"tes|a", "tes-a"},
{"tes\\a", "tesa"}, {"tes\\a", "tes-a"},
{"tes/a", "tesa"}, {"tes/a", "tes-a"},
{"a啊啊b", "a啊啊b"}, {"a啊啊b", "a啊啊b"},
{"c🤔🤔d", "cd"}, {"c🤔🤔d", "c-d"},
{"a⚡a", "aa"}, {"a⚡a", "a-a"},
{"e.~f", "ef"}, {"e.~f", "e-f"},
} }
for _, test := range tests { for _, test := range tests {
assert.Equal(t, []byte(test.expect), CleanValue([]byte(test.param)), test.param) assert.Equal(t, []byte(test.expect), CleanValue([]byte(test.param)), test.param)