Skip to content

Commit 781e2d5

Browse files
author
Brian Muenzenmeyer
authored
Merge pull request #380 from pattern-lab/dev
Pattern Lab Node 2.1.0
2 parents 45643c9 + 0860540 commit 781e2d5

13 files changed

+374
-329
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
[![Build Status](https://travis-ci.org/pattern-lab/patternlab-node.png?branch=master)](https://travis-ci.org/pattern-lab/patternlab-node) [![Join the chat at Gitter](https://badges.gitter.im/pattern-lab/node.svg)](https://gitter.im/pattern-lab/node)
1+
[![Build Status](https://travis-ci.org/pattern-lab/patternlab-node.png?branch=master)](https://travis-ci.org/pattern-lab/patternlab-node) ![current release](https://img.shields.io/github/release/pattern-lab/patternlab-node.svg?maxAge=2592000) ![license](https://img.shields.io/github/license/pattern-lab/patternlab-node.svg?maxAge=2592000) [![Join the chat at Gitter](https://badges.gitter.im/pattern-lab/node.svg)](https://gitter.im/pattern-lab/node)
22

33
# Pattern Lab Node Core
44

55
This repository contains the core functionality for Pattern Lab Node. Pattern Lab Core is designed to be included as a dependency within [Node Editions](https://github.com/pattern-lab?utf8=%E2%9C%93&query=edition-node).
66
If this looks **REALLY DIFFERENT** from what you expected, check out the [ChangeLog](https://github.com/pattern-lab/patternlab-node/wiki/ChangeLog).
77

88
* [Pattern Lab/Node: Gulp Edition](https://github.com/pattern-lab/edition-node-gulp) contains info how to get started within a Gulp task running environment.
9-
* [Pattern Lab/Node: Grunt Node Edition](https://github.com/pattern-lab/edition-node-grunt) contains info how to get started within a Grunt task running environment.
9+
* [Pattern Lab/Node: Grunt Edition](https://github.com/pattern-lab/edition-node-grunt) contains info how to get started within a Grunt task running environment.
1010

1111
## Core Team
1212

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/object_factory.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ var Pattern = function (relPath, data) {
4646
// name of the pattern. UPDATE: this.key is now known as this.patternPartial
4747
this.patternPartial = this.patternGroup + '-' + this.patternBaseName;
4848

49+
this.patternState = '';
4950
this.template = '';
5051
this.patternPartialCode = '';
5152
this.lineage = [];

core/lib/pattern_assembler.js

Lines changed: 69 additions & 20 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,11 +66,19 @@ var pattern_assembler = function () {
6666
}
6767
}
6868

69-
function setState(pattern, patternlab) {
69+
/*
70+
* Deprecated in favor of .md 'status' frontmatter inside a pattern. Still used for unit tests at this time.
71+
* Will be removed in future versions
72+
*/
73+
function setState(pattern, patternlab, displayDeprecatedWarning) {
7074
if (patternlab.config.patternStates && patternlab.config.patternStates[pattern.patternPartial]) {
75+
76+
if (displayDeprecatedWarning) {
77+
plutils.logRed("Deprecation Warning: Using patternlab-config.json patternStates object will be deprecated in favor of the state frontmatter key associated with individual pattern markdown files.");
78+
console.log("This feature will still work in it's current form this release (but still be overridden by the new parsing method), and will be removed in the future.");
79+
}
80+
7181
pattern.patternState = patternlab.config.patternStates[pattern.patternPartial];
72-
} else {
73-
pattern.patternState = "";
7482
}
7583
}
7684

@@ -123,6 +131,54 @@ var pattern_assembler = function () {
123131
}
124132
}
125133

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

128184
var pseudopattern_hunter = new pph();
@@ -149,7 +205,7 @@ var pattern_assembler = function () {
149205
}
150206

151207
//see if this file has a state
152-
setState(currentPattern, patternlab);
208+
setState(currentPattern, patternlab, true);
153209

154210
//look for a json file for this template
155211
try {
@@ -193,18 +249,7 @@ var pattern_assembler = function () {
193249
}
194250

195251
//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-
}
252+
parsePatternMarkdown(currentPattern, patternlab);
208253

209254
//add the raw template to memory
210255
currentPattern.template = fs.readFileSync(path.resolve(patternsPath, relPath), 'utf8');
@@ -246,14 +291,15 @@ var pattern_assembler = function () {
246291
currentPattern.extendedTemplate = currentPattern.template;
247292

248293
//find how many partials there may be for the given pattern
249-
var foundPatternPartials = currentPattern.findPartials(currentPattern);
294+
var foundPatternPartials = currentPattern.findPartials();
250295

251296
//find any listItem blocks that within the pattern, even if there are no partials
252297
list_item_hunter.process_list_item_partials(currentPattern, patternlab);
253298

254299
// expand any partials present in this pattern; that is, drill down into
255300
// the template and replace their calls in this template with rendered
256301
// results
302+
257303
if (currentPattern.engine.expandPartials && (foundPatternPartials !== null && foundPatternPartials.length > 0)) {
258304
// eslint-disable-next-line
259305
expandPartials(foundPatternPartials, list_item_hunter, patternlab, currentPattern);
@@ -371,8 +417,8 @@ var pattern_assembler = function () {
371417
find_list_items: function (pattern) {
372418
return pattern.findListItems();
373419
},
374-
setPatternState: function (pattern, patternlab) {
375-
setState(pattern, patternlab);
420+
setPatternState: function (pattern, patternlab, displayDeprecatedWarning) {
421+
setState(pattern, patternlab, displayDeprecatedWarning);
376422
},
377423
addPattern: function (pattern, patternlab) {
378424
addPattern(pattern, patternlab);
@@ -397,6 +443,9 @@ var pattern_assembler = function () {
397443
},
398444
parse_data_links_specific: function (patternlab, data, label) {
399445
return parseDataLinksHelper(patternlab, data, label)
446+
},
447+
parse_pattern_markdown: function (pattern, patternlab) {
448+
parsePatternMarkdown(pattern, patternlab);
400449
}
401450
};
402451

core/lib/patternlab.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* patternlab-node - v2.0.0 - 2016
2+
* patternlab-node - v2.1.0 - 2016
33
*
44
* Brian Muenzenmeyer, Geoff Pursell, and the web community.
55
* Licensed under the MIT license.

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

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.0.1",
4+
"version": "2.1.0",
55
"main": "./core/lib/patternlab.js",
66
"dependencies": {
77
"diveSync": "^0.3.0",

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)