@@ -21,24 +21,32 @@ const SIZE_FROM_ALT_RE = /\s*=\s*(\d+)?x?(\d+)?\s*$/;
2121export default function remarkImageResize ( ) {
2222 return tree =>
2323 visit ( tree , { type : 'mdxJsxTextElement' , name : 'img' } , node => {
24-
25- // Handle MDX JSX <img> produced by remark-mdx-images
26- const altIndex = node . attributes . findIndex ( a => a && a . name === 'alt' ) ;
27- const altValue = altIndex !== - 1 && typeof node . attributes [ altIndex ] . value === 'string'
28- ? node . attributes [ altIndex ] . value
29- : null ;
30- if ( altValue ) {
31- const m = altValue . match ( SIZE_FROM_ALT_RE ) ;
32- if ( m ) {
33- const [ , wStr , hStr ] = m ;
34- const cleanedAlt = altValue . replace ( SIZE_FROM_ALT_RE , '' ) . trim ( ) ;
35- // set cleaned alt
36- node . attributes [ altIndex ] = { type : 'mdxJsxAttribute' , name : 'alt' , value : cleanedAlt } ;
37- // remove any pre-existing width/height attributes to avoid duplicates
38- node . attributes = node . attributes . filter ( a => ! ( a && ( a . name === 'width' || a . name === 'height' ) ) ) ;
39- if ( wStr ) node . attributes . push ( { type : 'mdxJsxAttribute' , name : 'width' , value : wStr } ) ;
40- if ( hStr ) node . attributes . push ( { type : 'mdxJsxAttribute' , name : 'height' , value : hStr } ) ;
24+ // Handle MDX JSX <img> produced by remark-mdx-images
25+ const altIndex = node . attributes . findIndex ( a => a && a . name === 'alt' ) ;
26+ const altValue =
27+ altIndex !== - 1 && typeof node . attributes [ altIndex ] . value === 'string'
28+ ? node . attributes [ altIndex ] . value
29+ : null ;
30+ if ( altValue ) {
31+ const m = altValue . match ( SIZE_FROM_ALT_RE ) ;
32+ if ( m ) {
33+ const [ , wStr , hStr ] = m ;
34+ const cleanedAlt = altValue . replace ( SIZE_FROM_ALT_RE , '' ) . trim ( ) ;
35+ // set cleaned alt
36+ node . attributes [ altIndex ] = {
37+ type : 'mdxJsxAttribute' ,
38+ name : 'alt' ,
39+ value : cleanedAlt ,
40+ } ;
41+ // remove any pre-existing width/height attributes to avoid duplicates
42+ node . attributes = node . attributes . filter (
43+ a => ! ( a && ( a . name === 'width' || a . name === 'height' ) )
44+ ) ;
45+ if ( wStr )
46+ node . attributes . push ( { type : 'mdxJsxAttribute' , name : 'width' , value : wStr } ) ;
47+ if ( hStr )
48+ node . attributes . push ( { type : 'mdxJsxAttribute' , name : 'height' , value : hStr } ) ;
49+ }
4150 }
42- }
4351 } ) ;
4452}
0 commit comments