-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrate.js
32 lines (32 loc) · 1.1 KB
/
migrate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
const glob = require("glob");
const fs = require("fs");
const path = require("path");
const sizeOf = require('image-size');
glob("content/projects/*.mdx", (error, files) => {
if (error) {
console.log(error)
}
else {
files.forEach(function (file) {
const slug = path.basename(file, path.extname(file));
console.log(slug);
fs.readFile(file, function (err, data) {
if (err) {
console.log(err);
} else {
const newData = data.toString().replace(/!\[(.+)\]\((.+)\)/g, (match, alt, src) => {
console.log(match, alt, src);
const dimensions = sizeOf('content/uploads/' + slug + '/' + src);
const newstring = `<Image src="${src}" alt="${alt}" width={${dimensions.width}} height={${dimensions.height}} />`;
return newstring;
});
console.log(newData);
fs.writeFile(file, newData, 'utf8', function (err) {
if (err) return console.log(err);
console.log(`${slug} updated`);
});
}
});
});
}
});