Skip to content

Commit

Permalink
parse yaml frontmatter using an actual library
Browse files Browse the repository at this point in the history
handle frontmatter with no markdown after it
test coverage for this scenario
tested annotations, pattern type/subtype markdown/yaml, and pattern markdown/yaml

close 396
  • Loading branch information
bmuenzenmeyer committed Aug 14, 2016
1 parent 230210f commit 9c44844
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 30 deletions.
35 changes: 14 additions & 21 deletions core/lib/markdown_parser.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use strict";

var md = require('markdown-it')();
var yaml = require('js-yaml');

var markdown_parser = function () {

Expand All @@ -11,31 +12,23 @@ var markdown_parser = function () {
//for each block process the yaml frontmatter and markdown
var frontmatterRE = /---\r?\n{1}([\s\S]*)---\r?\n{1}([\s\S]*)+/gm;
var chunks = frontmatterRE.exec(block);
if (chunks && chunks[1]) {

//convert each yaml frontmatter key / value into an object key
var frontmatter = chunks[1];
var frontmatterLines = frontmatter.split(/\n/gm);
for (var j = 0; j < frontmatterLines.length; j++) {

var frontmatterLine = frontmatterLines[j];
if (frontmatterLine.length > 0) {

var frontmatterLineChunks = frontmatterLine.split(':'); //test this
var frontmatterKey = frontmatterLineChunks[0].toLowerCase().trim();
var frontmatterValueString = frontmatterLineChunks[1].trim();

returnObject[frontmatterKey] = frontmatterValueString;
}

if (chunks) {
//we got some frontmatter
if (chunks && chunks[1]) {
//parse the yaml if we got it
var frontmatter = chunks[1];
returnObject = yaml.safeLoad(frontmatter);
}
}

if (chunks && chunks[2]) {
//parse the actual markdown
returnObject.markdown = md.render(chunks[2]);
if (chunks[2]) {
//parse the actual markdown if it exists
returnObject.markdown = md.render(chunks[2]);
} else {
returnObject.markdown = '';
}
} else {
//assume the passed in block is raw markdown
//assume the block was only markdown
returnObject.markdown = md.render(block);
}
} catch (ex) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"diveSync": "^0.3.0",
"fs-extra": "^0.30.0",
"glob": "^7.0.0",
"js-yaml": "^3.6.1",
"json5": "^0.5.0",
"lodash": "~4.13.1",
"markdown-it": "^6.0.1",
Expand Down
3 changes: 3 additions & 0 deletions test/files/_patterns/00-test/03-styled-atom.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
status: inprogress
---
2 changes: 1 addition & 1 deletion test/files/nav.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
el: #nav
el: '#nav'
title : Navigation
---
Navigation for adaptive web experiences can be tricky. Refer to [these repsonsive patterns](https://bradfrost.github.io/this-is-responsive/patterns.html#navigation) when evaluating solutions.
Expand Down
28 changes: 20 additions & 8 deletions test/markdown_parser_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,46 @@

var path = require('path');
var fs = require('fs-extra');
var eol = require('os').EOL;
var mp = require('../core/lib/markdown_parser');
var markdown_parser = new mp();

exports['markdown_parser'] = {
'parses pattern description block correctly when frontmatter not present' : function(test){
//arrange
'parses pattern description block correctly when frontmatter not present' : function(test) {
//arrange
var markdownFileName = path.resolve("./test/files/_patterns/00-test/00-foo.md");
var markdownFileContents = fs.readFileSync(markdownFileName, 'utf8');

//act
var returnObject = markdown_parser.parse(markdownFileContents)
//act
var returnObject = markdown_parser.parse(markdownFileContents);

//assert
//assert
test.equals(returnObject.markdown, '<h2>A Simple Include</h2>\n<p>This pattern contains an include of <code>test-bar</code>. It also has this markdown file, which does not have frontmatter.</p>\n');
test.done();
},
'parses pattern description block correctly when frontmatter present' : function(test){
'parses pattern description block correctly when frontmatter present' : function (test) {
//arrange
var markdownFileName = path.resolve("./test/files/_patterns/00-test/01-bar.md");
var markdownFileContents = fs.readFileSync(markdownFileName, 'utf8');

//act
var returnObject = markdown_parser.parse(markdownFileContents)
var returnObject = markdown_parser.parse(markdownFileContents);

//assert
test.equals(returnObject.markdown, '<h2>A Simple Bit of Markup</h2>\n<p>Foo cannot get simpler than bar, amiright?</p>\n');
test.equals(returnObject.status, 'complete');
test.done();
},
'parses frontmatter only when no markdown present': function (test) {
//arrange
var markdownFileName = path.resolve("./test/files/_patterns/00-test/03-styled-atom.md");
var markdownFileContents = fs.readFileSync(markdownFileName, 'utf8');

//act
var returnObject = markdown_parser.parse(markdownFileContents);

//assert
test.equals(returnObject.markdown, '');
test.equals(returnObject.status, 'inprogress');
test.done();
}
};

0 comments on commit 9c44844

Please sign in to comment.