Skip to content

Commit 9c44844

Browse files
committed
parse yaml frontmatter using an actual library
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
1 parent 230210f commit 9c44844

File tree

5 files changed

+39
-30
lines changed

5 files changed

+39
-30
lines changed

core/lib/markdown_parser.js

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"use strict";
22

33
var md = require('markdown-it')();
4+
var yaml = require('js-yaml');
45

56
var markdown_parser = function () {
67

@@ -11,31 +12,23 @@ var markdown_parser = function () {
1112
//for each block process the yaml frontmatter and markdown
1213
var frontmatterRE = /---\r?\n{1}([\s\S]*)---\r?\n{1}([\s\S]*)+/gm;
1314
var chunks = frontmatterRE.exec(block);
14-
if (chunks && chunks[1]) {
15-
16-
//convert each yaml frontmatter key / value into an object key
17-
var frontmatter = chunks[1];
18-
var frontmatterLines = frontmatter.split(/\n/gm);
19-
for (var j = 0; j < frontmatterLines.length; j++) {
20-
21-
var frontmatterLine = frontmatterLines[j];
22-
if (frontmatterLine.length > 0) {
23-
24-
var frontmatterLineChunks = frontmatterLine.split(':'); //test this
25-
var frontmatterKey = frontmatterLineChunks[0].toLowerCase().trim();
26-
var frontmatterValueString = frontmatterLineChunks[1].trim();
27-
28-
returnObject[frontmatterKey] = frontmatterValueString;
29-
}
3015

16+
if (chunks) {
17+
//we got some frontmatter
18+
if (chunks && chunks[1]) {
19+
//parse the yaml if we got it
20+
var frontmatter = chunks[1];
21+
returnObject = yaml.safeLoad(frontmatter);
3122
}
32-
}
3323

34-
if (chunks && chunks[2]) {
35-
//parse the actual markdown
36-
returnObject.markdown = md.render(chunks[2]);
24+
if (chunks[2]) {
25+
//parse the actual markdown if it exists
26+
returnObject.markdown = md.render(chunks[2]);
27+
} else {
28+
returnObject.markdown = '';
29+
}
3730
} else {
38-
//assume the passed in block is raw markdown
31+
//assume the block was only markdown
3932
returnObject.markdown = md.render(block);
4033
}
4134
} catch (ex) {

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"diveSync": "^0.3.0",
88
"fs-extra": "^0.30.0",
99
"glob": "^7.0.0",
10+
"js-yaml": "^3.6.1",
1011
"json5": "^0.5.0",
1112
"lodash": "~4.13.1",
1213
"markdown-it": "^6.0.1",
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
status: inprogress
3+
---

test/files/nav.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
el: #nav
2+
el: '#nav'
33
title : Navigation
44
---
55
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: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,46 @@
22

33
var path = require('path');
44
var fs = require('fs-extra');
5-
var eol = require('os').EOL;
65
var mp = require('../core/lib/markdown_parser');
76
var markdown_parser = new mp();
87

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

15-
//act
16-
var returnObject = markdown_parser.parse(markdownFileContents)
14+
//act
15+
var returnObject = markdown_parser.parse(markdownFileContents);
1716

18-
//assert
17+
//assert
1918
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');
2019
test.done();
2120
},
22-
'parses pattern description block correctly when frontmatter present' : function(test){
21+
'parses pattern description block correctly when frontmatter present' : function (test) {
2322
//arrange
2423
var markdownFileName = path.resolve("./test/files/_patterns/00-test/01-bar.md");
2524
var markdownFileContents = fs.readFileSync(markdownFileName, 'utf8');
2625

2726
//act
28-
var returnObject = markdown_parser.parse(markdownFileContents)
27+
var returnObject = markdown_parser.parse(markdownFileContents);
2928

3029
//assert
3130
test.equals(returnObject.markdown, '<h2>A Simple Bit of Markup</h2>\n<p>Foo cannot get simpler than bar, amiright?</p>\n');
3231
test.equals(returnObject.status, 'complete');
3332
test.done();
33+
},
34+
'parses frontmatter only when no markdown present': function (test) {
35+
//arrange
36+
var markdownFileName = path.resolve("./test/files/_patterns/00-test/03-styled-atom.md");
37+
var markdownFileContents = fs.readFileSync(markdownFileName, 'utf8');
38+
39+
//act
40+
var returnObject = markdown_parser.parse(markdownFileContents);
41+
42+
//assert
43+
test.equals(returnObject.markdown, '');
44+
test.equals(returnObject.status, 'inprogress');
45+
test.done();
3446
}
3547
};

0 commit comments

Comments
 (0)