From 9ad2dfde94230445577e3c36303f2c304e1fa8fe Mon Sep 17 00:00:00 2001 From: CaiCandong <1290147055@qq.com> Date: Tue, 8 Aug 2023 14:24:37 +0800 Subject: [PATCH 1/6] make `user-content-*` consistent with github --- modules/markup/common/footnote.go | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/modules/markup/common/footnote.go b/modules/markup/common/footnote.go index 0e75e2adfd272..4406803694fca 100644 --- a/modules/markup/common/footnote.go +++ b/modules/markup/common/footnote.go @@ -29,17 +29,12 @@ func CleanValue(value []byte) []byte { value = bytes.TrimSpace(value) rs := bytes.Runes(value) result := make([]rune, 0, len(rs)) - needsDash := false for _, r := range rs { - switch { - case unicode.IsLetter(r) || unicode.IsNumber(r) || r == '_': - if needsDash && len(result) > 0 { - result = append(result, '-') - } - needsDash = false + if unicode.IsLetter(r) || unicode.IsNumber(r) || r == '_' || r == '-' { result = append(result, unicode.ToLower(r)) - default: - needsDash = true + } + if unicode.IsSpace(r) { + result = append(result, '-') } } return []byte(string(result)) From b9dcdbffae6d6ca933594b63105e1d4366c26778 Mon Sep 17 00:00:00 2001 From: CaiCandong <1290147055@qq.com> Date: Tue, 8 Aug 2023 14:25:14 +0800 Subject: [PATCH 2/6] add some test cases from github --- modules/markup/common/footnote_test.go | 42 ++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 modules/markup/common/footnote_test.go diff --git a/modules/markup/common/footnote_test.go b/modules/markup/common/footnote_test.go new file mode 100644 index 0000000000000..41e4f852386ce --- /dev/null +++ b/modules/markup/common/footnote_test.go @@ -0,0 +1,42 @@ +package common + +import "testing" + +func TestCleanValue(t *testing.T) { + var tests = []struct { + param string + expect string + }{ + // Github behavior test cases + {"", ""}, + {"test(0)", "test0"}, + {"test!1", "test1"}, + {"test:2", "test2"}, + {"test*3", "test3"}, + {"test!4", "test4"}, + {"test:5", "test5"}, + {"test*6", "test6"}, + {"test:6 a", "test6-a"}, + {"test:6 !b", "test6-b"}, + {"test:ad # df", "testad--df"}, + {"test:ad #23 df 2*/*", "testad-23-df-2"}, + {"test:ad 23 df 2*/*", "testad-23-df-2"}, + {"test:ad # 23 df 2*/*", "testad--23-df-2"}, + {"Anchors in Markdown", "anchors-in-markdown"}, + {"a_b_c", "a_b_c"}, + {"a-b-c", "a-b-c"}, + {"a-b-c----", "a-b-c----"}, + {"test:6a", "test6a"}, + {"test:a6", "testa6"}, + {"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"}, + {"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-links-click"}, + } + for _, test := range tests { + if got := CleanValue([]byte(test.param)); string(got) != test.expect { + t.Errorf("CleanValue(%q) = %q, want %q", test.param, got, test.expect) + } + } +} From 9528e65727c68ce86fa897eab4538fdd602b040b Mon Sep 17 00:00:00 2001 From: CaiCandong <1290147055@qq.com> Date: Tue, 8 Aug 2023 14:54:04 +0800 Subject: [PATCH 3/6] fix lint --- modules/markup/common/footnote_test.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/modules/markup/common/footnote_test.go b/modules/markup/common/footnote_test.go index 41e4f852386ce..2626fa6770b1b 100644 --- a/modules/markup/common/footnote_test.go +++ b/modules/markup/common/footnote_test.go @@ -1,9 +1,11 @@ +// Copyright 2023 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT package common import "testing" func TestCleanValue(t *testing.T) { - var tests = []struct { + tests := []struct { param string expect string }{ @@ -32,7 +34,13 @@ func TestCleanValue(t *testing.T) { {" tes a a a a ", "tes-a-a---a--a"}, {"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-links-click"}, + {"tes()", "tes"}, + {"tes…@a", "tesa"}, + {"tes¥& a", "tes-a"}, + {"tes= a", "tes-a"}, + {"tes|a", "tesa"}, + {"tes\a", "tesa"}, + {"tes/a", "tesa"}, } for _, test := range tests { if got := CleanValue([]byte(test.param)); string(got) != test.expect { From 66c5b6a475e2faeef3bde8de5367fcf16d7f6ada Mon Sep 17 00:00:00 2001 From: CaiCandong <1290147055@qq.com> Date: Tue, 8 Aug 2023 15:16:21 +0800 Subject: [PATCH 4/6] fix test --- modules/markup/common/footnote_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/markup/common/footnote_test.go b/modules/markup/common/footnote_test.go index 2626fa6770b1b..9474ef0902da8 100644 --- a/modules/markup/common/footnote_test.go +++ b/modules/markup/common/footnote_test.go @@ -39,7 +39,7 @@ func TestCleanValue(t *testing.T) { {"tes¥& a", "tes-a"}, {"tes= a", "tes-a"}, {"tes|a", "tesa"}, - {"tes\a", "tesa"}, + {"tes\\a", "tesa"}, {"tes/a", "tesa"}, } for _, test := range tests { From 8ff20a42bb543f3ea2e350b381518896386bb8b8 Mon Sep 17 00:00:00 2001 From: CaiCandong <1290147055@qq.com> Date: Tue, 8 Aug 2023 15:39:53 +0800 Subject: [PATCH 5/6] refactor test --- modules/markup/common/footnote_test.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/modules/markup/common/footnote_test.go b/modules/markup/common/footnote_test.go index 9474ef0902da8..14556695809cd 100644 --- a/modules/markup/common/footnote_test.go +++ b/modules/markup/common/footnote_test.go @@ -2,7 +2,10 @@ // SPDX-License-Identifier: MIT package common -import "testing" +import ( + "github.com/stretchr/testify/assert" + "testing" +) func TestCleanValue(t *testing.T) { tests := []struct { @@ -35,6 +38,10 @@ func TestCleanValue(t *testing.T) { {"Header with \"double quotes\"", "header-with-double-quotes"}, {"Placeholder to force scrolling on link's click", "placeholder-to-force-scrolling-on-links-click"}, {"tes()", "tes"}, + {"tes(0)", "tes0"}, + {"tes{0}", "tes0"}, + {"tes[0]", "tes0"}, + {"test【0】", "test0"}, {"tes…@a", "tesa"}, {"tes¥& a", "tes-a"}, {"tes= a", "tes-a"}, @@ -43,8 +50,6 @@ func TestCleanValue(t *testing.T) { {"tes/a", "tesa"}, } for _, test := range tests { - if got := CleanValue([]byte(test.param)); string(got) != test.expect { - t.Errorf("CleanValue(%q) = %q, want %q", test.param, got, test.expect) - } + assert.Equal(t, []byte(test.expect), CleanValue([]byte(test.param)), test.param) } } From 64f5bb1ad9bbb690b7cb890a643611f413132d0d Mon Sep 17 00:00:00 2001 From: CaiCandong <1290147055@qq.com> Date: Tue, 8 Aug 2023 16:27:31 +0800 Subject: [PATCH 6/6] fix lint --- modules/markup/common/footnote_test.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/modules/markup/common/footnote_test.go b/modules/markup/common/footnote_test.go index 14556695809cd..2327a7b14b23a 100644 --- a/modules/markup/common/footnote_test.go +++ b/modules/markup/common/footnote_test.go @@ -3,8 +3,9 @@ package common import ( - "github.com/stretchr/testify/assert" "testing" + + "github.com/stretchr/testify/assert" ) func TestCleanValue(t *testing.T) { @@ -48,6 +49,10 @@ func TestCleanValue(t *testing.T) { {"tes|a", "tesa"}, {"tes\\a", "tesa"}, {"tes/a", "tesa"}, + {"a啊啊b", "a啊啊b"}, + {"c🤔️🤔️d", "cd"}, + {"a⚡a", "aa"}, + {"e.~f", "ef"}, } for _, test := range tests { assert.Equal(t, []byte(test.expect), CleanValue([]byte(test.param)), test.param)