import path from "path" ; import sizeOf from "image-size" ; import { visit } from "unist-util-visit" ; /**
 * Customized for use case from https://github.com/ksoichiro/rehype-img-size
 */ export function rehypeImgSize ( options ) { const opts = options || { } ; const dir = opts . dir ; return transformer ; function transformer ( tree ) { visit ( tree , "element" , visitor ) ; function visitor ( node ) { if ( node . tagName === "img" && node . properties . src . slice ( - 3 ) !== "mp4" ) { let src = node . properties . src ; if ( src . startsWith ( "http" ) ) { return ; } if ( dir && src . startsWith ( "/" ) ) { src = path . join ( dir , src ) ; } const dimensions = sizeOf ( src ) ; node . properties . width = dimensions . width ; node . properties . height = dimensions . height ; } } } }