Skip to content
Myr

Open an in-project README link through the router by its built href, so its query and fragment stay out of the path

Before adoption: this commit came in with the history the project adopted, so no landing records it.

- Review fix: `<Link to={href}>` put `?raw=1#L3` inside the router's pathname (search `{}`, hash `""`), so a link's location was wrong for active state, matching and hash scroll even though the rendered href looked right. `<Link href={href}>` is TanStack's documented way to navigate to a built href: the router parses it into path, query and fragment. - `Link<RegisteredRouter, string, string>` makes `to` optional in the props; no cast. - Test: a README link to the page the router is on is marked `aria-current="page"`, with its query and fragment. It failed by assertion with `to`, and it also shows the anchor is a router Link (a plain `<a>` carries no active state), which the earlier routed test could not tell apart.

authored by oscabriel · committed by oscabriel · parent c0ea449 · Browse files · 2 files+25 added−5 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+25 added−5 removed

  • tests:+20 added−3 removed
  • other:+5 added−2 removed

2 files+25 added−5 removed

modified:apps/web/src/features/markdown/markdown.tsx+5 added-2 removed
modified:apps/web/test/markdown.test.tsx+20 added-3 removed
29 unmodified lines
30 renderToString(31 <RepoMarkdown at={sha} dir={dir} project={project} source={source} />32 );3334/** The same, inside a router, as the app renders it. */35const routed = (element: ReactElement) =>34/** The same, inside a router at `location`, as the app renders it. */35const routed = (element: ReactElement, location = "/p") =>36 renderToString(37 <RouterContextProvider38 router={createRouter({39 history: createMemoryHistory({ initialEntries: ["/p"] }),39 history: createMemoryHistory({ initialEntries: [location] }),40 routeTree: createRootRoute(),41 })}42 >43 {element}
84 unmodified lines
128 `<a href="/p/blob/${sha}/x?raw=1#L3">`,129 ]);130 });131132 it("marks a link to the page it is on as the current page, its query and fragment read as such", () => {133 const markup = routed(134 <RepoMarkdown135 at={sha}136 dir="docs"137 project={project}138 source="[a](../../x?raw=1#L3)"139 />,140 `/p/blob/${sha}/x?raw=1#L3`141 );142143 const [anchor] = anchorsOf(markup);144145 expect(anchor).toContain(`href="/p/blob/${sha}/x?raw=1#L3"`);146 expect(anchor).toContain('aria-current="page"');147 });148149 it("leaves an external link as written, opened with no referrer", () => {150 const [anchor] = anchorsOf(readme("[c](https://e.org)"));151152 expect(anchor).toContain('href="https://e.org"');
83 unmodified lines