We’re required to have a “last updated” message on all of our content. The following node script will inject a lastUpdate property into the front matter of a file on change. If you’re using a build tool like gulp, grunt or npm scripts this will automate the process of adding lastUpdate to any file that you modify that contains front-matter.
(function () {
'use strict';
var chokidar = require('chokidar');
var matter = require('gray-matter');
var replace = require('replace-in-file');
var moment = require('moment');
var yaml = require('yamljs');
// If you want to use something other than lastUpdate change this var
var propertyName = 'lastUpdate';
// Where is your content located?
chokidar.watch('content/*.{md,html}').on('change', updateFrontMatter);
function updateFrontMatter(path) {
var regex = /^---[\s\S]*?---/;
var fm = matter.read(path);
fm.data[propertyName] = moment().format('MMMM-DD-YYYY');
var output = '---\n' + yaml.stringify(fm.data) + '---';
replace({
files: path,
replace: regex,
with: output
}, function (err, files) {
if (err) return console.error(err);
console.log('Modified files: ', files.join(', '));
});
}
})();