Skip to content

refactor: replace regex markdown renderer with react markdown#94

Merged
Zahnentferner merged 5 commits intoStabilityNexus:mainfrom
bored-arvi:main
Mar 22, 2026
Merged

refactor: replace regex markdown renderer with react markdown#94
Zahnentferner merged 5 commits intoStabilityNexus:mainfrom
bored-arvi:main

Conversation

@bored-arvi
Copy link
Contributor

@bored-arvi bored-arvi commented Mar 20, 2026

Refactor: Replace regex markdown renderer with react-markdown

Addressed Issues:

Fixes #92

Screenshots/Recordings:

before:
image
after:
image

Additional Notes:

Replaces the broken regex-based markdown renderer with react-markdown + gray-matter. Only two files changed — lib/markdown-renderer.tsx (full rewrite) and package.json (added react-markdown). No other files were touched. All existing Tailwind styles, image base path handling, and component API are preserved.

AI Usage Disclosure:

  • 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: Claude (Anthropic)

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

Release Notes

  • Bug Fixes

    • Fixed improper HTML nesting in markdown-rendered images.
  • Refactor

    • Improved markdown rendering system for better stability and performance.

Copilot AI review requested due to automatic review settings March 20, 2026 17:18
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 20, 2026

Warning

Rate limit exceeded

@bored-arvi has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 25 minutes and 4 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 88d9c5fb-01d3-4d82-a08b-9fc021dd53f3

📥 Commits

Reviewing files that changed from the base of the PR and between c2d5904 and 310ce3e.

📒 Files selected for processing (2)
  • lib/markdown-renderer.tsx
  • package.json

Walkthrough

Replaced custom regex-based markdown rendering with the react-markdown library, using a typed component map to handle headers, lists, code blocks, images, and other elements. Removed the dangerouslySetInnerHTML approach and internal renderMarkdown function. Added react-markdown dependency to package.json.

Changes

Cohort / File(s) Summary
Markdown Renderer Refactoring
lib/markdown-renderer.tsx
Migrated from string-based HTML transformation (regex replacements, list/paragraph post-processing) to component-based rendering using react-markdown with typed components map. Handles headers, paragraphs, emphasis, lists, blockquotes, code (inline/block), links, and images through React components instead of HTML rewriting. Image paths starting with / still resolved via BASE_PATH in production. Removed internal renderMarkdown function and post-processing logic.
Dependency Update
package.json
Added react-markdown at version ^9.0.1 to dependencies.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested labels

Typescript Lang

Suggested reviewers

  • ceilican

Poem

🐰 From regex tangles, a rabbit hops free,
React components now render with glee,
Code snippets aligned, indentation bright,
No dangerous HTML—the code looks just right! 📝✨

🚥 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 accurately describes the main change: replacing a regex-based markdown renderer with react-markdown library.
Linked Issues check ✅ Passed The PR directly addresses issue #92 by replacing the broken regex renderer with react-markdown, which resolves code snippet indentation and formatting issues.
Out of Scope Changes check ✅ Passed All changes are directly scoped to fixing the markdown renderer: lib/markdown-renderer.tsx rewrite and package.json dependency addition for react-markdown.

✏️ 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.

@bored-arvi bored-arvi marked this pull request as draft March 20, 2026 17:20
@bored-arvi bored-arvi marked this pull request as ready for review March 20, 2026 17:21
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Refactors the blog’s markdown rendering pipeline to replace the custom regex-based HTML renderer with react-markdown, addressing formatting issues (notably code block indentation) reported in #92.

Changes:

  • Rewrote MarkdownRenderer to render markdown via react-markdown with Tailwind-styled element overrides.
  • Added react-markdown as a dependency and updated lockfiles accordingly.

Reviewed changes

Copilot reviewed 2 out of 3 changed files in this pull request and generated 1 comment.

