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

feat: support to handle img source from local #1163

Closed
wants to merge 1 commit into from

Conversation

timeTravelCYN
Copy link

@timeTravelCYN timeTravelCYN commented Aug 9, 2022

#1157
rehypeImg 相对路径图片引入

@vercel
Copy link

vercel bot commented Aug 9, 2022

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

1 Ignored Deployment
Name Status Preview Updated
dumi ⬜️ Ignored (Inspect) Aug 9, 2022 at 8:24AM (UTC)

@PeachScript PeachScript added the 🚩 2.0 dumi 2.0 related label Aug 9, 2022
@PeachScript
Copy link
Member

@timeTravelCYN 合并下 master 最新代码,参考已有 case 加一条单独的用例

@@ -32,6 +33,7 @@ export default async (raw: string, opts: IMdTransformerOptions) => {
fileAbsPath: opts.fileAbsPath,
})
.use(rehypeJsxify)
.use(rehypeImg)
Copy link
Member

Choose a reason for hiding this comment

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

rehypeImg 放在 rehypeStrip 后面,rehypeJsxify 是 Compiler 始终放在最后

})();

function isRelativeUrl(url: string) {
return typeof url === 'string' && !/^(?:(?:blob:)?\w+:)?\/\//.test(url) && !path.isAbsolute(url);
Copy link
Member

Choose a reason for hiding this comment

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

判断 string 应该不需要,正则 ?: 应该不需要

// use wrapper element to workaround for skip props escape
// https://github.com/mapbox/jsxtreme-markdown/blob/main/packages/hast-util-to-jsx/index.js#L159
// eslint-disable-next-line no-new-wrappers
node.properties!.src = `require('${decodeURI(src)}')`;
Copy link
Member

Choose a reason for hiding this comment

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

dumi 2 换了 jsx 转换的工具,包装对象的玩法不一定奏效,要验证一下

Copy link
Author

Choose a reason for hiding this comment

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

@PeachScript 请问这个需要怎么验证,目前还没有完全熟悉 dumi 的流程,所以想厚着脸皮直接请教一下😁

Copy link
Member

Choose a reason for hiding this comment

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

先验证插件编译结果是否符合预期:参考 demo 用例 加个 img 用例,在 expect.ts 里输出看编译出来的结果是不是 <img src={require('xxx')} />,如果属性变成了纯字符串,那么 require 就无法生效了

然后验证渲染:在 examples/normal 下随便加个图片(比如 dumi 官网的 logo),然后在 md 里引入,启动 dev 看能不能正常在页面展示图片

Copy link
Author

Choose a reason for hiding this comment

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

@PeachScript 这块确实是不生效了,我看了下,主要是 https://github.com/syntax-tree/estree-util-to-js/blob/main/lib/jsx.js#L47 typeLiteral,这块有什么修改的办法吗,找了一下,没找到怎么处理..

Copy link
Member

Choose a reason for hiding this comment

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

那这个任务难度应该比较高了,之前忘了这个点…你可以选择先做其他任务,我找下解法

Copy link
Author

Choose a reason for hiding this comment

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

我也尝试一下先,如果不好解决先整点别的

Copy link
Author

Choose a reason for hiding this comment

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

@PeachScript 大佬目前有什么思路吗,我想了下大概需要从 JSXAttribute 转变到 JSXExpressionContainer,或者在某个钩子里直接一开始把 img 转为 text(<img src={require('./demo.png')} />会直接被解析成 text 而不是 img)

Copy link
Member

@PeachScript PeachScript Aug 10, 2022

Choose a reason for hiding this comment

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

大佬不敢当…目前想到的思路,我们自行约定 node.jsxProps 替代 node.properties,在 rehypeJsxify 里的 toEstree 以后再遍历一遍 node,把 jsxProps 里的属性追加创建成 node 的 JSXExpressionContainer,再交给 toJs 转成 JSX

Copy link
Author

Choose a reason for hiding this comment

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

俺试一试

Copy link
Member

@PeachScript PeachScript Aug 31, 2022

Choose a reason for hiding this comment

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

JSX Props 的支持我加上了,可以同步下 master 试下

用法:node.JSXAttributes = [{ type: 'JSXAttribute', name: 'src', value: "require('./xxx')" }],Compiler 阶段会自动把 value 转换成 AST 做序列化

export default function rehypeImg(): Transformer<Root> {
return tree => {
visit<Root, 'element'>(tree, 'element', (node: Element) => {
if (isElement(node, 'img') && hasProperty(node, 'src')) {
Copy link
Member

Choose a reason for hiding this comment

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

可以直接判断 node.tagNametypeof node.properties?.src,代替 isElementhasProperty,这样可以确保类型推导正常工作

@timeTravelCYN
Copy link
Author

ok

@PeachScript
Copy link
Member

该特性由 #1226 继续跟进,后续有精力可以继续参与其他任务,感谢贡献 ❤

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🚩 2.0 dumi 2.0 related
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants