Skip to content

Commit ddfd363

Browse files
committed
Support for pattern link.* shortcut inside pattern data objects.
Unit test to cover this Resolves #171 Marking this version 1.0.0 - having achieved broad feature parity with PL PHP v1
1 parent a66d0a8 commit ddfd363

19 files changed

+144
-15
lines changed

CHANGELOG

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
THIS CHANGELOG IS AN ATTEMPT TO DOCUMENT CHANGES TO THIS PROJECT.
22

3-
PL-node-v0.16.0
3+
PL-node-v1.0.0
44
- FIX: Resolve issue with not hiding underscored patterns.
55
- THX: Thanks @ivancamilov for reporting this regression.
66
- FIX: Fix misapplied error input class
@@ -11,6 +11,9 @@ PL-node-v0.16.0
1111
- ADD: Added unit tests for pattern states and pseudopatterns
1212
- CHG: Changed pseudopattern generation to use config.patterns.source directory instead of hardcode
1313
- CHG: Explicitly sorting patterns by name prior to building the UI
14+
- ADD: Added ability to specify link.* urls inside data objects
15+
- CHG: Incremented version to 1.0.0. Achieved near-parity with PL PHP 1!
16+
- THX: Thanks to each and every person who cared about Pattern Lab Node! - Brian
1417

1518
PL-node-v0.15.1
1619
- FIX: Resolve issue with styleModifiers not being replaced when the partial has spaces in it.

builder/lineage_hunter.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* patternlab-node - v0.16.0 - 2015
2+
* patternlab-node - v1.0.0 - 2015
33
*
44
* Brian Muenzenmeyer, and the web community.
55
* Licensed under the MIT license.

builder/list_item_hunter.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* patternlab-node - v0.16.0 - 2015
2+
* patternlab-node - v1.0.0 - 2015
33
*
44
* Brian Muenzenmeyer, and the web community.
55
* Licensed under the MIT license.

builder/media_hunter.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* patternlab-node - v0.16.0 - 2015
2+
* patternlab-node - v1.0.0 - 2015
33
*
44
* Brian Muenzenmeyer, and the web community.
55
* Licensed under the MIT license.

builder/object_factory.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* patternlab-node - v0.16.0 - 2015
2+
* patternlab-node - v1.0.0 - 2015
33
*
44
* Brian Muenzenmeyer, and the web community.
55
* Licensed under the MIT license.

builder/parameter_hunter.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* patternlab-node - v0.16.0 - 2015
2+
* patternlab-node - v1.0.0 - 2015
33
*
44
* Brian Muenzenmeyer, and the web community.
55
* Licensed under the MIT license.

builder/pattern_assembler.js

+35-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* patternlab-node - v0.16.0 - 2015
2+
* patternlab-node - v1.0.0 - 2015
33
*
44
* Brian Muenzenmeyer, and the web community.
55
* Licensed under the MIT license.
@@ -54,6 +54,7 @@
5454
}
5555

5656
function addPattern(pattern, patternlab){
57+
//add the link to the global object
5758
patternlab.data.link[pattern.patternGroup + '-' + pattern.patternName] = '/patterns/' + pattern.patternLink;
5859

5960
//only push to array if the array doesn't contain this pattern
@@ -317,6 +318,36 @@
317318
return o;
318319
}
319320

321+
//look for pattern links included in data files.
322+
//these will be in the form of link.* WITHOUT {{}}, which would still be there from direct pattern inclusion
323+
function parseDataLinks(patternlab){
324+
325+
//loop through all patterns
326+
for (var i = 0; i < patternlab.patterns.length; i++){
327+
var pattern = patternlab.patterns[i];
328+
//look for link.* such as link.pages-blog as a value
329+
var linkRE = /link.[A-z0-9-_]+/g;
330+
//convert to string for easier searching
331+
var dataObjAsString = JSON.stringify(pattern.jsonFileData);
332+
var linkMatches = dataObjAsString.match(linkRE);
333+
334+
//if no matches found, escape current loop iteration
335+
if(linkMatches === null) { continue; }
336+
337+
for(var i = 0; i < linkMatches.length; i++){
338+
//for each match, find the expanded link within the already constructed patternlab.data.link object
339+
var expandedLink = patternlab.data.link[linkMatches[i].split('.')[1]];
340+
if(patternlab.config.debug){
341+
console.log('expanded data link from ' + linkMatches[i] + ' to ' + expandedLink + ' inside ' + pattern.key);
342+
}
343+
//replace value with expandedLink on the pattern
344+
dataObjAsString = dataObjAsString.replace(linkMatches[i], expandedLink);
345+
}
346+
//write back to data on the pattern
347+
pattern.jsonFileData = JSON.parse(dataObjAsString);
348+
}
349+
}
350+
320351
return {
321352
find_pattern_partials: function(pattern){
322353
return findPartials(pattern);
@@ -356,6 +387,9 @@
356387
},
357388
is_object_empty: function(obj){
358389
return isObjectEmpty(obj);
390+
},
391+
parse_data_links: function(patternlab){
392+
parseDataLinks(patternlab);
359393
}
360394
};
361395

builder/pattern_exporter.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* patternlab-node - v0.16.0 - 2015
2+
* patternlab-node - v1.0.0 - 2015
33
*
44
* Brian Muenzenmeyer, and the web community.
55
* Licensed under the MIT license.

builder/patternlab.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* patternlab-node - v0.16.0 - 2015
2+
* patternlab-node - v1.0.0 - 2015
33
*
44
* Brian Muenzenmeyer, and the web community.
55
* Licensed under the MIT license.
@@ -91,6 +91,10 @@ var patternlab_engine = function () {
9191
pattern_assembler.process_pattern_iterative(file.substring(2), patternlab);
9292
});
9393

