Skip to content

Commit 4086faa

Browse files
author
Brian Muenzenmeyer
authored
Merge pull request #382 from pattern-lab/markdown-parse-error
Parse markdown frontmatter better
2 parents 0860540 + 0c75011 commit 4086faa

File tree

8 files changed

+151
-100
lines changed

8 files changed

+151
-100
lines changed

core/lib/markdown_parser.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ var markdown_parser = function () {
1111
//for each block process the yaml frontmatter and markdown
1212
var frontmatterRE = /---\r?\n{1}([\s\S]*)---\r?\n{1}([\s\S]*)+/gm;
1313
var chunks = frontmatterRE.exec(block);
14-
if (chunks && chunks[1] && chunks[2]) {
14+
if (chunks && chunks[1]) {
1515

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

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

3131
}
32+
}
3233

34+
if (chunks && chunks[2]) {
3335
//parse the actual markdown
3436
returnObject.markdown = md.render(chunks[2]);
37+
} else{
38+
//assume the passed in block is raw markdown
39+
returnObject.markdown = md.render(block);
3540
}
3641
} catch (ex) {
3742
console.log(ex);
3843
console.log('error parsing markdown block', block);
44+
return undefined;
3945
}
4046

4147
//return the frontmatter keys and markdown for a consumer to decide what to do with

