1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- const fs = require('fs')
- const mdYmlParser = require('markdown-yaml-metadata-parser');
- var utilities = require('./utilities');
- 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.published == true){
- posts.push(post);
- }
- }
- });
- console.log(`Found ${posts.length} posts. Creating files:`);
- var createPost = function(post){
- // Create folder
- var post_name = post.filename.split(".")[0];
- utilities.createDirectory(`source/_posts/blog/${post_name}`)
- // Copy post
- var post_path = `${source}/${post.filename}`
- var post_destination = `source/_posts/blog/${post_name}.md`;
- fs.copyFileSync(post_path, post_destination);
- // Copy hero image
- var hero_path = `${source}/images/${post.metadata.hero_image}`;
- var hero_destination = `source/_posts/blog/${post_name}/hero.jpg`
- fs.copyFileSync(hero_path, hero_destination);
- // Copy cover image
- var thumb_path = `${source}/images/${post.metadata.cover_image}`;
- var thumb_destination = `source/_posts/blog/${post_name}/cover.jpg`
- fs.copyFileSync(thumb_path, thumb_destination);
- }
- var counter = 0;
- posts.forEach(post => {
- try{
- createPost(post);
- counter += 1;
- } catch(err){
- console.log(err);
- }
- });
- //console.log(`Created ${counter} blog posts`);
|