Last active
December 14, 2015 11:09
-
-
Save mytharcher/5076912 to your computer and use it in GitHub Desktop.
Node script for migrating all images in markdown posts into figure tags.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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