import crc from "crc" ; import path from "path" ; import imagesUtil from "../../utils/images" ; import fs from "../../utils/fs" ; import LocationUtils from "../../utils/location" ; import editHTMLElement from "./editHTMLElement" ; param { string } rootFolder param { HTMLDom } $ return { Promise } function inlinePng ( rootFolder , currentFile , $ ) { const currentDirectory = path . dirname ( currentFile ) ; return editHTMLElement ( $ , "img" , ( $img ) => { const src = $img . attr ( "src" ) ; if ( ! LocationUtils . isDataURI ( src ) ) { return ; } const hash = crc . crc32 ( src ) . toString ( 16 ) ; let fileName = `${ hash }.png` ; const filePath = path . join ( rootFolder , fileName ) ; return fs . assertFile ( filePath , ( ) => { return imagesUtil . convertInlinePNG ( src , filePath ) ; } ) . then ( ( ) => { fileName = LocationUtils . relative ( currentDirectory , fileName ) ; $img . attr ( "src" , fileName ) ; } ) ; } ) ; } export default inlinePng ;