94+
//now that all the main patterns are known, look for any links that might be within data and expand them
95+
//we need to do this before expanding patterns & partials into extendedTemplates, otherwise we could lose the data -> partial reference
96+
pattern_assembler.parse_data_links(patternlab);
97+
9498
//diveSync again to recursively include partials, filling out the
9599
//extendedTemplate property of the patternlab.patterns elements
96100
diveSync(patterns_dir, {
@@ -124,6 +128,7 @@ var patternlab_engine = function () {
124128
var allData = JSON.parse(JSON.stringify(patternlab.data));
125129
allData = pattern_assembler.merge_data(allData, pattern.jsonFileData);
126130

131+
//render the extendedTemplate with all data
127132
pattern.patternPartial = pattern_assembler.renderPattern(pattern.extendedTemplate, allData);
128133

129134
//add footer info before writing

builder/patternlab_grunt.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* patternlab-node - v0.16.0 - 2015
2+
* patternlab-node - v1.0.0 - 2015
33
*
44
* Brian Muenzenmeyer, and the web community.
55
* Licensed under the MIT license.

builder/patternlab_gulp.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* patternlab-node - v0.16.0 - 2015
2+
* patternlab-node - v1.0.0 - 2015
33
*
44
* Brian Muenzenmeyer, and the web community.
55
* Licensed under the MIT license.

builder/pseudopattern_hunter.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* patternlab-node - v0.16.0 - 2015
2+
* patternlab-node - v1.0.0 - 2015
33
*
44
* Brian Muenzenmeyer, and the web community.
55
* Licensed under the MIT license.

builder/style_modifier_hunter.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* patternlab-node - v0.16.0 - 2015
2+
* patternlab-node - v1.0.0 - 2015
33
*
44
* Brian Muenzenmeyer, and the web community.
55
* Licensed under the MIT license.

package.gulp.json

+1-1
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": "0.16.0",
4+
"version": "1.0.0",
55
"devDependencies": {
66
"browser-sync": "^2.10.0",
77
"del": "^2.0.2",

package.json

+1-1
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": "0.16.0",
4+
"version": "1.0.0",
55
"devDependencies": {
66
"bs-html-injector": "^3.0.0",
77
"diveSync": "^0.3.0",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<a href="{{url}}">Cool Dude</a>

test/files/_patterns/00-test/nav.json

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"brad" : {
3+
"url" : "link.twitter-brad"
4+
},
5+
"dave" : {
6+
"url" : "link.twitter-dave"
7+
},
8+
"brian" : {
9+
"url" : "link.twitter-brian"
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{{# brad }}
2+
{{> test-link }}
3+
{{/ brad }}
4+
{{# dave }}
5+
{{> test-link }}
6+
{{/ dave }}
7+
{{# brian }}
8+
{{> test-link }}
9+
{{/ brian }}

test/pattern_assembler_tests.js

+66
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,7 @@
514514
var pattern_assembler = new pa();
515515
var patterns_dir = './test/files/_patterns';
516516
var patternlab = {};
517+
//THIS IS BAD.
517518
patternlab.config = fs.readJSONSync('./config.json');
518519
patternlab.config.patterns = {source: patterns_dir};
519520
patternlab.data = fs.readJSONSync('./source/_data/data.json');
@@ -596,6 +597,71 @@
596597
//assert
597598
test.equals(pattern.patternState, "");
598599
test.done();
600+
},
601+
'parseDataLinks - replaces found link.* data for their expanded links' : function(test){
602+
//arrange
603+
var diveSync = require('diveSync');
604+
var fs = require('fs-extra');
605+
var pa = require('../builder/pattern_assembler');
606+
var pattern_assembler = new pa();
607+
var patterns_dir = './test/files/_patterns/';
608+
var patternlab = {};
609+
//THIS IS BAD
610+
patternlab.config = fs.readJSONSync('./config.json');
611+
patternlab.config.patterns = {source: patterns_dir};
612+
patternlab.data = fs.readJSONSync('./source/_data/data.json');
613+
patternlab.listitems = fs.readJSONSync('./source/_data/listitems.json');
614+
patternlab.header = fs.readFileSync('./source/_patternlab-files/pattern-header-footer/header.html', 'utf8');
615+
patternlab.footer = fs.readFileSync('./source/_patternlab-files/pattern-header-footer/footer.html', 'utf8');
616+
patternlab.patterns = [];
617+
patternlab.data.link = {};
618+
patternlab.partials = {};
619+
620+
diveSync(patterns_dir,
621+
{
622+
filter: function(path, dir){
623+
if(dir){
624+
var remainingPath = path.replace(patterns_dir, '');
625+
var isValidPath = remainingPath.indexOf('/_') === -1;
626+
return isValidPath;
627+
}
628+
return true;
629+
}
630+
},
631+
function(err, file){
632+
//log any errors
633+
if(err){
634+
console.log(err);
635+
return;
636+
}
637+
pattern_assembler.process_pattern_iterative(file.substring(2), patternlab);
638+
}
639+
);
640+
641+
//for the sake of the test, also imagining I have the following pages...
642+
patternlab.data.link['twitter-brad'] = 'https://twitter.com/brad_frost';
643+
patternlab.data.link['twitter-dave'] = 'https://twitter.com/dmolsen';
644+
patternlab.data.link['twitter-brian'] = 'https://twitter.com/bmuenzenmeyer';
645+
646+
var pattern;
647+
for(var i = 0; i < patternlab.patterns.length; i++){
648+
if(patternlab.patterns[i].key === 'test-nav'){
649+
pattern = patternlab.patterns[i];
650+
}
651+
}
652+
//assert before
653+
test.equals(pattern.jsonFileData.brad.url, "link.twitter-brad");
654+
test.equals(pattern.jsonFileData.dave.url, "link.twitter-dave");
655+
test.equals(pattern.jsonFileData.brian.url, "link.twitter-brian");
656+
657+
//act
658+
pattern_assembler.parse_data_links(patternlab);
659+
660+
//assert after
661+
test.equals(pattern.jsonFileData.brad.url, "https://twitter.com/brad_frost");
662+
test.equals(pattern.jsonFileData.dave.url, "https://twitter.com/dmolsen");
663+
test.equals(pattern.jsonFileData.brian.url, "https://twitter.com/bmuenzenmeyer");
664+
test.done();
599665
}
600666
};
601667
}());

0 commit comments

Comments
 (0)