Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

Ricbet
Copy link
Member

@Ricbet Ricbet commented Oct 25, 2024

Types

  • 🐛 Bug Fixes

Background or solution

Changelog

修复 markdown react 组件渲染 html 标签异常的问题

Summary by CodeRabbit

  • 新特性

    • 简化了Markdown链接的渲染过程,恢复了默认的链接处理方式。
    • 改进了HTML和文本令牌的处理,增强了渲染控制。
  • Bug修复

    • 更新了HTML内容的渲染方式,增强了安全性。
  • 文档

    • 更新了MarkdownReactRenderer和MarkdownReactParser的相关逻辑,确保更清晰的代码结构。

@Ricbet Ricbet requested review from hacke2 and erha19 October 25, 2024 03:50
@Ricbet Ricbet self-assigned this Oct 25, 2024
@opensumi opensumi bot added the 🐞 bug Something isn't working label Oct 25, 2024
Copy link
Contributor

coderabbitai bot commented Oct 25, 2024

Warning

Rate limit exceeded

@Ricbet has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 20 minutes and 54 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.

📥 Commits

Files that changed from the base of the PR and between d410e54 and 256ee5c.

Walkthrough

此次更改涉及三个主要文件,分别是 ChatMarkdown.tsxparse.tsxrender.tsx。在 ChatMarkdown.tsx 中,移除了自定义链接渲染逻辑,简化了链接渲染过程。在 parse.tsx 中,修改了 HTML 令牌的处理方式,采用了更受控的渲染方法,并引入了新的私有属性 unescapeInfo。最后,在 render.tsx 中,更新了 MarkdownReactRenderer 类的 html 方法签名,并简化了链接的目标属性设置。

Changes

文件路径 更改摘要
packages/ai-native/src/browser/components/ChatMarkdown.tsx 移除自定义链接渲染逻辑,恢复默认链接渲染行为。
packages/components/src/markdown-react/parse.tsx 修改 HTML 令牌处理,采用 this.renderer.html(token.text) 替代 dangerouslySetInnerHTML,引入 unescapeInfo 属性。
packages/components/src/markdown-react/render.tsx 更新 html 方法签名,简化链接目标属性设置,移除 openLinksInNewTab 选项。

Possibly related PRs

Suggested reviewers

  • bytemain
  • erha19
  • hacke2

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

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: 3

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between d5d4dae and e900433.

📒 Files selected for processing (3)
  • packages/ai-native/src/browser/components/ChatMarkdown.tsx (0 hunks)
  • packages/components/src/markdown-react/parse.tsx (3 hunks)
  • packages/components/src/markdown-react/render.tsx (3 hunks)
💤 Files with no reviewable changes (1)
  • packages/ai-native/src/browser/components/ChatMarkdown.tsx
🧰 Additional context used
🪛 Biome
packages/components/src/markdown-react/render.tsx

[error] 132-132: Avoid passing content using the dangerouslySetInnerHTML prop.

Setting content using code can expose users to cross-site scripting (XSS) attacks

(lint/security/noDangerouslySetInnerHtml)

🔇 Additional comments (2)
packages/components/src/markdown-react/parse.tsx (2)

22-22: 改进:HTML 渲染方式更安全

将 HTML 渲染委托给 renderer.html 处理是一个很好的改进,这样可以避免直接使用 dangerouslySetInnerHTML,提高了安全性。


122-123: 验证:HTML 实体替换的正则表达式

正则表达式 /&(#\d+|[a-zA-Z]+);/g 看起来能够正确匹配 HTML 实体,但建议验证一下是否能处理所有边缘情况。

✅ Verification successful

Let me analyze the results and generate additional verification to check the implementation details.


HTML 实体替换实现已验证完备

代码中的正则表达式 /&(#\d+|[a-zA-Z]+);/g 配合 unescapeInfo Map 的实现已经完全覆盖了常见的 HTML 实体转换场景:

  • 支持数字实体(如 &#39;
  • 支持命名实体(如 &quot;&nbsp;&amp;&lt;&gt;
  • 对于未知实体会保持原样(通过 ?? m 实现)

实现方案简洁且健壮,无需修改。

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# 搜索代码库中的 HTML 实体使用情况,确保所有情况都能被正确处理
rg -g '*.{md,mdx,html,tsx,jsx}' '&(#\d+|[a-zA-Z]+);' -A 2

Length of output: 1269


Script:

#!/bin/bash
# Check the implementation of unescapeInfo and related code
ast-grep --pattern 'class $_ {
  $$$
  unescapeInfo$_$$$
  $$$
}'

Length of output: 67


Script:

#!/bin/bash
# 检查 parse.tsx 文件中的 unescapeInfo 相关实现
cat packages/components/src/markdown-react/parse.tsx

Length of output: 4754

Copy link

codecov bot commented Oct 25, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 54.29%. Comparing base (d5d4dae) to head (256ee5c).
Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4119      +/-   ##
==========================================
+ Coverage   54.26%   54.29%   +0.03%     
==========================================
  Files        1598     1597       -1     
  Lines       97564    97498      -66     
  Branches    19946    19937       -9     
==========================================
- Hits        52941    52935       -6     
+ Misses      37074    37020      -54     
+ Partials     7549     7543       -6     
Flag Coverage Δ
jsdom 49.84% <ø> (+0.02%) ⬆️
node 15.61% <ø> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

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: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between e900433 and d410e54.

📒 Files selected for processing (1)
  • packages/components/src/markdown-react/render.tsx (3 hunks)
🧰 Additional context used
🪛 Biome
packages/components/src/markdown-react/render.tsx

[error] 132-132: Avoid passing content using the dangerouslySetInnerHTML prop.

Setting content using code can expose users to cross-site scripting (XSS) attacks

(lint/security/noDangerouslySetInnerHtml)

🔇 Additional comments (1)
packages/components/src/markdown-react/render.tsx (1)

131-132: HTML 内容需要安全处理

之前的评审意见关于使用 DOMPurify 进行 HTML 内容净化的建议仍然有效。直接使用 dangerouslySetInnerHTML 存在 XSS 安全风险。

🧰 Tools
🪛 Biome

[error] 132-132: Avoid passing content using the dangerouslySetInnerHTML prop.

Setting content using code can expose users to cross-site scripting (XSS) attacks

(lint/security/noDangerouslySetInnerHtml)

@hacke2 hacke2 merged commit 0dec87c into main Oct 25, 2024
11 checks passed
@hacke2 hacke2 deleted the fix/markdown-link-render branch October 25, 2024 08:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐞 bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants