Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
147 lines (126 sloc) 5.15 KB
/**
* All the standard modules so far...
**/
const fs = require("fs");
const path = require("path");
/**
* All the third party modules so far...
**/
const markdown = require("markdown-it");
const frontmatter = require("frontmatter");
const ejs = require("ejs");
yaml = require('js-yaml');
try {
var doc = yaml.safeLoad(fs.readFileSync(path.join(__dirname, 'config.yml'), 'utf8'));
console.log(doc);
} catch (e) {
console.log(e);
}
return;
/**
* Some application wide constants
**/
const md = new markdown();
/**
* Directories
**/
const src = path.join(__dirname, "src");
const dest = path.join(__dirname, "docs");
const assets = path.join(__dirname, "public");
const include = path.join(__dirname, "partials");
fs.readdir(src, (err, files) => {
if ( err ) {
console.log("Something is wrong with reading src directory!");
} else {
if ( files.length == 0 ) {
console.log("src is empty, please fill it with your thoughts!");
} else {
files.forEach( (file) => {
if ( file.indexOf(".md") > -1 ) {
fs.readFile(path.join(src, file), "utf8", (err, content) => {
if ( err ) {
console.log("There is some issue with reading file: " + file);
} else {
/**
* Get the values from font-matter
* title : Title of the post, display as header of post and inside of title tag.
* layout : Layout for the HTML, how it will going to be rendered
* thought : Content that you actually written for your blog post.
* TODO: Add name of the page, date and author fields.
**/
var post = frontmatter(content);
var title = post.data.title;
var layout = post.data.layout;
var thought = post.content;
/**
* Now checking for the layout that user have written with post.
* TODO: Separate out this as function.
**/
fs.readdir(include, (err, files) => {
if ( err ) {
console.log("There is some problem while reading layout directory!");
console.log("Please make sure you have layout directory!");
} else {
if ( files.indexOf(layout + ".ejs") > -1 ) {
console.log("Layout found, spicing things up!");
} else {
console.log("Ooops! The layout you mentioned is not found. Can you confirm the name of it?");
}
}
});
/**
* Ahh! Such a mess.
* TODO: Clean up and add the comment.
**/
var filename_wo_ext = file.substr(0, file.lastIndexOf('.'));
rendered = md.render(thought)
ejs.renderFile(path.join(include, layout + ".ejs"), {"title": title, "content" : rendered}, (err, str) => {
if ( err )
console.log(err);
output_content = str;
});
/**
* This code is just a dump process of creating an `.html`.
* TODO: Seperate out this part as function.
**/
fs.writeFile(path.join(dest, filename_wo_ext + ".html"), output_content , { flag: 'w' }, (err) => {
if ( err ) {
console.log("There is some issue while creating file : " + filename_wo_ext + ".html");
} else {
console.log("File " + file + " is converted into " + filename_wo_ext + ".html!");
}
});
}
});
}
});
}
}
});
//finally copy all the assets
function copyFileSync( source, target ) {
var targetFile = target;
//if target is a directory a new file with the same name will be created
if ( fs.existsSync( target ) ) {
if ( fs.lstatSync( target ).isDirectory() ) {
targetFile = path.join( target, path.basename( source ) );
}
}
fs.writeFileSync(targetFile, fs.readFileSync(source));
}
function copyFolderRecursiveSync( source, target ) {
var files = [];
//copy
if ( fs.lstatSync( source ).isDirectory() ) {
files = fs.readdirSync( source );
files.forEach( function ( file ) {
var curSource = path.join( source, file );
if ( fs.lstatSync( curSource ).isDirectory() ) {
copyFolderRecursiveSync( curSource, target );
} else {
copyFileSync( curSource, target );
}
} );
}
}
copyFolderRecursiveSync("public", "docs");