fix: 改进 Markdown 代码块渲染 (#206)#210
Conversation
- Fix inline code vs code block detection logic * Add data-code-block marker from pre to code element * Support code blocks without language specification * Prevent misidentification of code blocks as inline code - Enhance code block CSS styles to prevent line wrapping * Add white-space: pre to maintain original layout * Add word-wrap: normal and word-break: normal * Ensure horizontal scrolling instead of wrapping - Fix code tag regex in translation mode * Support code tags with attributes like <code class="..."> * Improve regex pattern to match all code tag variations Fixes #206 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughImproves inline vs block code detection in MarkdownRenderer, relaxes BilingualMarkdownRenderer's code-tag regex to match code tags with attributes, and adds CSS to preserve preformatted layout and enable horizontal scrolling for long code lines. ChangesCode Rendering Fix
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
src/index.css (1)
194-213:⚠️ Potential issue | 🟡 Minor | ⚡ Quick winReplace deprecated
word-wrapwithoverflow-wrap.The
word-wrapproperty is deprecated. Useoverflow-wrapinstead, which is the standard property name.🔧 Proposed fix
.prose pre { overflow-x: auto; white-space: pre; - word-wrap: normal; + overflow-wrap: normal; word-break: normal; } .prose pre code { display: block; padding: 1rem; border-radius: 0.5rem; font-size: 0.875rem; line-height: 1.5rem; tab-size: 2; -moz-tab-size: 2; white-space: pre; - word-wrap: normal; + overflow-wrap: normal; word-break: normal; overflow-x: auto; }🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/index.css` around lines 194 - 213, Replace the deprecated CSS property `word-wrap` with the standard `overflow-wrap` in both selectors; update the rules in the `.prose pre` and `.prose pre code` blocks to remove `word-wrap: normal;` and add `overflow-wrap: normal;` (keeping the other properties like `white-space`, `word-break`, and `overflow-x` unchanged) so the styling uses the modern property name.Source: Linters/SAST tools
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/components/MarkdownRenderer.tsx`:
- Around line 879-885: The pre renderer in MarkdownRenderer.tsx currently uses
React.cloneElement(children as React.ReactElement<any>, ...) which weakens type
safety; change the cast to a specific element prop type such as
React.ReactElement<React.ComponentPropsWithoutRef<'code'>> (or another
appropriate HTML/code prop type) so cloneElement's props are correctly typed and
the 'data-code-block' attribute is applied with proper typing for the children
element.
---
Outside diff comments:
In `@src/index.css`:
- Around line 194-213: Replace the deprecated CSS property `word-wrap` with the
standard `overflow-wrap` in both selectors; update the rules in the `.prose pre`
and `.prose pre code` blocks to remove `word-wrap: normal;` and add
`overflow-wrap: normal;` (keeping the other properties like `white-space`,
`word-break`, and `overflow-x` unchanged) so the styling uses the modern
property name.
🪄 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: defaults
Review profile: CHILL
Plan: Pro
Run ID: 5793cd81-7c47-4466-b1d9-2911274ef9ff
📒 Files selected for processing (3)
src/components/BilingualMarkdownRenderer.tsxsrc/components/MarkdownRenderer.tsxsrc/index.css
- Replace deprecated word-wrap with overflow-wrap in CSS - Improve TypeScript type safety for React.cloneElement - Add styling for plain pre tags (ASCII art/character art) * Support pre tags without code child elements * Ensure proper display for character art and diagrams * Maintain white-space: pre to prevent line wrapping Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
- Keep pre tag when it doesn't contain code child element - Ensure ASCII art and character diagrams maintain their container - Previously the pre tag was stripped causing layout issues Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
问题描述
修复 Issue #206 报告的 Markdown 代码块渲染问题:
根本原因
问题 1:行内代码 vs 代码块判断错误
!className判断是否为行内代码问题 2:CSS 样式未明确禁止换行
white-space: pre等属性问题 3:翻译模式下的代码标签匹配不完整
/<code>([\s\S]*?)<\/code>/g匹配<code>标签实施的修复
修复 1:改进行内代码 vs 代码块判断逻辑
文件:
src/components/MarkdownRenderer.tsxpre组件给其子code元素添加data-code-block属性code组件通过检查该属性或className来判断是否为代码块修复 2:增强代码块 CSS 样式
文件:
src/index.csswhite-space: pre保留空白字符,不折行word-wrap: normal和word-break: normal防止单词断行overflow-x: auto提供横向滚动条修复 3:修复翻译模式下的代码标签匹配
文件:
src/components/BilingualMarkdownRenderer.tsx/<code(?:\s+[^>]*)?>([\s\S]*?)<\/code>/g<code>、<code class="...">、<code data-code-block>等测试建议
修改的文件
src/index.css- 增强代码块 CSS 样式src/components/MarkdownRenderer.tsx- 改进代码块判断逻辑src/components/BilingualMarkdownRenderer.tsx- 修复翻译模式代码匹配兼容性说明
Closes #206
🤖 Generated with Claude Code
Summary by CodeRabbit
Bug Fixes
Style