Permalink
Browse files

Initial commit.

  • Loading branch information...
0 parents commit 0a6d0ba00558f465382481c6af71b2ac594cb41e @jprichardson committed Oct 30, 2012
Showing with 285 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +3 −0 CHANGELOG.md
  3. +15 −0 LICENSE
  4. +36 −0 README.md
  5. +67 −0 lib/markdown-page.js
  6. +32 −0 package.json
  7. +128 −0 test/markdown-page.test.js
  8. +3 −0 test/mocha.opts
  9. 0 test/resources/.gitkeep
@@ -0,0 +1 @@
+node_modules/
@@ -0,0 +1,3 @@
+0.0.1 / 2012-10-30
+------------------
+* Initial release.
15 LICENSE
@@ -0,0 +1,15 @@
+(The MIT License)
+
+Copyright (c) 2012, JP Richardson <jprichardson@gmail.com>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
+(the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify,
+ merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+ furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
+WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
+OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,36 @@
+Node.js - markdown-page
+================
+
+Parse a markdown article or page.
+
+
+Why?
+----
+
+I wanted to extract some code from [Potter](http://pottercms.com).
+
+
+
+
+Installation
+------------
+
+ npm install markdown-page
+
+
+
+Example
+------
+
+Don't use it now. Look at the test file for examples. Don't use it.
+
+
+
+License
+-------
+
+(MIT License)
+
+Copyright 2012, JP Richardson <jprichardson@gmail.com>
+
+
@@ -0,0 +1,67 @@
+/*this will probably be made browser compatible at some point in time*/
+;(function(root){
+ var marked = require('marked')
+ , mde = require('markdown-extra')
+ , S = require('string')
+ , hl = require('highlight').Highlight
+
+
+ function MarkdownPage(text) {
+ this.text = text;
+ this.metadata = {};
+ this.title = null;
+ this.markdown = null;
+
+ marked.setOptions({gfm: true, pedantic: false, sanitize: true, highlight: function(code, lang) {
+ return hl(code);
+ }});
+ }
+
+ MarkdownPage.prototype.parse = function(callback) {
+ //this is actually just a sync method, but in the future, may call out to other executables or services
+ this.metadata = mde.metadata(this.text, parseMetadata);
+ this.title = mde.heading(this.text);
+ this.markdown = mde.content(this.text);
+ this.html = marked(this.markdown);
+
+ callback(null);
+ }
+
+ MarkdownPage.prototype.slug = function(slugString) {
+ if (slugString) return S(slugString).slugify().s;
+ if (this.metadata.slug) return this.metadata.slug;
+ return S(this.title).slugify().s;
+ }
+
+ /*************************
+ * STATIC CLASS Methods
+ *************************/
+
+
+ MarkdownPage.create = function(text) {
+ return new MarkdownPage(text);
+ }
+
+
+ /*************************
+ * EXPORTS
+ *************************/
+
+ root.MarkdownPage = MarkdownPage;
+
+
+ /*************************
+ * PRIVATE
+ *************************/
+
+ function parseMetadata(metaText){
+ var metaObj = {};
+ metaText.split('\n').forEach(function(line) {
+ var data = line.split(':');
+ metaObj[data[0].trim()] = data[1].trim();
+ });
+ return metaObj;
+ }
+
+
+})(module.exports || this);
@@ -0,0 +1,32 @@
+{
+ "name": "markdown-page",
+ "version": "0.0.1",
+ "description": "",
+ "homepage": [
+ ""
+ ],
+ "repository": {
+ "type": "git",
+ "url": ""
+ },
+ "keywords": [],
+ "author": "JP Richardson <jprichardson@gmail.com>",
+ "licenses": [
+ {
+ "type": "MIT",
+ "url": ""
+ }
+ ],
+ "dependencies": {
+ "marked": "~0.2.5",
+ "markdown-extra": "0.0.2",
+ "string": "~1.1.0"
+ },
+ "devDependencies": {
+ "testutil": "~0.2.4"
+ },
+ "main": "./lib/markdown-page.js",
+ "scripts": {
+ "test": "mocha test"
+ }
+}
@@ -0,0 +1,128 @@
+var testutil = require('testutil')
+ , MarkdownPage = require('../lib/markdown-page').MarkdownPage;
+
+var data = [""
+ , "<!--"
+ , "author: JP Richardson"
+ , "publish: 2012-03-04"
+ , "tags: programming, node.js"
+ , "anything: can write anything"
+ , "-->"
+ , ""
+ , "Node.js is a Fun Platform"
+ , "============================"
+ , ""
+ , "Here is some node.js code:"
+ , ""
+ , "```javascript"
+ , "function(){"
+ , " console.log('hi');"
+ , "}"
+ , "```"
+ , ""].join('\n');
+
+describe('MarkdownPage', function() {
+ var MDP = null;
+ beforeEach(function(done) {
+ MDP = MarkdownPage.create(data);
+ MDP.parse(done);
+ })
+
+ describe('new', function() {
+ it ('should create a new MarkdownPage', function() {
+ var mdp = new MarkdownPage();
+ T (mdp)
+ })
+
+ it ('should create a new MarkdownPage with markdown text', function() {
+ var mdp = new MarkdownPage(data);
+ T (mdp)
+ T (mdp.text === data)
+ })
+ })
+
+ describe('+ create()', function() {
+ it ('should create a new MarkdownPage', function() {
+ var mdp = MarkdownPage.create();
+ T (mdp)
+ })
+
+ it ('should create a new MarkdownPage with markdown text', function() {
+ var mdp = MarkdownPage.create(data);
+ T (mdp)
+ T (mdp.text === data)
+ })
+ })
+
+ describe('- html', function() {
+ it ('should be the converted html only', function() {
+ console.log(MDP.html)
+ T (MDP.html.length > 0)
+ })
+ })
+
+
+ describe('- markdown', function() {
+ it ('should be the markdown only', function() {
+ T (MDP.markdown.length > 0)
+ })
+ })
+
+ describe('- parse()', function() {
+ it ('should parse the markdown text without error', function(done) {
+ var mdp = MarkdownPage.create(data);
+ mdp.parse(function(err) {
+ F (err);
+ done();
+ })
+ })
+ })
+
+ describe('- text', function() {
+ it ('should be the text passed into the constructor', function() {
+ T (MDP.text === data)
+ })
+ })
+
+ describe('- title', function() {
+ it('should retrieve the title', function() {
+ T (MDP.title === 'Node.js is a Fun Platform')
+ })
+ })
+
+ describe('- metadata', function() {
+ it ('should retrieve the metadata', function(){
+ T (MDP.metadata.author)
+ T (MDP.metadata.tags)
+ T (MDP.metadata.publish)
+ T (MDP.metadata.anything)
+ })
+ })
+
+ describe('- slug([textString])', function() {
+ describe('> when textString parameter is present', function() {
+ it ('should slugify the string', function() {
+ T (MDP.slug('Smart Programming') === 'smart-programming')
+ })
+ })
+
+ describe('> when a slug metadata field is present', function() {
+ it ('should use the metadata field verbatim', function() {
+ MDP.metadata.slug = 'some-slug'
+ T (MDP.slug() === 'some-slug')
+ })
+ })
+
+ describe('> when neither a parameter is present or a slug metadata field is present, slugify the title', function() {
+ it ('should slugify the title', function() {
+ T (MDP.slug() === 'nodejs-is-a-fun-platform')
+ })
+ })
+ })
+
+})
+
+
+
+
+
@@ -0,0 +1,3 @@
+--reporter spec
+--ui bdd
+--timeout 2000
No changes.

0 comments on commit 0a6d0ba

Please sign in to comment.