src/plugins/navigation.js
function(config, stream, extras, callback) {
    stream = stream.pipe(through.obj(function(file, enc, cb) {

      var contents = file.contents.toString();
      var changed = false;

      if(file.prev) {
        changed = true;
        file.prev.$el = file.prev.$el || cheerio.load(file.prev.contents.toString());
        var title = file.prev.$el('h1').first().text();
        contents = contents.replace(/MBINSERTPREVLABEL/g, title);
        var href = path.relative(path.dirname(file.relative), file.prev.relative);
        contents = contents.replace(/MBINSERTPREVHREF/g, href);
      }

      if(file.next) {
        changed = true;
        file.next.$el = file.next.$el || cheerio.load(file.next.contents.toString());
        var title = file.next.$el('h1').first().text();
        contents = contents.replace(/MBINSERTNEXTLABEL/g, title);
        var href = path.relative(path.dirname(file.relative), file.next.relative);
        contents = contents.replace(/MBINSERTNEXTHREF/g, href);
      }

      if(changed) {
        file.contents = new Buffer(contents);
        file.$el = undefined;
      }

      cb(null, file);

    }));
    callback(null, config, stream, extras);
  }
function(config, stream, extras, callback) {
    stream = stream.pipe(through.obj(function(file, enc, cb) {

      var contents = file.contents.toString();
      var changed = false;

      if(file.prev) {
        changed = true;
        file.prev.$el = file.prev.$el || cheerio.load(file.prev.contents.toString());
        var title = file.prev.$el('h1').first().text();
        contents = contents.replace(/MBINSERTPREVLABEL/g, title);
        var href = path.relative(path.dirname(file.relative), file.prev.relative);
        contents = contents.replace(/MBINSERTPREVHREF/g, href);
      }

      if(file.next) {
        changed = true;
        file.next.$el = file.next.$el || cheerio.load(file.next.contents.toString());
        var title = file.next.$el('h1').first().text();
        contents = contents.replace(/MBINSERTNEXTLABEL/g, title);
        var href = path.relative(path.dirname(file.relative), file.next.relative);
        contents = contents.replace(/MBINSERTNEXTHREF/g, href);
      }

      if(changed) {
        file.contents = new Buffer(contents);
        file.$el = undefined;
      }

      cb(null, file);

    }));
    callback(null, config, stream, extras);
  }
function ( config , stream , extras , callback ) { stream = stream . pipe ( through . obj ( function ( file , enc , cb ) { var contents = file . contents . toString ( ) ; var changed = false ; if ( file . prev ) { changed = true ; file . prev . $el = file . prev . $el || cheerio . load ( file . prev . contents . toString ( ) ) ; var title = file . prev . $el ( 'h1' ) . first ( ) . text ( ) ; contents = contents . replace ( / MBINSERTPREVLABEL / g , title ) ; var href = path . relative ( path . dirname ( file . relative ) , file . prev . relative ) ; contents = contents . replace ( / MBINSERTPREVHREF / g , href ) ; } if ( file . next ) { changed = true ; file . next . $el = file . next . $el || cheerio . load ( file . next . contents . toString ( ) ) ; var title = file . next . $el ( 'h1' ) . first ( ) . text ( ) ; contents = contents . replace ( / MBINSERTNEXTLABEL / g , title ) ; var href = path . relative ( path . dirname ( file . relative ) , file . next . relative ) ; contents = contents . replace ( / MBINSERTNEXTHREF / g , href ) ; } if ( changed ) { file . contents = new Buffer ( contents ) ; file . $el = undefined ; } cb ( null , file ) ; } ) ) ; callback ( null , config , stream , extras ) ; }
Pipe through all the files and insert the title and link instead of the placeholders.
Pipe through all the files and insert the title and link instead of the placeholders .