-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import path from 'path'; | ||
import type { Element, Root } from 'hast'; | ||
import type { Transformer } from 'unified'; | ||
|
||
let visit: typeof import('unist-util-visit').visit; | ||
let hasProperty: typeof import('hast-util-has-property').hasProperty; | ||
let isElement: typeof import('hast-util-is-element').isElement; | ||
|
||
// workaround to import pure esm module | ||
(async () => { | ||
({ visit } = await import('unist-util-visit')); | ||
({ hasProperty } = await import('hast-util-has-property')); | ||
({ isElement } = await import('hast-util-is-element')); | ||
})(); | ||
|
||
function isRelativeUrl(url: string) { | ||
return typeof url === 'string' && !/^(?:(?:blob:)?\w+:)?\/\//.test(url) && !path.isAbsolute(url); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 判断 string 应该不需要,正则 |
||
} | ||
|
||
/** | ||
* rehype plugin to handle img source from local | ||
*/ | ||
export default function rehypeImg(): Transformer<Root> { | ||
return tree => { | ||
visit<Root, 'element'>(tree, 'element', (node: Element) => { | ||
if (isElement(node, 'img') && hasProperty(node, 'src')) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 可以直接判断 |
||
const src = node.properties!.src as string; | ||
|
||
if (isRelativeUrl(src)) { | ||
// 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)}')`; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. dumi 2 换了 jsx 转换的工具,包装对象的玩法不一定奏效,要验证一下 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @PeachScript 请问这个需要怎么验证,目前还没有完全熟悉 dumi 的流程,所以想厚着脸皮直接请教一下😁 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 先验证插件编译结果是否符合预期:参考 demo 用例 加个 img 用例,在 expect.ts 里输出看编译出来的结果是不是 然后验证渲染:在 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 那这个任务难度应该比较高了,之前忘了这个点…你可以选择先做其他任务,我找下解法 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 我也尝试一下先,如果不好解决先整点别的 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @PeachScript 大佬目前有什么思路吗,我想了下大概需要从 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 大佬不敢当…目前想到的思路,我们自行约定 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 俺试一试 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. JSX Props 的支持我加上了,可以同步下 master 试下 用法: |
||
} | ||
} | ||
}); | ||
}; | ||
} |
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.
rehypeImg 放在 rehypeStrip 后面,rehypeJsxify 是 Compiler 始终放在最后