From 00b24d2d9adab83322e3d508b06c974c7fb75665 Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Tue, 27 Feb 2024 11:04:12 +0100 Subject: [PATCH] Allow `'s` in mentions When mentioning a user in a comment, or a similar place, sometimes one would wish to use a possessive: `As per @user's suggestion` or somesuch. This patch modifies the `mentionPattern` used to find mentions, to allow - and then ignore - apostrophes and whatever comes after them. Signed-off-by: Gergely Nagy --- modules/references/references.go | 2 +- modules/references/references_test.go | 3 +++ modules/templates/util_render_test.go | 5 +++++ 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/modules/references/references.go b/modules/references/references.go index 775831256..fce893cf5 100644 --- a/modules/references/references.go +++ b/modules/references/references.go @@ -29,7 +29,7 @@ var ( // TODO: fix invalid linking issue // mentionPattern matches all mentions in the form of "@user" or "@org/team" - mentionPattern = regexp.MustCompile(`(?:\s|^|\(|\[)(@[0-9a-zA-Z-_]+|@[0-9a-zA-Z-_]+\/?[0-9a-zA-Z-_]+|@[0-9a-zA-Z-_][0-9a-zA-Z-_.]+\/?[0-9a-zA-Z-_.]+[0-9a-zA-Z-_])(?:\s|[:,;.?!]\s|[:,;.?!]?$|\)|\])`) + mentionPattern = regexp.MustCompile(`(?:\s|^|\(|\[)(@[0-9a-zA-Z-_]+|@[0-9a-zA-Z-_]+\/?[0-9a-zA-Z-_]+|@[0-9a-zA-Z-_][0-9a-zA-Z-_.]+\/?[0-9a-zA-Z-_.]+[0-9a-zA-Z-_])(?:'|\s|[:,;.?!]\s|[:,;.?!]?$|\)|\])`) // issueNumericPattern matches string that references to a numeric issue, e.g. #1287 issueNumericPattern = regexp.MustCompile(`(?:\s|^|\(|\[|\')([#!][0-9]+)(?:\s|$|\)|\]|[:;,.?!]\s|[:;,.?!]$)`) // issueAlphanumericPattern matches string that references to an alphanumeric issue, e.g. ABC-1234 diff --git a/modules/references/references_test.go b/modules/references/references_test.go index ba7dda80c..3f1f52401 100644 --- a/modules/references/references_test.go +++ b/modules/references/references_test.go @@ -392,6 +392,9 @@ func TestRegExp_mentionPattern(t *testing.T) { {"@gitea,", "@gitea"}, {"@gitea;", "@gitea"}, {"@gitea/team1;", "@gitea/team1"}, + {"@jess'", "@jess"}, + {"@forgejo's", "@forgejo"}, + {"Оно сломалось из-за коммитов от @jopik'а", "@jopik"}, } falseTestCases := []string{ "@ 0", diff --git a/modules/templates/util_render_test.go b/modules/templates/util_render_test.go index 8648967d3..465640e08 100644 --- a/modules/templates/util_render_test.go +++ b/modules/templates/util_render_test.go @@ -59,6 +59,11 @@ func TestMain(m *testing.M) { os.Exit(m.Run()) } +func TestApostrophesInMentions(t *testing.T) { + rendered := RenderMarkdownToHtml(context.Background(), "@mention-user's comment") + assert.EqualValues(t, "

@mention-user's comment

\n", rendered) +} + func TestRenderCommitBody(t *testing.T) { type args struct { ctx context.Context