Search code examples
gulp

Looking for a Gulp plugin that will skip markdown files based on particular header attributes


I have markdown files that look like this

---
name: Some Name
date: '2013-09-09'
isCool: true
---
really cool text

I want to have a gulp task that only lets markdown through that has a particular property, for example isCool = true.

So I would imagine something like this

gulp.src('source/content/*/*.md')
.pipe(mdPrune({
    isCool: true
}))
.pipe(gulp.dest('build/content/cool'));

then only the markdown that had an isCool attribute in the header would end up the build/content/cool folder.


Solution

  • gulp-filter would work.

    const filter = require('gulp-filter');
    
    gulp.task('default', function () {
    
          // return true if want the file in the stream
      const myFilter = filter(function (file) {
    
        let contents = file.contents.toString();
        return contents.match('isCool: true');
      });
    
    
      return gulp.src(['./src/*.md'])    
        .pipe(myFilter)
        .pipe(gulp.dest('md'));
    });
    

    That will allow the file through if isCool: true is anywhere in the file. If that is a problem, just work on the regex to restrict it to the line after the date entry for example.

    [The filter could also be defined outside of any task if it might be reused elsewhere or you just prefer it that way.