Skip to content

Commit

Permalink
Merge pull request #382 from pattern-lab/markdown-parse-error
Browse files Browse the repository at this point in the history
Parse markdown frontmatter better
  • Loading branch information
Brian Muenzenmeyer authored Jul 6, 2016
2 parents 0860540 + 0c75011 commit 4086faa
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 100 deletions.
10 changes: 8 additions & 2 deletions core/lib/markdown_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ 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] && chunks[2]) {
if (chunks && chunks[1]) {

//convert each yaml frontmatter key / value into an object key
var frontmatter = chunks[1];
Expand All @@ -25,17 +25,23 @@ var markdown_parser = function () {
var frontmatterKey = frontmatterLineChunks[0].toLowerCase().trim();
var frontmatterValueString = frontmatterLineChunks[1].trim();

returnObject[frontmatterKey] = frontmatterValueString.substring(1, frontmatterValueString.length - 1);
returnObject[frontmatterKey] = frontmatterValueString;
}

}
}

if (chunks && chunks[2]) {
//parse the actual markdown
returnObject.markdown = md.render(chunks[2]);
} else{
//assume the passed in block is raw markdown
returnObject.markdown = md.render(block);
}
} catch (ex) {
console.log(ex);
console.log('error parsing markdown block', block);
return undefined;
}

//return the frontmatter keys and markdown for a consumer to decide what to do with
Expand Down
8 changes: 6 additions & 2 deletions core/lib/pattern_assembler.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,12 @@ var pattern_assembler = function () {
console.log('found pattern-specific markdown for ' + currentPattern.patternPartial);
}
}
catch (e) {
// do nothing
catch (err) {
// do nothing when file not found
if (err.errno !== -4058) {
console.log('there was an error setting pattern keys after markdown parsing of the companion file for pattern ' + currentPattern.patternPartial);
console.log(err);
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "patternlab-node",
"description": "Pattern Lab is a collection of tools to help you create atomic design systems. This is the node command line interface (CLI).",
"version": "2.1.0",
"version": "2.1.1",
"main": "./core/lib/patternlab.js",
"dependencies": {
"diveSync": "^0.3.0",
Expand Down
3 changes: 3 additions & 0 deletions test/files/_patterns/00-test/00-foo.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## A Simple Include

This pattern contains an include of `test-bar`. It also has this markdown file, which does not have frontmatter.
6 changes: 6 additions & 0 deletions test/files/_patterns/00-test/01-bar.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
status: complete
---
## A Simple Bit of Markup

Foo cannot get simpler than bar, amiright?
12 changes: 6 additions & 6 deletions test/files/annotations.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
---
el: "header[role=banner]"
title: "Masthead"
el: header[role=banner]
title: Masthead
---
The main header of the site doesn't take up *too much screen real estate* in order to keep the focus on the core content.
It's using a linear CSS gradient instead of a background image to give greater design flexibility and reduce HTTP requests.
~*~
---
selector: ".logo"
title: "Logo"
selector: .logo
title: Logo
---
The _logo image_ is an SVG file.
~*~
---
el: "#nav"
title : "Navigation"
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.
35 changes: 35 additions & 0 deletions test/markdown_parser_tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"use strict";

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
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)

//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){
//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)

//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();
}
};
175 changes: 86 additions & 89 deletions test/style_modifier_hunter_tests.js
Original file line number Diff line number Diff line change
@@ -1,89 +1,86 @@
(function () {
"use strict";

var smh = require('../core/lib/style_modifier_hunter');

exports['consume_style_modifier'] = {
'uses the partial stylemodifer to modify the patterns extendedTemplate' : function(test){
//arrange
var pl = {};
pl.partials = {};
pl.config = {};
pl.config.debug = false;

var pattern = {
extendedTemplate: '<div class="foo {{styleModifier}}"></div>'
};

var style_modifier_hunter = new smh();

//act
style_modifier_hunter.consume_style_modifier(pattern, '{{> partial:bar}}', pl);

//assert
test.equals(pattern.extendedTemplate, '<div class="foo bar"></div>');
test.done();
},
'replaces style modifiers with spaces in the syntax' : function(test){
//arrange
var pl = {};
pl.partials = {};
pl.config = {};
pl.config.debug = false;

var pattern = {
extendedTemplate: '<div class="foo {{ styleModifier }}"></div>'
};

var style_modifier_hunter = new smh();

//act
style_modifier_hunter.consume_style_modifier(pattern, '{{> partial:bar}}', pl);

//assert
test.equals(pattern.extendedTemplate, '<div class="foo bar"></div>');
test.done();
},
'replaces multiple style modifiers' : function(test){
//arrange
var pl = {};
pl.partials = {};
pl.config = {};
pl.config.debug = false;

var pattern = {
extendedTemplate: '<div class="foo {{ styleModifier }}"></div>'
};

var style_modifier_hunter = new smh();

//act
style_modifier_hunter.consume_style_modifier(pattern, '{{> partial:bar|baz|dum}}', pl);

//assert
test.equals(pattern.extendedTemplate, '<div class="foo bar baz dum"></div>');
test.done();
},
'does not alter pattern extendedTemplate if styleModifier not found in partial' : function(test){
//arrange
var pl = {};
pl.partials = {};
pl.config = {};
pl.config.debug = false;

var pattern = {
extendedTemplate: '<div class="foo {{styleModifier}}"></div>'
};

var style_modifier_hunter = new smh();

//act
style_modifier_hunter.consume_style_modifier(pattern, '{{> partial}}', pl);

//assert
test.equals(pattern.extendedTemplate, '<div class="foo {{styleModifier}}"></div>');
test.done();
}
};

}());
"use strict";

var smh = require('../core/lib/style_modifier_hunter');

exports['consume_style_modifier'] = {
'uses the partial stylemodifer to modify the patterns extendedTemplate' : function(test){
//arrange
var pl = {};
pl.partials = {};
pl.config = {};
pl.config.debug = false;

var pattern = {
extendedTemplate: '<div class="foo {{styleModifier}}"></div>'
};

var style_modifier_hunter = new smh();

//act
style_modifier_hunter.consume_style_modifier(pattern, '{{> partial:bar}}', pl);

//assert
test.equals(pattern.extendedTemplate, '<div class="foo bar"></div>');
test.done();
},
'replaces style modifiers with spaces in the syntax' : function(test){
//arrange
var pl = {};
pl.partials = {};
pl.config = {};
pl.config.debug = false;

var pattern = {
extendedTemplate: '<div class="foo {{ styleModifier }}"></div>'
};

var style_modifier_hunter = new smh();

//act
style_modifier_hunter.consume_style_modifier(pattern, '{{> partial:bar}}', pl);

//assert
test.equals(pattern.extendedTemplate, '<div class="foo bar"></div>');
test.done();
},
'replaces multiple style modifiers' : function(test){
//arrange
var pl = {};
pl.partials = {};
pl.config = {};
pl.config.debug = false;

var pattern = {
extendedTemplate: '<div class="foo {{ styleModifier }}"></div>'
};

var style_modifier_hunter = new smh();

//act
style_modifier_hunter.consume_style_modifier(pattern, '{{> partial:bar|baz|dum}}', pl);

//assert
test.equals(pattern.extendedTemplate, '<div class="foo bar baz dum"></div>');
test.done();
},
'does not alter pattern extendedTemplate if styleModifier not found in partial' : function(test){
//arrange
var pl = {};
pl.partials = {};
pl.config = {};
pl.config.debug = false;

var pattern = {
extendedTemplate: '<div class="foo {{styleModifier}}"></div>'
};

var style_modifier_hunter = new smh();

//act
style_modifier_hunter.consume_style_modifier(pattern, '{{> partial}}', pl);

//assert
test.equals(pattern.extendedTemplate, '<div class="foo {{styleModifier}}"></div>');
test.done();
}
};

0 comments on commit 4086faa

Please sign in to comment.