diff --git a/CHANGELOG.md b/CHANGELOG.md index 7b8d4ee..58f86cd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,12 @@ ``` +0.2.0: + - marked module is now included and used to parse markdown for files with a markdown extension. + It is exposed to the end user as markyMark.marked. + - returned post.markdown will be populated with markdown if file has a markdown extension. + post.content will contain either the html of a processed markdown file, or the file's contents sans yaml. + - bugfixes (can't readdir a directory) + - Added to GitHub + 0.1.2: - returned posts have "content" field as well as "markdown" for now they are the same, but down the road this will change diff --git a/README.md b/README.md index 84d4db0..ce451e6 100644 --- a/README.md +++ b/README.md @@ -1,15 +1,11 @@ # Marky-mark -Marky-mark helps you consume all your markdown files used for static-site generation. +Marky-mark helps you consume all your markdown files used for static-site generation. - -## Overview - -A Node.js module that helps you write your own static site generator. -Marky-mark reads a directory of markdown files with yaml meta-data/front-matter and parses it out. +It reads a directory of files with yaml meta-data/front-matter and parses it out. +And if the extension is a markdown one it'll generate the html of that markdown. Add your favorite templating language and markdown parser to make your own static site generator. - ## Usage Let's assume you have a folder of markdown files that optionally have front-matter/meta-data, looking something like this: @@ -22,6 +18,8 @@ tags: - 90s whatever: you want --- + +## A Blog Post A blog post about how I can't believe Mark Wahlberg is Marky Mark. Written in markdown of course. @@ -46,7 +44,8 @@ The parsed result is in the meta property, but the original yaml content is stor { filename: "My Marky Mark post.md", yaml: "title: Marky Mark. A retrospective.\ntags: ...", - markdown: "\nA blog post about how I ...", + markdown: "\n## A Blog Post\n\nA blog post about how I ...", + content: "

A Blog Post

A blog post about how I ...", meta: { title: "Marky Mark. A retrospective.", tags: ["music", "90s"], @@ -64,12 +63,10 @@ And that's it. It's up to you to do something with all the data marky-mark just ## Recommended Pairings -Because marky-mark doesn't do anything but read markdown files meant for static-site generators, you'll want to pair it up with other sweet modules to create your own site generator. -(I propose that any static site generator built with marky-mark be classified as a funky bunch site generator, or a site generator that uses the funky-bunch approach.) +Because marky-mark doesn't do anything but read and parse files meant for static-site generators, you'll want to pair it up with other sweet modules to create your own site generator (the funky-bunch approach). Here are some suggested modules that are fun to use with marky-mark: -- a markdown processor (such as marked) - a templating library (EJS, Jade, eco, handlebars, mustache, etc.) - a date formatting library (such as moment) - a map/reduce or javascript object querying library (I've found taffydb to be super helpful) diff --git a/marky-mark.js b/marky-mark.js index 0ba7fab..6854297 100644 --- a/marky-mark.js +++ b/marky-mark.js @@ -20,9 +20,12 @@ var fs = require('fs'); var path = require('path'); var yaml = require('js-yaml'); +var marked = require('marked'); // markdown processor var markyMark = { + marked: marked, + // parseDirectorySync // // you give it a directory, it gives you back an array of post objects @@ -37,39 +40,47 @@ var markyMark = { var posts = []; var postFilenames = fs.readdirSync(postPath); postFilenames.forEach(function(filename) { - var extname = path.extname(filename); var filepath = path.join(postPath,filename); - var contents = fs.readFileSync(filepath, 'utf8'); - var parsedContent = { - filename: filename.replace(extname, ''), - filenameExtension: extname, - yaml: '', - markdown: '', // TODO: remove at later version - content: '', - meta: {} - }; - var lines = contents.split('\n'); - // If the first line is a front matter deal, - // collect all the lines for yaml-parsing til we reach the next --- - // we'll keep shifting lines from the front of the array and collecting them, - // until we reach the next ---. Then we stop and leave the rest for the markdown content - var frontMatter = ''; - if (lines[0].trim() === '---') { - var firstFrontMatterMarker = lines.shift(); - var line = ''; - while (line !== '---') { - frontMatter = frontMatter + line + "\n"; // since we split by \n we'll add it back here and stay true to the source doc - line = lines.shift().trim(); + if (!fs.lstatSync(filepath).isDirectory()) { + var extname = path.extname(filename); + var contents = fs.readFileSync(filepath, {encoding: 'utf8'}); + var parsedContent = { + filename: filename.replace(extname, ''), + filenameExtension: extname, + yaml: '', + markdown: '', + content: '', + meta: {} + }; + var lines = contents.split('\n'); + // If the first line is a front matter deal, + // collect all the lines for yaml-parsing til we reach the next --- + // we'll keep shifting lines from the front of the array and collecting them, + // until we reach the next ---. Then we stop and leave the rest for the markdown content + var frontMatter = ''; + if (lines[0].trim() === '---') { + var firstFrontMatterMarker = lines.shift(); + var line = ''; + while (line !== '---') { + frontMatter = frontMatter + line + "\n"; // since we split by \n we'll add it back here and stay true to the source doc + line = lines.shift().trim(); + } + } + parsedContent.yaml = frontMatter; + parsedContent.meta = yaml.load(frontMatter); + + // handle what's left of the file... + // if its markdown stick the markdown where it belongs, and the generated content in the content property + if (extname === '.md' || extname === 'markdown' || extname === 'mdown' || extname === 'mkdn' || extname === 'mkd') { + parsedContent.markdown = lines.join('\n'); + parsedContent.content = marked(parsedContent.markdown); + } else { + parsedContent.content = lines.join('\n'); } + posts.push(parsedContent); } - parsedContent.yaml = frontMatter; - parsedContent.meta = yaml.load(frontMatter); - // what's left of the lines must be markdown meant for the post... - parsedContent.markdown = lines.join('\n'); - parsedContent.content = lines.join('\n'); - posts.push(parsedContent); - }); + return posts; } diff --git a/package.json b/package.json index 8e5f536..a95c627 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "marky-mark", - "version": "0.1.2", + "version": "0.2.0", "author": "Rick Bergfalk (http://rickbergfalk.com/)", "description": "Reads a directory of markdown files with meta-data/front-matter and parses it out. Add your favorite templating language and markdown parser to make your own static site generator", "keywords": [ @@ -12,8 +12,16 @@ "utility", "jifasnif" ], + "scripts": { + "test": "node ./test/test.js" + }, "dependencies": { - "js-yaml": "2.0.x" + "js-yaml": "2.0.x", + "marked": "0.2.x" }, + "repository" : { + "type": "git", + "url": "git://github.com/rickbergfalk/marky-mark" + }, "main": "marky-mark" -} +} \ No newline at end of file diff --git a/test/posts/post1.md b/test/posts/post1.md index c5946a9..fa8329e 100644 --- a/test/posts/post1.md +++ b/test/posts/post1.md @@ -1,9 +1,8 @@ --- -date: 2012-01-01 -category: test -title: post 1 ---- +date: 2012-01-01 +category: test +title: post 1 -This is a test post. The first one. +--- -## h2 \ No newline at end of file +This is a test post. The first one. \ No newline at end of file diff --git a/test/posts/webstuff.html b/test/posts/webstuff.html new file mode 100644 index 0000000..0503b43 --- /dev/null +++ b/test/posts/webstuff.html @@ -0,0 +1 @@ +

This is some web stuff

\ No newline at end of file diff --git a/test/test.js b/test/test.js index e7fbbf3..1bebcd6 100644 --- a/test/test.js +++ b/test/test.js @@ -1,3 +1,5 @@ var mm = require('../marky-mark.js'); + +// Boring array of files var posts = mm.parseDirectorySync(__dirname + "/posts"); console.log(JSON.stringify(posts, null, 2)); \ No newline at end of file