-
Notifications
You must be signed in to change notification settings - Fork 32
fix: handle relative image paths in markdown renderer #95
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -56,7 +56,18 @@ const components: Components = { | |||||
|
|
||||||
| // no <div> wrapper — avoids invalid <p><div></div></p> nesting | ||||||
| img: ({ src, alt }) => { | ||||||
| const resolvedSrc = typeof src === "string" && src.startsWith("/") ? `${BASE_PATH}${src}` : src | ||||||
| let resolvedSrc = src | ||||||
| if (typeof src === "string") { | ||||||
| // Handle relative paths like ../images/ - assumes images are in public/images/ | ||||||
| if (src.startsWith("../")) { | ||||||
| resolvedSrc = src.replace("../", "/") | ||||||
| } | ||||||
|
|
||||||
| // Prepend BASE_PATH if it's a root-relative path | ||||||
| if (resolvedSrc?.startsWith("/") && !resolvedSrc.startsWith(BASE_PATH)) { | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧹 Nitpick | 🔵 Trivial Optional chaining is redundant after the type guard. After the 🧹 Remove redundant optional chaining- if (resolvedSrc?.startsWith("/") && !resolvedSrc.startsWith(BASE_PATH)) {
+ if (resolvedSrc.startsWith("/") && !resolvedSrc.startsWith(BASE_PATH)) {📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||
| resolvedSrc = `${BASE_PATH}${resolvedSrc}` | ||||||
| } | ||||||
| } | ||||||
| // eslint-disable-next-line @next/next/no-img-element | ||||||
| return <img src={resolvedSrc} alt={alt ?? ""} className="my-8 block rounded-lg w-full h-auto max-w-2xl mx-auto shadow-lg" /> | ||||||
| }, | ||||||
|
|
||||||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧹 Nitpick | 🔵 Trivial
Consider handling deeper relative paths for future-proofing.
String.replace()only replaces the first occurrence. If a markdown file ever uses../../images/or paths like../foo/../images/, only the first../would be replaced. While context confirms current files use single-level../, a regex or loop would be more robust.🔧 Optional: Handle multiple relative path segments
Alternatively, a simpler regex approach:
📝 Committable suggestion
🤖 Prompt for AI Agents