Skip to content

fix: handle relative image paths in markdown renderer#95

Open
sejalkutriyar wants to merge 1 commit intoStabilityNexus:mainfrom
sejalkutriyar:fix/markdown-image-rendering
Open

fix: handle relative image paths in markdown renderer#95
sejalkutriyar wants to merge 1 commit intoStabilityNexus:mainfrom
sejalkutriyar:fix/markdown-image-rendering

Conversation

@sejalkutriyar
Copy link
Copy Markdown

@sejalkutriyar sejalkutriyar commented Mar 30, 2026

Addressed Issues:

Fixes #(TODO:issue number)

Fixes #89

Screenshots/Recordings:

TODO: If applicable, add screenshots or recordings that demonstrate the interface before and after the changes.

before fix

rosen_bridge_image_bug_1774894993122

after fix

test_bug_content_check_1774895063742

check_relative_image_path_1774894967468

Additional Notes:

The markdown articles in this repository use relative paths (e.g., ../images/) to reference assets. This PR updates lib/markdown-renderer.tsx to correctly resolve these paths to the root-relative /images/ directory, ensuring images render correctly when viewed on the /a/[slug] route. It also ensures that any configured BASE_PATH is respected.

AI Usage Disclosure:

We encourage contributors to use AI tools responsibly when creating Pull Requests. While AI can be a valuable aid, it is essential to ensure that your contributions meet the task requirements, build successfully, include relevant tests, and pass all linters. Submissions that do not meet these standards may be closed without warning to maintain the quality and integrity of the project. Please take the time to understand the changes you are proposing and their impact. AI slop is strongly discouraged and may lead to banning and blocking. Do not spam our repos with AI slop.

Check one of the checkboxes below:

  • This PR does not contain AI-generated code at all.
  • This PR contains AI-generated code. I have read the AI Usage Policy and this PR complies with this policy. I have tested the code locally and I am responsible for it.

I have used the following AI models and tools: TODO

I have used chatgpt to identify the markdown rendering bug

Checklist

  • My PR addresses a single issue, fixes a single bug or makes a single improvement.
  • My code follows the project's code style and conventions
  • If applicable, I have made corresponding changes or additions to the documentation
  • If applicable, I have made corresponding changes or additions to tests
  • My changes generate no new warnings or errors
  • I have joined the Discord server and I will share a link to this PR with the project maintainers there
  • I have read the Contribution Guidelines
  • Once I submit my PR, CodeRabbit AI will automatically review it and I will address CodeRabbit's comments.
  • I have filled this PR template completely and carefully, and I understand that my PR may be closed without review otherwise.

Summary by CodeRabbit

  • Bug Fixes
    • Improved image path resolution in markdown to more flexibly handle relative and root-relative paths, ensuring images display correctly across different path formats.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Mar 30, 2026

Walkthrough

Updated the img renderer in markdown-renderer.tsx to resolve image source paths more flexibly: normalizes "../" prefixes to "/" and conditionally prepends BASE_PATH to root-relative paths not already prefixed with it, fixing markdown image rendering failures.

Changes

Cohort / File(s) Summary
Image Path Resolution
lib/markdown-renderer.tsx
Enhanced img renderer logic to normalize relative paths starting with "../" by replacing the prefix with "/", then conditionally prepend BASE_PATH to root-relative paths, improving markdown image rendering reliability.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

Suggested labels

Typescript Lang

Suggested reviewers

  • Zahnentferner
  • ceilican

Poem

🐰 Through twisted paths of ../ and /,
The renderer now sees images above,
With BASE_PATH logic set just right,
Markdown images dance back into sight! ✨

🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: fixing the handling of relative image paths in the markdown renderer.
Linked Issues check ✅ Passed The PR addresses issue #89 by updating the markdown renderer to correctly resolve relative image paths (../images/) to root-relative paths (/images/), enabling markdown image syntax to render properly.
Out of Scope Changes check ✅ Passed The changes are limited to lib/markdown-renderer.tsx and focus solely on fixing image path resolution logic, staying within the scope of issue #89.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@lib/markdown-renderer.tsx`:
- Line 67: The optional chaining on resolvedSrc is redundant because the
preceding type guard (typeof src === "string") ensures resolvedSrc is a string;
update the conditional in the markdown-renderer (the if that currently uses
resolvedSrc?.startsWith("/") && !resolvedSrc.startsWith(BASE_PATH)) to remove
the ?. so it reads resolvedSrc.startsWith("/") &&
!resolvedSrc.startsWith(BASE_PATH), keeping the same logic and using the
existing resolvedSrc identifier.
- Around line 62-64: The current check in markdown-renderer.tsx only replaces
the first "../" (resolvedSrc = src.replace("../", "/")) so deeper relative paths
like "../../images" or "../foo/../images" won't be normalized; update the logic
that sets resolvedSrc (using the src variable) to strip or collapse all leading
"../" segments (e.g., use a regex that matches one-or-more leading "../"
segments or loop while src.startsWith("../") to repeatedly remove them) so
resolvedSrc becomes a proper rooted path for any depth of relative path.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: c86ba8d5-744f-4895-9052-adb8977c137d

📥 Commits

Reviewing files that changed from the base of the PR and between 865ea0d and cd95257.

⛔ Files ignored due to path filters (1)
  • package-lock.json is excluded by !**/package-lock.json
📒 Files selected for processing (1)
  • lib/markdown-renderer.tsx

Comment on lines +62 to +64
if (src.startsWith("../")) {
resolvedSrc = src.replace("../", "/")
}
Copy link
Copy Markdown
Contributor

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
      // Handle relative paths like ../images/ - assumes images are in public/images/
-     if (src.startsWith("../")) {
-       resolvedSrc = src.replace("../", "/")
+     // Normalize by collapsing leading ../ sequences to root-relative
+     while (resolvedSrc.startsWith("../")) {
+       resolvedSrc = resolvedSrc.slice(3) // Remove leading "../"
      }
+     if (resolvedSrc !== src) {
+       resolvedSrc = "/" + resolvedSrc
+     }

Alternatively, a simpler regex approach:

-     if (src.startsWith("../")) {
-       resolvedSrc = src.replace("../", "/")
-     }
+     // Strip all leading ../ and prepend /
+     resolvedSrc = src.replace(/^(\.\.\/)+/, "/")
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (src.startsWith("../")) {
resolvedSrc = src.replace("../", "/")
}
// Strip all leading ../ and prepend /
resolvedSrc = src.replace(/^(\.\.\/)+/, "/")
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@lib/markdown-renderer.tsx` around lines 62 - 64, The current check in
markdown-renderer.tsx only replaces the first "../" (resolvedSrc =
src.replace("../", "/")) so deeper relative paths like "../../images" or
"../foo/../images" won't be normalized; update the logic that sets resolvedSrc
(using the src variable) to strip or collapse all leading "../" segments (e.g.,
use a regex that matches one-or-more leading "../" segments or loop while
src.startsWith("../") to repeatedly remove them) so resolvedSrc becomes a proper
rooted path for any depth of relative path.

}

// Prepend BASE_PATH if it's a root-relative path
if (resolvedSrc?.startsWith("/") && !resolvedSrc.startsWith(BASE_PATH)) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick | 🔵 Trivial

Optional chaining is redundant after the type guard.

After the typeof src === "string" check on line 60, resolvedSrc is guaranteed to be a string, making the ?. unnecessary. This is a minor nitpick and doesn't affect functionality.

🧹 Remove redundant optional chaining
-     if (resolvedSrc?.startsWith("/") && !resolvedSrc.startsWith(BASE_PATH)) {
+     if (resolvedSrc.startsWith("/") && !resolvedSrc.startsWith(BASE_PATH)) {
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (resolvedSrc?.startsWith("/") && !resolvedSrc.startsWith(BASE_PATH)) {
if (resolvedSrc.startsWith("/") && !resolvedSrc.startsWith(BASE_PATH)) {
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@lib/markdown-renderer.tsx` at line 67, The optional chaining on resolvedSrc
is redundant because the preceding type guard (typeof src === "string") ensures
resolvedSrc is a string; update the conditional in the markdown-renderer (the if
that currently uses resolvedSrc?.startsWith("/") &&
!resolvedSrc.startsWith(BASE_PATH)) to remove the ?. so it reads
resolvedSrc.startsWith("/") && !resolvedSrc.startsWith(BASE_PATH), keeping the
same logic and using the existing resolvedSrc identifier.

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.

[BUG]: Image files added to blogs in markdown syntax are not rendering

1 participant