Skip to content

Commit f61a039

Browse files
Fix mirrored link rewriting for TanStack examples (#4320)
### Motivation - Mirrored example pages could incorrectly rewrite links pointing to a different origin into internal marketplace routes, producing wrong "mirrored from" URLs for TanStack examples. - The intent is to preserve external-origin links (e.g. `github.com`) and only rewrite links that are on the same origin as the mirrored source (e.g. `raw.githubusercontent.com`). ### Description - In `packages/website-v2/src/marketplace/marketplaceData.ts` compute the page `baseOrigin` and added a `getUrlOrigin` helper. - In `resolveHtmlAssetLinks` skip mapping an `href` to a marketplace route when the resolved link's origin differs from the page origin and return the original resolved URL instead. - Added a unit test in `packages/website-v2/src/marketplace/marketplaceData.test.ts` that asserts links pointing to a different origin are not rewritten to internal routes. ### Testing - Added a unit test `does not rewrite links that point to a different origin` in `packages/website-v2/src/marketplace/marketplaceData.test.ts` to cover the new behavior. - No automated test suite was executed as part of this change (tests added but not run). ------ [Codex Task](https://chatgpt.com/codex/tasks/task_e_6973be5762848326946f723cbaae5cb2) <!-- CURSOR_SUMMARY --> --- > [!NOTE] > Ensures mirrored pages only rewrite links on the same origin as the source, preventing incorrect internal routing for external links (e.g., GitHub). > > - In `resolveHtmlAssetLinks`, compute `baseOrigin` and compare with `targetOrigin`; if origins differ, keep the resolved external URL > - Added `getUrlOrigin` helper to safely extract URL origins > - Added unit test `does not rewrite links that point to a different origin` in `marketplaceData.test.ts` verifying external links remain unchanged > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit 3fffdea. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY -->
1 parent 356ae43 commit f61a039

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

packages/website-v2/src/marketplace/marketplaceData.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,4 +200,23 @@ describe("resolveHtmlAssetLinks", () => {
200200
'href="https://raw.githubusercontent.com/opral/paraglide-js/refs/heads/main/docs/assets/og.png"',
201201
);
202202
});
203+
204+
it("does not rewrite links that point to a different origin", () => {
205+
const baseUrl =
206+
"https://raw.githubusercontent.com/TanStack/router/main/examples/react/i18n-paraglide/README.md";
207+
const html =
208+
'<p><a href="https://github.com/TanStack/router/tree/main/examples/react/i18n-paraglide">TanStack Router</a></p>';
209+
const pageLinkMap = new Map([
210+
[
211+
"https://github.com/TanStack/router/tree/main/examples/react/i18n-paraglide",
212+
"/m/gerre34r/library-inlang-paraglideJs/tanstack-router",
213+
],
214+
]);
215+
216+
const resolved = resolveHtmlAssetLinks(html, baseUrl, pageLinkMap);
217+
218+
expect(resolved).toContain(
219+
'href="https://github.com/TanStack/router/tree/main/examples/react/i18n-paraglide"',
220+
);
221+
});
203222
});

packages/website-v2/src/marketplace/marketplaceData.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,7 @@ export function resolveHtmlAssetLinks(
355355
pageLinkMap?: PageLinkMap,
356356
) {
357357
if (!baseUrl || !baseUrl.startsWith("http")) return html;
358+
const baseOrigin = getUrlOrigin(baseUrl);
358359
return html.replace(
359360
/(src|href)=(["'])([^"']+)\2/gi,
360361
(_match, attr, quote, value) => {
@@ -364,6 +365,10 @@ export function resolveHtmlAssetLinks(
364365
if (attr.toLowerCase() === "href" && pageLinkMap) {
365366
const target = pageLinkMap.get(normalizePageLinkKey(resolved));
366367
if (target) {
368+
const targetOrigin = getUrlOrigin(resolved);
369+
if (baseOrigin && targetOrigin && baseOrigin !== targetOrigin) {
370+
return `${attr}=${quote}${resolved}${quote}`;
371+
}
367372
const suffix = extractSearchAndHash(resolved);
368373
return `${attr}=${quote}${target}${suffix}${quote}`;
369374
}
@@ -393,6 +398,14 @@ function extractSearchAndHash(value: string) {
393398
}
394399
}
395400

401+
function getUrlOrigin(value: string) {
402+
try {
403+
return new URL(value).origin;
404+
} catch {
405+
return null;
406+
}
407+
}
408+
396409
export function resolveRelativeUrl(
397410
value: string,
398411
baseUrl: string,

0 commit comments

Comments
 (0)