From 1c0566f66dcb35b33b0b308356e1b16a26ea906b Mon Sep 17 00:00:00 2001 From: Yarden Shoham <git@yardenshoham.com> Date: Sat, 11 Nov 2023 06:08:19 +0200 Subject: [PATCH] Render email addresses as such if followed by punctuation (#27987) Added the following characters to the regular expression for the email: - , - ; - ? - ! Also added a test case. - Fixes #27616 # Before  # After  Signed-off-by: Yarden Shoham <git@yardenshoham.com> --- modules/markup/html.go | 2 +- modules/markup/html_test.go | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/modules/markup/html.go b/modules/markup/html.go index e53ccc6a79..774cbe1557 100644 --- a/modules/markup/html.go +++ b/modules/markup/html.go @@ -66,7 +66,7 @@ var ( // well as the HTML5 spec: // http://spec.commonmark.org/0.28/#email-address // https://html.spec.whatwg.org/multipage/input.html#e-mail-state-(type%3Demail) - emailRegex = regexp.MustCompile("(?:\\s|^|\\(|\\[)([a-zA-Z0-9.!#$%&'*+\\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9]{2,}(?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+)(?:\\s|$|\\)|\\]|\\.(\\s|$))") + emailRegex = regexp.MustCompile("(?:\\s|^|\\(|\\[)([a-zA-Z0-9.!#$%&'*+\\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9]{2,}(?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+)(?:\\s|$|\\)|\\]|;|,|\\?|!|\\.(\\s|$))") // blackfriday extensions create IDs like fn:user-content-footnote blackfridayExtRegex = regexp.MustCompile(`[^:]*:user-content-`) diff --git a/modules/markup/html_test.go b/modules/markup/html_test.go index 21bfc8314a..d1f4e9e8a3 100644 --- a/modules/markup/html_test.go +++ b/modules/markup/html_test.go @@ -264,6 +264,18 @@ func TestRender_email(t *testing.T) { "send email to info@gitea.co.uk.", `<p>send email to <a href="mailto:info@gitea.co.uk" rel="nofollow">info@gitea.co.uk</a>.</p>`) + test( + `j.doe@example.com, + j.doe@example.com. + j.doe@example.com; + j.doe@example.com? + j.doe@example.com!`, + `<p><a href="mailto:j.doe@example.com" rel="nofollow">j.doe@example.com</a>,<br/> +<a href="mailto:j.doe@example.com" rel="nofollow">j.doe@example.com</a>.<br/> +<a href="mailto:j.doe@example.com" rel="nofollow">j.doe@example.com</a>;<br/> +<a href="mailto:j.doe@example.com" rel="nofollow">j.doe@example.com</a>?<br/> +<a href="mailto:j.doe@example.com" rel="nofollow">j.doe@example.com</a>!</p>`) + // Test that should *not* be turned into email links test( "\"info@gitea.com\"",