Skip to content

Commit 80c28e8

Browse files
committed
abstracted annotations.md markdown parser into a shared component
- all existing tests pass - patternState and many other keys are now parsed out of pattern.md - supports pseudopatterns too #376
1 parent a525628 commit 80c28e8

File tree

5 files changed

+131
-59
lines changed

5 files changed

+131
-59
lines changed

core/lib/annotation_exporter.js

Lines changed: 18 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
"use strict";
22

3+
var path = require('path'),
4+
fs = require('fs-extra'),
5+
JSON5 = require('json5'),
6+
_ = require('lodash'),
7+
mp = require('./markdown_parser');
8+
39
var annotations_exporter = function (pl) {
4-
var path = require('path'),
5-
fs = require('fs-extra'),
6-
JSON5 = require('json5'),
7-
_ = require('lodash'),
8-
md = require('markdown-it')(),
9-
paths = pl.config.paths;
10+
11+
var paths = pl.config.paths;
1012

1113
/*
1214
Returns the array of comments that used to be wrapped in raw JS.
@@ -38,6 +40,7 @@ var annotations_exporter = function (pl) {
3840
Converts the annotations.md file yaml list into an array of annotations
3941
*/
4042
function parseAnnotationsMD() {
43+
var markdown_parser = new mp();
4144
var annotations = [];
4245

4346
//attempt to read the file
@@ -53,51 +56,25 @@ var annotations_exporter = function (pl) {
5356

5457
//take the annotation snippets and split them on our custom delimiter
5558
var annotationsYAML = annotationsMD.split('~*~');
59+
5660
for (var i = 0; i < annotationsYAML.length; i++) {
5761
var annotation = {};
5862

59-
//for each annotation process the yaml frontmatter and markdown
60-
var annotationSnippet = annotationsYAML[i];
61-
var annotationsRE = /---\r?\n{1}([\s\S]*)---\r?\n{1}([\s\S]*)+/gm;
62-
var chunks = annotationsRE.exec(annotationSnippet);
63-
if (chunks && chunks[1] && chunks[2]) {
64-
65-
//convert each yaml frontmatter key into an object key
66-
var frontmatter = chunks[1];
67-
var frontmatterLines = frontmatter.split(/\n/gm);
68-
for (var j = 0; j < frontmatterLines.length; j++) {
69-
var frontmatterLine = frontmatterLines[j];
70-
if (frontmatterLine.length > 0) {
71-
var frontmatterLineChunks = frontmatterLine.split(':'); //test this
72-
var frontmatterKey = frontmatterLineChunks[0].toLowerCase().trim();
73-
var frontmatterValueString = frontmatterLineChunks[1].trim();
74-
var frontmatterValue = frontmatterValueString.substring(1, frontmatterValueString.length - 1);
75-
if (frontmatterKey === 'el' || frontmatterKey === 'selector') {
76-
annotation.el = frontmatterValue;
77-
}
78-
if (frontmatterKey === 'title') {
79-
annotation.title = frontmatterValue;
80-
}
81-
}
82-
}
83-
84-
//set the comment to the parsed markdown
85-
var annotationMarkdown = chunks[2];
86-
annotation.comment = md.render(annotationMarkdown);
87-
88-
annotations.push(annotation);
89-
} else {
90-
console.log('annotations.md file not formatted as expected. Error parsing frontmatter and markdown out of ' + annotationSnippet);
91-
}
63+
var markdownObj = markdown_parser.parse(annotationsYAML[i]);
64+
65+
annotation.el = markdownObj.el || markdownObj.selector;
66+
annotation.title = markdownObj.title;
67+
annotation.comment = markdownObj.markdown;
68+
69+
annotations.push(annotation);
9270
}
9371
return annotations;
9472
}
9573

9674
function gatherAnnotations() {
9775
var annotationsJS = parseAnnotationsJS();
9876
var annotationsMD = parseAnnotationsMD();
99-
var mergedAnnotations = _.unionBy(annotationsJS, annotationsMD, 'el');
100-
return mergedAnnotations;
77+
return _.unionBy(annotationsJS, annotationsMD, 'el');
10178
}
10279

10380
return {

core/lib/markdown_parser.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
"use strict";
2+
3+
var md = require('markdown-it')();
4+
5+
var markdown_parser = function () {
6+
7+
function parseMarkdownBlock(block) {
8+
var returnObject = {};
9+
10+
try {
11+
//for each block process the yaml frontmatter and markdown
12+
var frontmatterRE = /---\r?\n{1}([\s\S]*)---\r?\n{1}([\s\S]*)+/gm;
13+
var chunks = frontmatterRE.exec(block);
14+
if (chunks && chunks[1] && chunks[2]) {
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.substring(1, frontmatterValueString.length - 1);
29+
}
30+
31+
}
32+
33+
//parse the actual markdown
34+
returnObject.markdown = md.render(chunks[2]);
35+
}
36+
} catch (ex) {
37+
console.log(ex);
38+
console.log('error parsing markdown block', block);
39+
}
40+
41+
//return the frontmatter keys and markdown for a consumer to decide what to do with
42+
return returnObject;
43+
}
44+
45+
return {
46+
parse: function (block) {
47+
return parseMarkdownBlock(block);
48+
}
49+
};
50+
51+
};
52+
53+
module.exports = markdown_parser;

core/lib/pattern_assembler.js

Lines changed: 57 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ var pattern_assembler = function () {
55
fs = require('fs-extra'),
66
Pattern = require('./object_factory').Pattern,
77
pph = require('./pseudopattern_hunter'),
8-
md = require('markdown-it')(),
8+
mp = require('./markdown_parser'),
99
plutils = require('./utilities'),
1010
patternEngines = require('./pattern_engines');
1111

@@ -66,6 +66,9 @@ var pattern_assembler = function () {
6666
}
6767
}
6868

69+
/*
70+
* Deprecated in favor of .md 'status' frontmatter inside a pattern. Still used for unit tests at this time.
71+
*/
6972
function setState(pattern, patternlab) {
7073
if (patternlab.config.patternStates && patternlab.config.patternStates[pattern.patternPartial]) {
7174
pattern.patternState = patternlab.config.patternStates[pattern.patternPartial];
@@ -123,6 +126,54 @@ var pattern_assembler = function () {
123126
}
124127
}
125128

129+
function parsePatternMarkdown(currentPattern, patternlab) {
130+
131+
var markdown_parser = new mp();
132+
133+
try {
134+
var markdownFileName = path.resolve(patternlab.config.paths.source.patterns, currentPattern.subdir, currentPattern.fileName + ".md");
135+
var markdownFileContents = fs.readFileSync(markdownFileName, 'utf8');
136+
137+
var markdownObject = markdown_parser.parse(markdownFileContents);
138+
if (!plutils.isObjectEmpty(markdownObject)) {
139+
//set keys and markdown itself
140+
currentPattern.patternDescExists = true;
141+
currentPattern.patternDesc = markdownObject.markdown;
142+
143+
//consider looping through all keys eventually. would need to blacklist some properties and whitelist others
144+
if (markdownObject.status) {
145+
currentPattern.patternState = markdownObject.status;
146+
}
147+
if (markdownObject.order) {
148+
currentPattern.order = markdownObject.order;
149+
}
150+
if (markdownObject.hidden) {
151+
currentPattern.hidden = markdownObject.hidden;
152+
}
153+
if (markdownObject.excludeFromStyleguide) {
154+
currentPattern.excludeFromStyleguide = markdownObject.excludeFromStyleguide;
155+
}
156+
if (markdownObject.tags) {
157+
currentPattern.tags = markdownObject.tags;
158+
}
159+
if (markdownObject.links) {
160+
currentPattern.links = markdownObject.links;
161+
}
162+
} else {
163+
if (patternlab.config.debug) {
164+
console.log('error processing markdown for ' + currentPattern.patternPartial);
165+
}
166+
}
167+
168+
if (patternlab.config.debug) {
169+
console.log('found pattern-specific markdown for ' + currentPattern.patternPartial);
170+
}
171+
}
172+
catch (e) {
173+
// do nothing
174+
}
175+
}
176+
126177
function processPatternIterative(relPath, patternlab) {
127178

128179
var pseudopattern_hunter = new pph();
@@ -149,7 +200,7 @@ var pattern_assembler = function () {
149200
}
150201

151202
//see if this file has a state
152-
setState(currentPattern, patternlab);
203+
//setState(currentPattern, patternlab);
153204

154205
//look for a json file for this template
155206
try {
@@ -193,18 +244,7 @@ var pattern_assembler = function () {
193244
}
194245

195246
//look for a markdown file for this template
196-
try {
197-
var markdownFileName = path.resolve(patternlab.config.paths.source.patterns, currentPattern.subdir, currentPattern.fileName + ".md");
198-
var markdownFileContents = fs.readFileSync(markdownFileName, 'utf8');
199-
currentPattern.patternDescExists = true;
200-
currentPattern.patternDesc = md.render(markdownFileContents);
201-
if (patternlab.config.debug) {
202-
console.log('found pattern-specific markdown-documentation.md for ' + currentPattern.patternPartial);
203-
}
204-
}
205-
catch (e) {
206-
// do nothing
207-
}
247+
parsePatternMarkdown(currentPattern, patternlab);
208248

209249
//add the raw template to memory
210250
currentPattern.template = fs.readFileSync(path.resolve(patternsPath, relPath), 'utf8');
@@ -397,6 +437,9 @@ var pattern_assembler = function () {
397437
},
398438
parse_data_links_specific: function (patternlab, data, label) {
399439
return parseDataLinksHelper(patternlab, data, label)
440+
},
441+
parse_pattern_markdown: function (pattern, patternlab) {
442+
parsePatternMarkdown(pattern, patternlab);
400443
}
401444
};
402445

core/lib/pseudopattern_hunter.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ var pseudopattern_hunter = function () {
3838
console.log('There was an error parsing pseudopattern JSON for ' + currentPattern.relPath);
3939
console.log(err);
4040
}
41-
41+
4242
//extend any existing data with variant data
4343
variantFileData = plutils.mergeData(currentPattern.jsonFileData, variantFileData);
4444

@@ -56,8 +56,8 @@ var pseudopattern_hunter = function () {
5656
engine: currentPattern.engine
5757
});
5858

59-
//see if this file has a state
60-
pattern_assembler.setPatternState(patternVariant, patternlab);
59+
//process the companion markdown file if it exists
60+
pattern_assembler.parse_pattern_markdown(patternVariant, patternlab);
6161

6262
//find pattern lineage
6363
lineage_hunter.find_lineage(patternVariant, patternlab);

patternlab-config.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
"ishMaximum": "2600",
5252
"patternStateCascade": ["inprogress", "inreview", "complete"],
5353
"patternStates": {
54-
"molecules-block-hero" : "inreview"
5554
},
5655
"patternExportPatternPartials": [],
5756
"patternExportDirectory": "./pattern_exports/",

0 commit comments

Comments
 (0)