Skip to content

Instantly share code, notes, and snippets.

@hughgrigg
Created May 11, 2015 05:43
Show Gist options
  • Save hughgrigg/b44fb2313b89576d8686 to your computer and use it in GitHub Desktop.
Save hughgrigg/b44fb2313b89576d8686 to your computer and use it in GitHub Desktop.
Gulp insert front-matter date from filename
// Throwaway Gulp script to insert dates from file-names into the front-matter
// of content source files
var gulp = require('gulp'),
through = require('through2');
gulp.task('insert-dates', function() {
return gulp.src('content/posts/**/*.md')
.pipe(through.obj(
function(file, enc, callback) {
var pathParts = file.path.split('/'),
pathDate = pathParts[pathParts.length - 1].substr(0, 10);
file.contents = new Buffer(String(file.contents).replace(
/match line of front matter to insert date after/,
'match line of front matter to insert date after\ndate: ' + pathDate
));
return callback(null, file);
})
)
.pipe(gulp.dest('content/posts'));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment