Skip to content

Commit 87c19c3

Browse files
committed
fix(rehypeImg): differentiate md image from html ones
1 parent df2b1a9 commit 87c19c3

File tree

1 file changed

+66
-9
lines changed

1 file changed

+66
-9
lines changed

src/components/mdx/Img/rehypeImg.ts

Lines changed: 66 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,45 @@ import resolveMdxUrl from '@/utils/resolveMdxUrl'
22
import type { Root } from 'hast'
33
import { visit } from 'unist-util-visit'
44

5-
import type { MdxJsxAttribute } from 'mdast-util-mdx-jsx'
5+
//
6+
// MD image
7+
//
8+
// ![](basic-example.gif)
9+
//
10+
// {
11+
// type: 'element',
12+
// tagName: 'img',
13+
// properties: { src: 'basic-example.gif', alt: '' },
14+
// children: [],
15+
// position: {
16+
// start: { line: 1, column: 1, offset: 0 },
17+
// end: { line: 1, column: 23, offset: 22 }
18+
// }
19+
// }
20+
21+
//
22+
// HTML image
23+
//
24+
// <img src="basic-example.gif" />
25+
//
26+
// {
27+
// type: 'mdxJsxFlowElement',
28+
// name: 'img',
29+
// attributes: [
30+
// {
31+
// type: 'mdxJsxAttribute',
32+
// name: 'src',
33+
// value: 'basic-example.gif',
34+
// position: [Object]
35+
// }
36+
// ],
37+
// position: {
38+
// start: { line: 3, column: 1, offset: 2 },
39+
// end: { line: 3, column: 32, offset: 33 }
40+
// },
41+
// data: { _mdxExplicitJsx: true },
42+
// children: []
43+
// }
644

745
// https://unifiedjs.com/learn/guide/create-a-rehype-plugin/
846
export function rehypeImg(
@@ -11,21 +49,40 @@ export function rehypeImg(
1149
) {
1250
return () => (tree: Root) => {
1351
visit(tree, null, function (node) {
14-
// console.log(node)
15-
if (node.type === 'mdxJsxFlowElement' && node.name === 'img') {
16-
node.name = 'Img' // map HTML <img> to <Img> React component
52+
// console.log('node', node)
53+
54+
const isMDImage = 'tagName' in node && node.tagName === 'img'
55+
const isHTMLImage = 'name' in node && node.name === 'img'
56+
57+
if (isMDImage) {
58+
node.tagName = 'Img' // map to <Img> React component
59+
60+
//
61+
// Resolve relative URLs
62+
//
63+
64+
const oldSrc = node.properties.src
65+
if (typeof oldSrc === 'string') {
66+
node.properties.src = resolveMdxUrl(oldSrc, relFilePath, MDX_BASEURL)
67+
}
68+
}
69+
70+
if (isHTMLImage) {
71+
node.name = 'Img' // map to <Img> React component
1772

1873
//
1974
// Resolve relative URLs
2075
//
2176

22-
const srcAttr = (
23-
node.attributes.filter(({ type }) => type === 'mdxJsxAttribute') as MdxJsxAttribute[]
24-
).find((attr) => attr.name === 'src')
77+
const srcAttr = node.attributes
78+
.filter((node) => 'name' in node)
79+
.find((attr) => attr.name === 'src')
2580

2681
if (srcAttr) {
27-
const src = srcAttr.value
28-
if (typeof src === 'string') srcAttr.value = resolveMdxUrl(src, relFilePath, MDX_BASEURL)
82+
const oldSrc = srcAttr?.value
83+
84+
if (typeof oldSrc === 'string')
85+
srcAttr.value = resolveMdxUrl(oldSrc, relFilePath, MDX_BASEURL)
2986
}
3087
}
3188
})

0 commit comments

Comments
 (0)