core/lib/pattern_assembler.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,12 @@ var pattern_assembler = function () {
174174
console.log('found pattern-specific markdown for ' + currentPattern.patternPartial);
175175
}
176176
}
177-
catch (e) {
178-
// do nothing
177+
catch (err) {
178+
// do nothing when file not found
179+
if (err.errno !== -4058) {
180+
console.log('there was an error setting pattern keys after markdown parsing of the companion file for pattern ' + currentPattern.patternPartial);
181+
console.log(err);
182+
}
179183
}
180184
}
181185

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "patternlab-node",
33
"description": "Pattern Lab is a collection of tools to help you create atomic design systems. This is the node command line interface (CLI).",
4-
"version": "2.1.0",
4+
"version": "2.1.1",
55
"main": "./core/lib/patternlab.js",
66
"dependencies": {
77
"diveSync": "^0.3.0",
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## A Simple Include
2+
3+
This pattern contains an include of `test-bar`. It also has this markdown file, which does not have frontmatter.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
status: complete
3+
---
4+
## A Simple Bit of Markup
5+
6+
Foo cannot get simpler than bar, amiright?

test/files/annotations.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
---
2-
el: "header[role=banner]"
3-
title: "Masthead"
2+
el: header[role=banner]
3+
title: Masthead
44
---
55
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.
66
It's using a linear CSS gradient instead of a background image to give greater design flexibility and reduce HTTP requests.
77
~*~
88
---
9-
selector: ".logo"
10-
title: "Logo"
9+
selector: .logo
10+
title: Logo
1111
---
1212
The _logo image_ is an SVG file.
1313
~*~
1414
---
15-
el: "#nav"
16-
title : "Navigation"
15+
el: #nav
16+
title : Navigation
1717
---
1818
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.

test/markdown_parser_tests.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"use strict";
2+
3+
var path = require('path');
4+
var fs = require('fs-extra');
5+
var eol = require('os').EOL;
6+
var mp = require('../core/lib/markdown_parser');
7+
var markdown_parser = new mp();
8+
9+
exports['markdown_parser'] = {
10+
'parses pattern description block correctly when frontmatter not present' : function(test){
11+
//arrange
12+
var markdownFileName = path.resolve("./test/files/_patterns/00-test/00-foo.md");
13+
var markdownFileContents = fs.readFileSync(markdownFileName, 'utf8');
14+
15+
//act
16+
var returnObject = markdown_parser.parse(markdownFileContents)
17+
18+
//assert
19+
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');
20+
test.done();
21+
},
22+
'parses pattern description block correctly when frontmatter present' : function(test){
23+
//arrange
24+
var markdownFileName = path.resolve("./test/files/_patterns/00-test/01-bar.md");
25+
var markdownFileContents = fs.readFileSync(markdownFileName, 'utf8');
26+
27+
//act
28+
var returnObject = markdown_parser.parse(markdownFileContents)
29+
30+
//assert
31+
test.equals(returnObject.markdown, '<h2>A Simple Bit of Markup</h2>\n<p>Foo cannot get simpler than bar, amiright?</p>\n');
32+
test.equals(returnObject.status, 'complete');
33+
test.done();
34+
}
35+
};

test/style_modifier_hunter_tests.js

Lines changed: 86 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,86 @@
1-
(function () {
2-
"use strict";
3-
4-
var smh = require('../core/lib/style_modifier_hunter');
5-
6-
exports['consume_style_modifier'] = {
7-
'uses the partial stylemodifer to modify the patterns extendedTemplate' : function(test){
8-
//arrange
9-
var pl = {};
10-
pl.partials = {};
11-
pl.config = {};
12-
pl.config.debug = false;
13-
14-
var pattern = {
15-
extendedTemplate: '<div class="foo {{styleModifier}}"></div>'
16-
};
17-
18-
var style_modifier_hunter = new smh();
19-
20-
//act
21-
style_modifier_hunter.consume_style_modifier(pattern, '{{> partial:bar}}', pl);
22-
23-
//assert
24-
test.equals(pattern.extendedTemplate, '<div class="foo bar"></div>');
25-
test.done();
26-
},
27-
'replaces style modifiers with spaces in the syntax' : function(test){
28-
//arrange
29-
var pl = {};
30-
pl.partials = {};
31-
pl.config = {};
32-
pl.config.debug = false;
33-
34-
var pattern = {
35-
extendedTemplate: '<div class="foo {{ styleModifier }}"></div>'
36-
};
37-
38-
var style_modifier_hunter = new smh();
39-
40-
//act
41-
style_modifier_hunter.consume_style_modifier(pattern, '{{> partial:bar}}', pl);
42-
43-
//assert
44-
test.equals(pattern.extendedTemplate, '<div class="foo bar"></div>');
45-
test.done();
46-
},
47-
'replaces multiple style modifiers' : function(test){
48-
//arrange
49-
var pl = {};
50-
pl.partials = {};
51-
pl.config = {};
52-
pl.config.debug = false;
53-
54-
var pattern = {
55-
extendedTemplate: '<div class="foo {{ styleModifier }}"></div>'
56-
};
57-
58-
var style_modifier_hunter = new smh();
59-
60-
//act
61-
style_modifier_hunter.consume_style_modifier(pattern, '{{> partial:bar|baz|dum}}', pl);
62-
63-
//assert
64-
test.equals(pattern.extendedTemplate, '<div class="foo bar baz dum"></div>');
65-
test.done();
66-
},
67-
'does not alter pattern extendedTemplate if styleModifier not found in partial' : function(test){
68-
//arrange
69-
var pl = {};
70-
pl.partials = {};
71-
pl.config = {};
72-
pl.config.debug = false;
73-
74-
var pattern = {
75-
extendedTemplate: '<div class="foo {{styleModifier}}"></div>'
76-
};
77-
78-
var style_modifier_hunter = new smh();
79-
80-
//act
81-
style_modifier_hunter.consume_style_modifier(pattern, '{{> partial}}', pl);
82-
83-
//assert
84-
test.equals(pattern.extendedTemplate, '<div class="foo {{styleModifier}}"></div>');
85-
test.done();
86-
}
87-
};
88-
89-
}());
1+
"use strict";
2+
3+
var smh = require('../core/lib/style_modifier_hunter');
4+
5+
exports['consume_style_modifier'] = {
6+
'uses the partial stylemodifer to modify the patterns extendedTemplate' : function(test){
7+
//arrange
8+
var pl = {};
9+
pl.partials = {};
10+
pl.config = {};
11+
pl.config.debug = false;
12+
13+
var pattern = {
14+
extendedTemplate: '<div class="foo {{styleModifier}}"></div>'
15+
};
16+
17+
var style_modifier_hunter = new smh();
18+
19+
//act
20+
style_modifier_hunter.consume_style_modifier(pattern, '{{> partial:bar}}', pl);
21+
22+
//assert
23+
test.equals(pattern.extendedTemplate, '<div class="foo bar"></div>');
24+
test.done();
25+
},
26+
'replaces style modifiers with spaces in the syntax' : function(test){
27+
//arrange
28+
var pl = {};
29+
pl.partials = {};
30+
pl.config = {};
31+
pl.config.debug = false;
32+
33+
var pattern = {
34+
extendedTemplate: '<div class="foo {{ styleModifier }}"></div>'
35+
};
36+
37+
var style_modifier_hunter = new smh();
38+
39+
//act
40+
style_modifier_hunter.consume_style_modifier(pattern, '{{> partial:bar}}', pl);
41+
42+
//assert
43+
test.equals(pattern.extendedTemplate, '<div class="foo bar"></div>');
44+
test.done();
45+
},
46+
'replaces multiple style modifiers' : function(test){
47+
//arrange
48+
var pl = {};
49+
pl.partials = {};
50+
pl.config = {};
51+
pl.config.debug = false;
52+
53+
var pattern = {
54+
extendedTemplate: '<div class="foo {{ styleModifier }}"></div>'
55+
};
56+
57+
var style_modifier_hunter = new smh();
58+
59+
//act
60+
style_modifier_hunter.consume_style_modifier(pattern, '{{> partial:bar|baz|dum}}', pl);
61+
62+
//assert
63+
test.equals(pattern.extendedTemplate, '<div class="foo bar baz dum"></div>');
64+
test.done();
65+
},
66+
'does not alter pattern extendedTemplate if styleModifier not found in partial' : function(test){
67+
//arrange
68+
var pl = {};
69+
pl.partials = {};
70+
pl.config = {};
71+
pl.config.debug = false;
72+
73+
var pattern = {
74+
extendedTemplate: '<div class="foo {{styleModifier}}"></div>'
75+
};
76+
77+
var style_modifier_hunter = new smh();
78+
79+
//act
80+
style_modifier_hunter.consume_style_modifier(pattern, '{{> partial}}', pl);
81+
82+
//assert
83+
test.equals(pattern.extendedTemplate, '<div class="foo {{styleModifier}}"></div>');
84+
test.done();
85+
}
86+
};

0 commit comments

Comments
 (0)