Skip to content

Commit 877fa8c

Browse files
mahlzahnEarl Warren
authored andcommitted
feat(ui): fediverse handle markup via redirect server (#8185)
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/<pull request number>.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 <[email protected]> Co-authored-by: Robert Wolff <[email protected]> Co-committed-by: Robert Wolff <[email protected]>
1 parent 5e157d4 commit 877fa8c

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

modules/markup/html.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ var (
6969
// https://html.spec.whatwg.org/multipage/input.html#e-mail-state-(type%3Demail)
7070
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|$))")
7171

72+
// Fediverse handle regex (same as emailRegex but with additonal @ or !
73+
// at start)
74+
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|$))")
75+
7276
// blackfriday extensions create IDs like fn:user-content-footnote
7377
blackfridayExtRegex = regexp.MustCompile(`[^:]*:user-content-`)
7478

@@ -153,6 +157,7 @@ var defaultProcessors = []processor{
153157
issueIndexPatternProcessor,
154158
commitCrossReferencePatternProcessor,
155159
hashCurrentPatternProcessor,
160+
fediAddressProcessor,
156161
emailAddressProcessor,
157162
emojiProcessor,
158163
emojiShortCodeProcessor,
@@ -1237,6 +1242,21 @@ func hashCurrentPatternProcessor(ctx *RenderContext, node *html.Node) {
12371242
}
12381243
}
12391244

1245+
// fediAddressProcessor replaces raw fediverse handles with toolforge links
1246+
func fediAddressProcessor(ctx *RenderContext, node *html.Node) {
1247+
next := node.NextSibling
1248+
for node != nil && node != next {
1249+
m := fediRegex.FindStringSubmatchIndex(node.Data)
1250+
if m == nil {
1251+
return
1252+
}
1253+
1254+
fedihandle := node.Data[m[2]:m[3]]
1255+
replaceContent(node, m[2], m[3], createLink("https://fedirect.toolforge.org/?id="+url.QueryEscape(fedihandle), fedihandle, "fedihandle"))
1256+
node = node.NextSibling.NextSibling
1257+
}
1258+
}
1259+
12401260
// emailAddressProcessor replaces raw email addresses with a mailto: link.
12411261
func emailAddressProcessor(ctx *RenderContext, node *html.Node) {
12421262
next := node.NextSibling

modules/markup/html_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,19 @@ func TestRender_email(t *testing.T) {
307307
test(
308308
309309
310+
311+
// Test fediverse handle
312+
test(
313+
314+
`<p><a href="https://fedirect.toolforge.org/?id=%40forgejo%40floss.social" rel="nofollow">@[email protected]</a></p>`)
315+
316+
test(
317+
318+
`<p><a href="https://fedirect.toolforge.org/?id=%21forgejo%40programming.dev" rel="nofollow">[email protected]</a></p>`)
319+
320+
test(
321+
"@#&@forgejo.org",
322+
`<p><a href="https://fedirect.toolforge.org/?id=%40%23%26%40forgejo.org" rel="nofollow">@#&amp;@forgejo.org</a></p>`)
310323
}
311324

312325
func TestRender_emoji(t *testing.T) {

0 commit comments

Comments
 (0)