Skip to content

fix: resolve relative README images and links against the repository - #74

Merged
lacymorrow merged 2 commits into
mainfrom
lac-4944/relative-readme-urls
Nov 5, 2026
Merged

lacymorrow merged 2 commits into
mainfrom
lac-4944/relative-readme-urls

Conversation

@lacymorrow

@lacymorrow lacymorrow commented Oct 19, 2026 •

Copy link
Copy Markdown
Owner

A README written for github.com assumes images/logo.png resolves against the repo. Render it anywhere else and that path resolves against the host page's origin, so the image 404s. Every relative image in a README came out as a broken-image icon, and every relative link went nowhere.

Found on lacymorrow.com/play/vscode/if-end-ghost-text (Paperclip LAC-4944).

Before / after

Same page, same README, 1.3.3 on the left and this branch on the right.

Before After

What changed

Rendered src, href and poster attributes that aren't already absolute now resolve against GitHub. Images go to raw.githubusercontent.com, links go to the blob view.

Both bases come from the README's own download URL. The GitHub API already hands back https://raw.githubusercontent.com/{owner}/{repo}/{ref}/{path}, so owner, repo and ref are free, and a README sitting in a subdirectory or on a non-default branch resolves correctly without a second API call. Root-relative paths (/images/a.png) resolve against the repo root rather than the README's directory.

Left alone: absolute URLs, protocol-relative URLs, data:, mailto:, and in-page anchors. Markup inside code fences arrives HTML-escaped, so the rewrite never touches it; there's a test pinning that.

No new props and no opt-out. The old behaviour was never useful.

Three pre-existing breakages, fixed because this couldn't ship without them

  • rollup -c failed on main. @rollup/plugin-typescript now rejects a declarationDir that sits outside the Rollup output directory. Both bundles share dir: "dist" instead of separate file outputs. Published paths are unchanged.
  • tsconfig.json had no include, so declarations for tests, stories and the StackBlitz example were emitted and shipped in the package. Scoped to src.
  • jest couldn't load marked, which is ESM only, so the entire component suite was unrunnable. Added transformIgnorePatterns. The GFM-alert regression test from fix: render inline formatting inside GitHub alerts; component audit cleanup #18 has not actually run since the marked 18 bump.

Verification

$ npx jest
PASS src/components/relative-urls.test.ts
PASS src/components/github-readme.test.tsx
Tests: 16 passed, 16 total

$ npm run build
created dist, dist in 1.2s
created dist/index.d.ts in 8ms

Then against the real site, with this build linked into lacymorrow.com and the page loaded in headless Chromium:

relative images resolved: 3/3   (logo.png, demo.gif, screenshot.png)
broken images:            0
relative links remaining: 0
failed requests:          0

dist/ is committed because CI asserts it's present.

Release

Needs a publish. npm latest is 1.3.3; package.json says 1.3.4 and the v1.3.4 tag exists, so a previous release got partway. I left the version alone for shipx to sort out. lacymorrow.com needs a dependency bump after this publishes.

A README is written to be read on github.com, where `images/logo.png`
resolves against the repo. Rendered anywhere else it resolved against the
host page's origin and 404'd, so every relative image showed as a broken
icon and every relative link went nowhere.

Rendered `src`, `href` and `poster` attributes that are not absolute now
resolve against GitHub: images to raw.githubusercontent.com, links to the
blob view. Both bases come from the README's own download URL, which
already carries owner, repo and ref, so a README in a subdirectory or on a
non-default branch resolves correctly without an extra API call. Absolute
URLs, in-page anchors and mailto: are untouched, and escaped markup inside
code blocks is never matched.

Three pre-existing breakages had to be fixed to ship it:

- `rollup -c` failed because @rollup/plugin-typescript now rejects a
  `declarationDir` outside the Rollup output directory. Both bundles share
  `dir: "dist"`; the published file paths are unchanged.
- tsconfig had no `include`, so declarations for tests, stories and the
  StackBlitz example were emitted and shipped.
- `jest` could not load `marked`, which is ESM only, so the whole component
  suite was unrunnable.

Refs LAC-4944
@lacymorrow

Copy link
Copy Markdown
Owner Author

Code review (LAC-7132), head 07c0a5a. Approve.

Read relative-urls.ts and the component diff in full. Ran jest (16/16), a clean rollup -c (committed dist/cjs, dist/esm and dist/index.d.ts are byte-identical to the clean build; only .map files differ) and a consumer tsc against the built package. Each scope extra is load-bearing: base jest.config fails with SyntaxError: Unexpected token export (ESM-only marked); base rollup/tsconfig fails on declarationDir. Each regression assert goes red under its own mutant.

Non-blocking nits go to follow-up issues (srcset not rewritten; raw > in an attribute leaves that tag unrewritten, fails safe; no prepublishOnly).

@lacymorrow
lacymorrow merged commit 5be3274 into main Nov 5, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant