const fs = require('fs') const mdYmlParser = require('markdown-yaml-metadata-parser'); const getDirectories = source => fs.readdirSync(source, { withFileTypes: true }) .filter(item => item.isDirectory()) .map(item => item.name); const getFiles = source => fs.readdirSync(source, { withFileTypes: true }) .filter(item => !item.isDirectory()) .map(item => item.name); var source = "/Users/james/codex/blog" var file_names = getFiles(source); var posts = [] file_names.forEach(file_name => { if(file_name != '.DS_Store'){ var file = fs.readFileSync(`${source}/${file_name}`, "utf8"); var post = mdYmlParser(file); post.filename = file_name; if(post.metadata.draft == false){ posts.push(post); } } }); console.log(`Found ${posts.length} posts. Creating files:`); var createPost = function(post){ // Copy post var post_path = `${source}/${post.filename}` var post_name = post.filename.split(".")[0]; var post_destination = `source/_posts/blog/${post_name}.md`; fs.copyFileSync(post_path, post_destination); console.log(`- \'${post.filename}\'`); // Copy hero image var hero_path = `${source}/images/hero/${post.metadata.image.feature}`; var hero_destination = `source/images/hero/blog/${post.metadata.image.feature}` fs.copyFileSync(hero_path, hero_destination); console.log(`- \'hero/${post.metadata.image.feature}\'`); // Copy thumbnail image var thumb_path = `${source}/images/thumbnails/${post.metadata.image.feature}`; var thumb_destination = `source/images/thumbnails/blog/${post.metadata.image.feature}` fs.copyFileSync(thumb_path, thumb_destination); console.log(`- \'thumbnails/${post.metadata.image.feature}\'`); } var counter = 0; posts.forEach(post => { try{ createPost(post); counter += 1; } catch(err){ console.log(err); } }); //console.log(`Created ${counter} blog posts`);