Skip to content

Instantly share code, notes, and snippets.

@mytharcher
Last active December 14, 2015 11:09
Show Gist options
  • Select an option

  • Save mytharcher/5076912 to your computer and use it in GitHub Desktop.

Select an option

Save mytharcher/5076912 to your computer and use it in GitHub Desktop.
Node script for migrating all images in markdown posts into figure tags.
/**
* usage:
*
* $ node migrate.js path/to/posts/dir
*/
var fs = require('fs');
var path = require('path');
var ENCODING = 'utf8';
var target = process.argv[2];
fs.readdirSync(target).forEach(function (file) {
var filePath = path.join(__dirname, target, file);
var content = fs.readFileSync(filePath, ENCODING)
.replace(/\[!\[\]\((.+)\)\].+\n([^\s]+)/g, function (matcher, url, caption) {
// console.log('>>>', url);
return '<figure>' +
'<img src="' + url.replace(/\/s\d+\//, '/s800/') + '" height="267" width="400" data-origin-width="600" data-origin-height="400" />' +
'<figcaption>' + caption + '</figcaption>' +
'</figure>';
});
// console.log(content);
fs.writeFileSync(filePath, content);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment