Instantly share code, notes, and snippets.

Embed
What would you like to do?
var jade = require('jade');
var path = require('path');
var fs = require('fs');
var md = require('markdown-it')();
if (!process.argv[2]) {
console.error("must specify website directory");
return;
}
process.chdir(process.argv[2]);
var postTemplate = jade.compileFile('post-template.jade', {pretty:false});
fs.readdir('b', (err, files) => {
if (err) {
throw err;
} else {
var posts = [];
files.forEach((file) => {
if (file.indexOf('.md') != file.length-3) return;
var data = fs.readFileSync(path.join('b', file), 'utf8');
var json = [];
var inMetaSection = false;
var partOfPreview = true;
var body = [];
var preview = [];
data.split('\n').forEach((e) => {
if (e === "<!--") {
inMetaSection = true;
} else if (e === "-->") {
inMetaSection = false;
} else if (inMetaSection) {
json.push(e);
} else if (e === "<!-- cutoff -->") {
partOfPreview = false;
} else {
if (partOfPreview) {
preview.push(e);
}
body.push(e);
}
});
var meta = JSON.parse(json.join('\n'));
meta.file = file;
meta.datestamp = new Date(meta.date);
var previewMd = preview.join('\n');
var bodyMd = body.join('\n');
var post = {
meta: meta,
preview: previewMd,
body: bodyMd,
previewHtml: md.render(previewMd),
bodyHtml: md.render(bodyMd)
};
fs.writeFileSync(path.join('b', file.replace('.md', '.html')), postTemplate(post));
posts.push(post);
});
console.log(posts);
var html = jade.renderFile('index-template.jade', {posts:posts});
fs.writeFileSync('index.html', html);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment