Last active
August 29, 2015 13:55
-
-
Save walterhiggins/8706939 to your computer and use it in GitHub Desktop.
A hypothetical server-side javascript blog-making file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs'), | |
_ = require('underscore'), | |
markdown = require('markdown').markdown, | |
tinymake = require('tinymake'); | |
// all posts are .md (markdown files) | |
var posts = _.filter(fs.readDirSync('posts'), function(name){ | |
return name.match(/\.md$/) | |
}); | |
var pages = []; | |
// setup target to build entire blog | |
tinymake.file ( | |
'blog', // target | |
pages // source | |
// targets with no build rule are synthetic | |
// make('blog') means make all of blog's pages | |
); | |
function Markdown2HTML(htmlFile, markdownFiles){ | |
var mdContent = fs.readFileSync( markdownFiles[0], 'utf8' ), | |
htmlContent = markdown.toHTML(mdContent); | |
fs.writeFileSync(htmlFile, htmlContent, {encoding: 'utf8'}); | |
} | |
// setup targets for each .md to be turned into a .html file | |
_.forEach(posts, function(post){ | |
// posts/blogPost.md => blogPost.html | |
var page = post | |
.replace(/\.md$/, '.html') | |
.replace(/posts\//, ''); | |
tinymake.file( | |
page, // target | |
[post], // source | |
Markdown2HTML // how to build target from source | |
); | |
pages.push(page); | |
}); | |
tinymake.make('blog'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment