From 877fa8cec142e7b4bddfc1ed85c8dfaa3902aa47 Mon Sep 17 00:00:00 2001 From: Robert Wolff Date: Mon, 16 Jun 2025 14:55:17 +0200 Subject: [PATCH] feat(ui): fediverse handle markup via redirect server (#8185) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This implements the fediverse handle markup as discussed in https://codeberg.org/forgejo/forgejo/issues/7942#issuecomment-5152173 by adding links to the https://fedirect.toolforge.org server. It’s likely a temporary solution that will be reverted by proper federation implementation. I wasn’t sure, but because I already had the implementation ready, I put the code here. I won’t be offended if we just close it. (Also it relies on external server, that could be done configurable, but given that this is likely to be temporary it may not be worth the further implementation?) ### Tests - I added test coverage for Go changes... - [ ] in their respective `*_test.go` for unit tests. - [x] in the `tests/integration` directory if it involves interactions with a live Forgejo server. - I added test coverage for JavaScript changes... - [ ] in `web_src/js/*.test.js` if it can be unit tested. - [ ] in `tests/e2e/*.test.e2e.js` if it requires interactions with a live Forgejo server (see also the [developer guide for JavaScript testing](https://codeberg.org/forgejo/forgejo/src/branch/forgejo/tests/e2e/README.md#end-to-end-tests)). ### Documentation - [ ] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change. - [ ] I did not document these changes and I do not expect someone else to do it. ### Release notes - [ ] I do not want this change to show in the release notes. - [x] I want the title to show in the release notes with a link to this pull request. - [ ] I want the content of the `release-notes/.md` to be be used for the release notes instead of the title. Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/8185 Reviewed-by: Lucas Co-authored-by: Robert Wolff Co-committed-by: Robert Wolff --- modules/markup/html.go | 20 ++++++++++++++++++++ modules/markup/html_test.go | 13 +++++++++++++ 2 files changed, 33 insertions(+) diff --git a/modules/markup/html.go b/modules/markup/html.go index c13ebab98a..7961c5c930 100644 --- a/modules/markup/html.go +++ b/modules/markup/html.go @@ -69,6 +69,10 @@ var ( // 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|$))") + // Fediverse handle regex (same as emailRegex but with additonal @ or ! + // at start) + fediRegex = 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-`) @@ -153,6 +157,7 @@ var defaultProcessors = []processor{ issueIndexPatternProcessor, commitCrossReferencePatternProcessor, hashCurrentPatternProcessor, + fediAddressProcessor, emailAddressProcessor, emojiProcessor, emojiShortCodeProcessor, @@ -1237,6 +1242,21 @@ func hashCurrentPatternProcessor(ctx *RenderContext, node *html.Node) { } } +// fediAddressProcessor replaces raw fediverse handles with toolforge links +func fediAddressProcessor(ctx *RenderContext, node *html.Node) { + next := node.NextSibling + for node != nil && node != next { + m := fediRegex.FindStringSubmatchIndex(node.Data) + if m == nil { + return + } + + fedihandle := node.Data[m[2]:m[3]] + replaceContent(node, m[2], m[3], createLink("https://fedirect.toolforge.org/?id="+url.QueryEscape(fedihandle), fedihandle, "fedihandle")) + node = node.NextSibling.NextSibling + } +} + // emailAddressProcessor replaces raw email addresses with a mailto: link. func emailAddressProcessor(ctx *RenderContext, node *html.Node) { next := node.NextSibling diff --git a/modules/markup/html_test.go b/modules/markup/html_test.go index 9d0c40c9e8..2bc929bb04 100644 --- a/modules/markup/html_test.go +++ b/modules/markup/html_test.go @@ -307,6 +307,19 @@ func TestRender_email(t *testing.T) { test( "email@domain..com", `

email@domain..com

`) + + // Test fediverse handle + test( + "@forgejo@floss.social", + `

@forgejo@floss.social

`) + + test( + "!forgejo@programming.dev", + `

!forgejo@programming.dev

`) + + test( + "@#&@forgejo.org", + `

@#&@forgejo.org

`) } func TestRender_emoji(t *testing.T) {