Repository navigation
fix: resolve relative README images and links against the repository - #74
Merged
Merged
Conversation
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
1 task
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 Non-blocking nits go to follow-up issues (srcset not rewritten; raw |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A README written for github.com assumes
images/logo.pngresolves 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.
What changed
Rendered
src,hrefandposterattributes that aren't already absolute now resolve against GitHub. Images go toraw.githubusercontent.com, links go to theblobview.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 -cfailed onmain.@rollup/plugin-typescriptnow rejects adeclarationDirthat sits outside the Rollup output directory. Both bundles sharedir: "dist"instead of separatefileoutputs. Published paths are unchanged.tsconfig.jsonhad noinclude, so declarations for tests, stories and the StackBlitz example were emitted and shipped in the package. Scoped tosrc.jestcouldn't loadmarked, which is ESM only, so the entire component suite was unrunnable. AddedtransformIgnorePatterns. 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
Then against the real site, with this build linked into lacymorrow.com and the page loaded in headless Chromium:
dist/is committed because CI asserts it's present.Release
Needs a publish. npm latest is 1.3.3;
package.jsonsays 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.