src/plugins/images.js
function replaceSrc(imageMap) {
  return through.obj(function(file, enc, cb) {
    file.$el = file.$el || cheerio.load(file.contents.toString());

    var changed = false;

    // loop over each image
    file.$el("img").each(function(i, el) {
      // convert el to cheerio el
      var jel = file.$el(this);
      var src = jel.attr("src");

      // if this image exists in source folder,
      // replace src with new source
      if (imageMap[src]) {
        // this file has changed
        changed = true;

        // find the relative path from the image to the file
        var srcRelative = path.relative(
          path.dirname(file.relative),
          imageMap[src]
        );

        jel.attr("src", srcRelative);
      } else if (!src.match(/^http/)) {
        console.log("image not found in source folder: " + src);
      }
    });

    // only if we find an image, replace contents in file
    if (changed) {
      file.contents = new Buffer(file.$el("body").html());
    }

    cb(null, file);
  });
}
function replaceSrc(imageMap) {
  return through.obj(function(file, enc, cb) {
    file.$el = file.$el || cheerio.load(file.contents.toString());

    var changed = false;

    // loop over each image
    file.$el("img").each(function(i, el) {
      // convert el to cheerio el
      var jel = file.$el(this);
      var src = jel.attr("src");

      // if this image exists in source folder,
      // replace src with new source
      if (imageMap[src]) {
        // this file has changed
        changed = true;

        // find the relative path from the image to the file
        var srcRelative = path.relative(
          path.dirname(file.relative),
          imageMap[src]
        );

        jel.attr("src", srcRelative);
      } else if (!src.match(/^http/)) {
        console.log("image not found in source folder: " + src);
      }
    });

    // only if we find an image, replace contents in file
    if (changed) {
      file.contents = new Buffer(file.$el("body").html());
    }

    cb(null, file);
  });
}
function replaceSrc ( imageMap ) { return through . obj ( function ( file , enc , cb ) { file . $el = file . $el || cheerio . load ( file . contents . toString ( ) ) ; var changed = false ; // loop over each image file . $el ( "img" ) . each ( function ( i , el ) { // convert el to cheerio el var jel = file . $el ( this ) ; var src = jel . attr ( "src" ) ; // if this image exists in source folder, // replace src with new source if ( imageMap [ src ] ) { // this file has changed changed = true ; // find the relative path from the image to the file var srcRelative = path . relative ( path . dirname ( file . relative ) , imageMap [ src ] ) ; jel . attr ( "src" , srcRelative ) ; } else if ( ! src . match ( / ^http / ) ) { console . log ( "image not found in source folder: " + src ) ; } } ) ; // only if we find an image, replace contents in file if ( changed ) { file . contents = new Buffer ( file . $el ( "body" ) . html ( ) ) ; } cb ( null , file ) ; } ) ; }
through2 pipe function that replaces images src attributes based on values in hashmap.
through2 pipe function that replaces images src attributes based on values in hashmap .