Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion lib/markdown-renderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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("../", "/")
}
Comment on lines +62 to +64
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.

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" />
},
Expand Down
13 changes: 12 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.