File Description
lib/markdown-renderer.tsx Replaces regex + dangerouslySetInnerHTML rendering with react-markdown and custom component mappings (including code/pre/img handling).
package.json Adds react-markdown dependency.
package-lock.json Locks react-markdown (and transitive deps) into the npm lockfile.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +29 to +31
code: ({ inline, className, children, ...props }: { inline?: boolean; className?: string; children?: React.ReactNode }) => {
if (inline) {
return <code className="bg-gray-100 px-1 py-0.5 rounded text-sm" {...props}>{children}</code>
Copy link

Copilot AI Mar 20, 2026

Choose a reason for hiding this comment

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

In the custom code renderer, ...props is spread onto the <code> element but react-markdown typically includes non-DOM props (notably node) in the props object. This will cause React warnings about unknown DOM attributes. Destructure and omit node (and only forward valid HTML props) before spreading, or avoid spreading entirely.

Suggested change
code: ({ inline, className, children, ...props }: { inline?: boolean; className?: string; children?: React.ReactNode }) => {
if (inline) {
return <code className="bg-gray-100 px-1 py-0.5 rounded text-sm" {...props}>{children}</code>
code: ({ inline, className, children }: { inline?: boolean; className?: string; children?: React.ReactNode }) => {
if (inline) {
return <code className="bg-gray-100 px-1 py-0.5 rounded text-sm">{children}</code>

Copilot uses AI. Check for mistakes.
Copy link
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: 4

🤖 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`:
- Around line 1-4: This file is a client-side React component but is missing the
"use client" directive and contains a hardcoded BASE_PATH that may conflict with
next.config.mjs; add the "use client" directive as the very first line of
lib/markdown-renderer.tsx, and replace the hardcoded BASE_PATH logic in the
BASE_PATH constant (used by ReactMarkdown rendering) so it derives from an
authoritative source—either read a NEXT_PUBLIC_BASE_PATH environment variable or
align it with the Next.js config (set to '' if next.config.mjs defines basePath:
''), ensuring images/paths rendered by ReactMarkdown get the correct prefix.
- Around line 40-44: The custom link renderer currently forces target="_blank"
for all links (the anonymous renderer in lib/markdown-renderer.tsx: a: ({ href,
children }) => ...), causing internal links to open in new tabs; change it to
detect internal vs external links (treat hrefs starting with "/" or same-origin
URLs as internal) and only add target="_blank" and rel="noopener noreferrer" for
external links, leaving internal links as normal anchors (or use client-side
navigation like Next.js Link if desired) so internal navigation stays in the
same tab.
- Around line 28-34: The code renderer relies on a removed `inline` prop
(react-markdown v9) so inline styling never runs; update the `code` renderer
(the code property in your Markdown renderers) to detect block vs inline by
inspecting the incoming className for a language pattern (e.g. /language-/). If
className contains a language class, render the block form (wrap in a pre with a
code element carrying the className so syntax highlighters work); otherwise
render the inline code element with your existing inline styling. Keep the
symbol names the same (the `code` renderer prop and the className variable) so
changes are localized.

In `@package.json`:
- Around line 22-23: Update the react-markdown dependency version in
package.json from "^9.0.1" to "^9.0.2" to pick up React 19 TypeScript fixes;
open package.json, locate the "react-markdown" entry and change its version spec
to "^9.0.2", then run your package manager (npm/yarn/pnpm) to reinstall and
update lockfile.
🪄 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: 33b429e8-7c7f-4305-ae0a-bb94319022b5

📥 Commits

Reviewing files that changed from the base of the PR and between 07bfb7e and c2d5904.

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

Copy link
Contributor

@Zahnentferner Zahnentferner left a comment

Choose a reason for hiding this comment

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

Thanks!

@Zahnentferner Zahnentferner merged commit 865ea0d into StabilityNexus:main Mar 22, 2026
1 check 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.

[BUG]: Code Snippets Loosing Indentation and Formatting on Render

3 participants