Skip to content

Commit

Permalink
version 0.2.0 stuff
Browse files Browse the repository at this point in the history
Why I work on this when I probably won't even blog I don't know.
  • Loading branch information
rickbergfalk committed May 30, 2013
1 parent 06e4d2a commit 7ca8c2e
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 49 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
19 changes: 8 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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.
Expand All @@ -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: "<h2>A Blog Post</h2><p>A blog post about how I ...",
meta: {
title: "Marky Mark. A retrospective.",
tags: ["music", "90s"],
Expand All @@ -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)
Expand Down
69 changes: 40 additions & 29 deletions marky-mark.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}

Expand Down
14 changes: 11 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "marky-mark",
"version": "0.1.2",
"version": "0.2.0",
"author": "Rick Bergfalk <rick.bergfalk@gmail.com> (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": [
Expand All @@ -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"
}
}
11 changes: 5 additions & 6 deletions test/posts/post1.md
Original file line number Diff line number Diff line change
@@ -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
This is a test post. The first one.
1 change: 1 addition & 0 deletions test/posts/webstuff.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div><p>This is some web stuff</p></div>
2 changes: 2 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -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));

0 comments on commit 7ca8c2e

Please sign in to comment.