Skip to content

Commit 7bd1054

Browse files
committed
fix: only replace URLs in href attributes, not in link text
The link rewriting was replacing ALL occurrences of the URL, including visible link text. Now it only replaces within href="..." or href='...' attributes. Before: <a href="tracking-url">tracking-url</a> After: <a href="tracking-url">original-url</a>
1 parent 2d06d34 commit 7bd1054

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

server/src/services/analytics.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -413,10 +413,14 @@ module.exports = ({ strapi }) => ({
413413
}
414414
}
415415

416-
// Apply all replacements
416+
// Apply all replacements - ONLY in href attributes, not in link text!
417417
let result = html;
418418
for (const replacement of replacements) {
419-
result = result.replace(replacement.from, replacement.to);
419+
// Escape special regex characters in the URL
420+
const escapedFrom = replacement.from.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
421+
// Only replace URLs within href="..." or href='...' attributes
422+
const hrefRegex = new RegExp(`(href\\s*=\\s*["'])${escapedFrom}(["'])`, 'gi');
423+
result = result.replace(hrefRegex, `$1${replacement.to}$2`);
420424
}
421425

422426
if (linkCount > 0) {

0 commit comments

Comments
 (0)