From d29ec91e91362b0657edee631f12cf1d55ba7a2c Mon Sep 17 00:00:00 2001
From: Earl Warren <contact@earl-warren.org>
Date: Wed, 13 Dec 2023 17:38:55 +0100
Subject: [PATCH 1/2] [GITEA] Revert "Make `user-content-* ` consistent with
 github (#26388)"

Refs: https://codeberg.org/forgejo/forgejo/issues/1943

This reverts commit d41aee1d1e1cb07ccf71330dd3414beff603e48a.
---
 modules/markup/common/footnote.go      | 13 ++++--
 modules/markup/common/footnote_test.go | 60 --------------------------
 2 files changed, 9 insertions(+), 64 deletions(-)
 delete mode 100644 modules/markup/common/footnote_test.go

diff --git a/modules/markup/common/footnote.go b/modules/markup/common/footnote.go
index 4406803694..0e75e2adfd 100644
--- a/modules/markup/common/footnote.go
+++ b/modules/markup/common/footnote.go
@@ -29,12 +29,17 @@ 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 {
-		if unicode.IsLetter(r) || unicode.IsNumber(r) || r == '_' || r == '-' {
+		switch {
+		case unicode.IsLetter(r) || unicode.IsNumber(r) || r == '_':
+			if needsDash && len(result) > 0 {
+				result = append(result, '-')
+			}
+			needsDash = false
 			result = append(result, unicode.ToLower(r))
-		}
-		if unicode.IsSpace(r) {
-			result = append(result, '-')
+		default:
+			needsDash = true
 		}
 	}
 	return []byte(string(result))
diff --git a/modules/markup/common/footnote_test.go b/modules/markup/common/footnote_test.go
deleted file mode 100644
index 2327a7b14b..0000000000
--- a/modules/markup/common/footnote_test.go
+++ /dev/null
@@ -1,60 +0,0 @@
-// Copyright 2023 The Gitea Authors. All rights reserved.
-// SPDX-License-Identifier: MIT
-package common
-
-import (
-	"testing"
-
-	"github.com/stretchr/testify/assert"
-)
-
-func TestCleanValue(t *testing.T) {
-	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"},
-		{"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"},
-		{"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)
-	}
-}

From 1666fba8f577e11ea234c8a671aeaab1290cfbaf Mon Sep 17 00:00:00 2001
From: Earl Warren <contact@earl-warren.org>
Date: Wed, 13 Dec 2023 17:42:34 +0100
Subject: [PATCH 2/2] [GITEA] test markdown CleanValue to prevent regression

It will determine how anchors are created and will break existing
links otherwise.

Adapted from Revert "Make `user-content-* ` consistent with github (#26388)
---
 modules/markup/common/footnote_test.go | 62 ++++++++++++++++++++++++++
 1 file changed, 62 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 0000000000..62763c5622
--- /dev/null
+++ b/modules/markup/common/footnote_test.go
@@ -0,0 +1,62 @@
+// Copyright 2023 The Gitea Authors. All rights reserved.
+// Copyright 2023 The Forgejo Authors. All rights reserved.
+// SPDX-License-Identifier: MIT
+package common
+
+import (
+	"testing"
+
+	"github.com/stretchr/testify/assert"
+)
+
+func TestCleanValue(t *testing.T) {
+	tests := []struct {
+		param  string
+		expect string
+	}{
+		// Github behavior test cases
+		{"", ""},
+		{"test.0.1", "test-0-1"},
+		{"test(0)", "test-0"},
+		{"test!1", "test-1"},
+		{"test:2", "test-2"},
+		{"test*3", "test-3"},
+		{"test!4", "test-4"},
+		{"test:5", "test-5"},
+		{"test*6", "test-6"},
+		{"test:6 a", "test-6-a"},
+		{"test:6 !b", "test-6-b"},
+		{"test:ad # df", "test-ad-df"},
+		{"test:ad #23 df 2*/*", "test-ad-23-df-2"},
+		{"test:ad 23 df 2*/*", "test-ad-23-df-2"},
+		{"test:ad # 23 df 2*/*", "test-ad-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", "test-6a"},
+		{"test:a6", "test-a6"},
+		{"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-link-s-click"},
+		{"tes()", "tes"},
+		{"tes(0)", "tes-0"},
+		{"tes{0}", "tes-0"},
+		{"tes[0]", "tes-0"},
+		{"test【0】", "test-0"},
+		{"tes…@a", "tes-a"},
+		{"tes¥& a", "tes-a"},
+		{"tes= a", "tes-a"},
+		{"tes|a", "tes-a"},
+		{"tes\\a", "tes-a"},
+		{"tes/a", "tes-a"},
+		{"a啊啊b", "a啊啊b"},
+		{"c🤔️🤔️d", "c-d"},
+		{"a⚡a", "a-a"},
+		{"e.~f", "e-f"},
+	}
+	for _, test := range tests {
+		assert.Equal(t, []byte(test.expect), CleanValue([]byte(test.param)), test.param)
+	}
+}