Skip to content
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

refactor: matching jsx component logic in string fragments #1861

Closed
wants to merge 5 commits into from

Conversation

Wxh16144
Copy link
Member

@Wxh16144 Wxh16144 commented Aug 27, 2023

🤔 这个变动的性质是?/ What is the nature of this change?

  • 新特性提交 / New feature
  • bug 修复 / Fix bug
  • 样式优化 / Style optimization
  • 代码风格优化 / Code style optimization
  • 性能优化 / Performance optimization
  • 构建优化 / Build optimization
  • 网站、文档、Demo 改进 / Website, documentation, demo improvements
  • 重构代码或样式 / Refactor code or style
  • 测试相关 / Test related
  • 其他 / Other

🔗 相关 Issue / Related Issue

fix #1836

💡 需求背景和解决方案 / Background or solution

解决方案

问了 chatGPT 然后得到一个匹配 jsx 组件的 正则表达式,试了一下能跑成功。然后写了一写测试用例。(不知道信不信得通)

📝 更新日志 / Changelog

Language Changelog
🇺🇸 English refactor: matching jsx component logic in string fragments
🇨🇳 Chinese 重构字符串片段解析 jsx 组件逻辑

@vercel
Copy link

vercel bot commented Aug 27, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
dumi ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 27, 2023 10:21am

@@ -5,8 +5,7 @@ import type { IMdTransformerOptions } from '.';

let raw: typeof import('hast-util-raw').raw;
let visit: typeof import('unist-util-visit').visit;
const COMPONENT_NAME_REGEX = /<[A-Z][a-zA-Z\d]*/g;
Copy link
Member

Choose a reason for hiding this comment

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

这个改动删掉的逻辑会导致驼峰 props 的丢失,比如 <Foo relativePath="xxx"></Foo> 会被转成 <Foo relative-path="xxx"></Foo>

另外看了下关键改动点,应该就是正则的改动从匹配 <Foo 变成匹配 <Foo xxx>,虽然能解 issue 提到的 case 但仍然存在字符串属性值里的 JSX 被误伤的可能性,比如 <Foo str="<Bar xxx></Bar>" 应该还是会误伤,只要还是用正则而不是 AST 误伤就不可避免

建议还是按原逻辑替换,在最后一步遍历 properties 恢复 key 名的时候,多加一步恢复 value 的逻辑,整体流程是先误伤、再恢复,因为到恢复这一步的时候,hast-util-raw 已经通过 AST 解析把真正的标签处理过了,剩下的都是在 properties 里的字符串标签,正好是之前误伤的内容

Copy link
Member Author

Choose a reason for hiding this comment

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

整体流程是先误伤、再恢复,因为到恢复这一步的时候,hast-util-raw 已经通过 AST 解析把真正的标签处理过了,

误伤后给 raw 处理得到的 properties 是有问题的。回复处理有点困难

我这边的解决思路还是用这个 pr, 然后继续跟进你说的 驼峰 props 丢失问题。先挂一下问题,对我来说有些挑战

Copy link
Member

Choose a reason for hiding this comment

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

试了下 properties 的确已经乱了,我换了种思路解决,有空可以帮忙 review 下:#1872

@Wxh16144 Wxh16144 marked this pull request as draft August 28, 2023 02:15
@Wxh16144
Copy link
Member Author

Resolved #1872

@Wxh16144 Wxh16144 closed this Sep 15, 2023
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: 在自定义组件属性中填写TS类型字符串,被解析器识别为html标签。
2 participants