fix(linkify): strip trailing punctuation from auto-linked URLs

URLs at the end of a sentence followed by . , ; : ) etc. were capturing
the punctuation character as part of the URL, producing broken links
(e.g. https://example.com. instead of https://example.com).

Fix in both places where URL linkification happens:
- lib/jf2-to-as2.js linkifyUrls() — used when federating posts via AP
- lib/mastodon/routes/statuses.js processStatusContent() — used when
  creating posts via the Mastodon Client API

Both now use a replacement callback that strips trailing [.,;:!?)\]'"]
from the captured URL before inserting it into the <a> tag.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
svemagie
2026-03-24 19:56:49 +01:00
parent e319c348d0
commit bd3a623488
2 changed files with 11 additions and 2 deletions

View File

@@ -32,7 +32,12 @@ function linkifyUrls(html) {
if (!html) return html; if (!html) return html;
return html.replace( return html.replace(
/(?<![=">])(https?:\/\/[^\s<"]+)/g, /(?<![=">])(https?:\/\/[^\s<"]+)/g,
'<a href="$1">$1</a>', (_, url) => {
// Strip trailing punctuation that is almost never part of a URL
// e.g. "See https://example.com." → link to https://example.com
const clean = url.replace(/[.,;:!?)\]'"]+$/, "");
return `<a href="${clean}">${clean}</a>`;
},
); );
} }

View File

@@ -1066,7 +1066,11 @@ function processStatusContent(content, rawText) {
// Linkify bare URLs (http/https) // Linkify bare URLs (http/https)
html = html.replace( html = html.replace(
/(https?:\/\/[^\s<>"')\]]+)/g, /(https?:\/\/[^\s<>"')\]]+)/g,
'<a href="$1" rel="nofollow noopener noreferrer" target="_blank">$1</a>', (_, url) => {
// Strip trailing punctuation that is almost never part of a URL
const clean = url.replace(/[.,;:!?]+$/, "");
return `<a href="${clean}" rel="nofollow noopener noreferrer" target="_blank">${clean}</a>`;
},
); );
// Convert @user@domain mentions to profile links // Convert @user@domain mentions to profile links