-
-
Notifications
You must be signed in to change notification settings - Fork 847
feat(XMarkdown): 支持通过组件匹配未完成的Markdown语法 #1223
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: next
Are you sure you want to change the base?
Conversation
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the Note Other AI code review bot(s) detectedCodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review. ✨ Finishing touches🧪 Generate 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. Comment |
Bundle ReportChanges will decrease total bundle size by 1.87MB (-45.04%) ⬇️. This is within the configured threshold ✅ Detailed changes
Affected Assets, Files, and Routes:view changes for bundle: x-markdown-array-pushAssets Changed:
|
Summary of ChangesHello @Div627, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! 此拉取请求显著增强了 Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
Deploying ant-design-x with
|
Latest commit: |
2b9a27e
|
Status: | ✅ Deploy successful! |
Preview URL: | https://964732a0.ant-design-x.pages.dev |
Branch Preview URL: | https://feature-markdown-format.ant-design-x.pages.dev |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
本次 PR 旨在通过组件匹配未完成的 Markdown 语法,以改善流式渲染的用户体验。整体实现思路清晰,通过状态机和 token 缓冲来处理流式数据,并增加了替换不完整语法的能力。代码重构和测试用例的补充都非常出色,特别是新增了大量针对边缘场景的测试,大大提高了代码的健壮性。
我发现了一些可以改进的地方,主要集中在 useStreaming.ts
hook 中,包括一个正则表达式的潜在 bug 和一处为了可读性可以优化的逻辑。此外,文档的更新和重构做得很好,使得新功能更容易理解和使用。
.replace(/!\[([^\]]*?)\](?!\([^)]*\)$)(?![^[]*\]\([^)]*\))$/, finalComponentMap.image) | ||
.replace(/\[([^\]]*?)\](?!\([^)]*\)$)(?![^[]*\]\([^)]*\))$/, finalComponentMap.link) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里的正则表达式 (?![^[]*]\\[([^)]*\\]))$/
存在一些问题,可能会导致对某些 Markdown 语法的错误处理。
- 逻辑混乱:
[^[]*
限制了[
字符的出现,这对于处理嵌套括号的链接文本来说过于严格。 - 潜在 Bug: 这个正则表达式无法正确处理引用式链接(reference-style links)。例如,对于
![alt][ref]
这样的语法,它会被错误地识别为不完整的图片并被替换,因为[ref]
不会匹配[^[]*]\\[([^)]*\\])
,导致负向先行断言成功。
建议简化和修正这个正则表达式,以更准确地处理链接和图片的边界情况。例如,可以分别检查是否跟着 (...)
或 [...]
。
.replace(/!\[([^\]]*?)\](?!\([^)]*\)$)(?![^[]*\]\([^)]*\))$/, finalComponentMap.image) | |
.replace(/\[([^\]]*?)\](?!\([^)]*\)$)(?![^[]*\]\([^)]*\))$/, finalComponentMap.link) | |
.replace(/!\[([^\]]*?)\](?!\([^\]]*?\)$)(?!\[[^\]]*?\]$)$/, finalComponentMap.image) | |
.replace(/\[([^\]]*?)\](?!\([^\]]*?\)$)(?!\[[^\]]*?\]$)$/, finalComponentMap.link) |
popToken(); | ||
if (emphasisCount === 1) { | ||
/** | ||
* _token_ / *token* | ||
* ^ ^ | ||
*/ | ||
pushToken(TokenType.Emphasis); | ||
} else if (emphasisCount === 2) { | ||
/** | ||
* __token__ / **token** | ||
* ^ ^ | ||
*/ | ||
pushToken(TokenType.Strong); | ||
} else if (emphasisCount === 3) { | ||
/** | ||
* ___token___ / ***token*** | ||
* ^ ^ | ||
*/ | ||
pushToken(TokenType.Emphasis); | ||
pushToken(TokenType.Strong); | ||
} else { | ||
// no more than 3 | ||
buffer.emphasisCount = 0; | ||
|
||
switch (emphasisCount) { | ||
case 1: | ||
/** | ||
* _token_ / *token* | ||
* ^ ^ | ||
*/ | ||
pushToken(TokenType.Emphasis); | ||
break; | ||
case 2: | ||
/** | ||
* __token__ / **token** | ||
* ^ ^ | ||
*/ | ||
pushToken(TokenType.Strong); | ||
break; | ||
case 3: | ||
/** | ||
* ___token___ / ***token*** | ||
* ^ ^ | ||
*/ | ||
pushToken(TokenType.Emphasis); | ||
pushToken(TokenType.Strong); | ||
break; | ||
default: | ||
buffer.emphasisCount = 0; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
在 MaybeEmphasis
token 的处理逻辑中,switch (emphasisCount)
使用的是在 handleTokenProcessing
函数开头解构的 emphasisCount
值。虽然在当前逻辑下它能正确工作,但这种写法有些微妙且难以理解:emphasisCount
的值在 case
内部通过 buffer.emphasisCount++
被修改,但 switch
语句却使用了修改前的值。
为了提高代码的可读性和可维护性,建议在 else
块内创建一个新变量来保存当前的 emphasisCount
,这样意图会更清晰。
const currentEmphasisCount = buffer.emphasisCount;
popToken();
switch (currentEmphasisCount) {
case 1:
/**
* _token_ / *token*
* ^ ^
*/
pushToken(TokenType.Emphasis);
break;
case 2:
/**
* __token__ / **token**
* ^ ^
*/
pushToken(TokenType.Strong);
break;
case 3:
/**
* ___token___ / ***token***
* ^ ^
*/
pushToken(TokenType.Emphasis);
pushToken(TokenType.Strong);
break;
default:
buffer.emphasisCount = 0;
}
}
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## next #1223 +/- ##
==========================================
- Coverage 94.61% 94.57% -0.04%
==========================================
Files 131 131
Lines 3455 3431 -24
Branches 954 948 -6
==========================================
- Hits 3269 3245 -24
Misses 184 184
Partials 2 2 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
size-limit report 📦
|
中文版模板 / Chinese template
🤔 This is a ...
💡 Background and Solution
Background: 在流式渲染时,遇到未完成的图片/链接 ![img](,缓存等待时间久,用户体验差。
Solution:匹配未完成的 Markdown 语法,支持传入 components 传入加载状态。
📝 Change Log