Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix: #171 pattern links inside base data object #239

Merged
merged 2 commits into from
Feb 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 26 additions & 23 deletions builder/pattern_assembler.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,33 +318,36 @@
return o;
}

function parseDataLinksHelper (patternlab, obj, key) {
var linkRE, dataObjAsString, linkMatches, expandedLink;

linkRE = /link\.[A-z0-9-_]+/g
dataObjAsString = JSON.stringify(obj);
linkMatches = dataObjAsString.match(linkRE)

if(linkMatches) {
for (var i = 0; i < linkMatches.length; i++) {
expandedLink = patternlab.data.link[linkMatches[i].split('.')[1]];
if (expandedLink) {
if(patternlab.config.debug){
console.log('expanded data link from ' + linkMatches[i] + ' to ' + expandedLink + ' inside ' + key);
}
dataObjAsString = dataObjAsString.replace(linkMatches[i], expandedLink);
}
}
}
return JSON.parse(dataObjAsString)
}
//look for pattern links included in data files.
//these will be in the form of link.* WITHOUT {{}}, which would still be there from direct pattern inclusion
function parseDataLinks(patternlab){
function parseDataLinks(patternlab) {
//look for link.* such as link.pages-blog as a value

patternlab.data = parseDataLinksHelper(patternlab, patternlab.data, 'data.json')

//loop through all patterns
for (var i = 0; i < patternlab.patterns.length; i++){
var pattern = patternlab.patterns[i];
//look for link.* such as link.pages-blog as a value
var linkRE = /link.[A-z0-9-_]+/g;
//convert to string for easier searching
var dataObjAsString = JSON.stringify(pattern.jsonFileData);
var linkMatches = dataObjAsString.match(linkRE);

//if no matches found, escape current loop iteration
if(linkMatches === null) { continue; }

for(var i = 0; i < linkMatches.length; i++){
//for each match, find the expanded link within the already constructed patternlab.data.link object
var expandedLink = patternlab.data.link[linkMatches[i].split('.')[1]];
if(patternlab.config.debug){
console.log('expanded data link from ' + linkMatches[i] + ' to ' + expandedLink + ' inside ' + pattern.key);
}
//replace value with expandedLink on the pattern
dataObjAsString = dataObjAsString.replace(linkMatches[i], expandedLink);
}
//write back to data on the pattern
pattern.jsonFileData = JSON.parse(dataObjAsString);
for (var i = 0; i < patternlab.patterns.length; i++) {
patternlab.patterns[i].jsonFileData = parseDataLinksHelper(patternlab, patternlab.patterns[i].jsonFileData, patternlab.patterns[i].key)
}
}

Expand Down
8 changes: 4 additions & 4 deletions builder/patternlab.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,6 @@ var patternlab_engine = function () {
pattern_assembler.process_pattern_iterative(file.substring(2), patternlab);
});

//now that all the main patterns are known, look for any links that might be within data and expand them
//we need to do this before expanding patterns & partials into extendedTemplates, otherwise we could lose the data -> partial reference
pattern_assembler.parse_data_links(patternlab);

//diveSync again to recursively include partials, filling out the
//extendedTemplate property of the patternlab.patterns elements
diveSync(patterns_dir, {
Expand All @@ -117,6 +113,10 @@ var patternlab_engine = function () {
pattern_assembler.process_pattern_recursive(file.substring(2), patternlab);
});

//now that all the main patterns are known, look for any links that might be within data and expand them
//we need to do this before expanding patterns & partials into extendedTemplates, otherwise we could lose the data -> partial reference
pattern_assembler.parse_data_links(patternlab);

//delete the contents of config.patterns.public before writing
if(deletePatternDir){
fs.emptyDirSync(patternlab.config.patterns.public);
Expand Down
9 changes: 9 additions & 0 deletions test/pattern_assembler_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,11 @@
patternlab.data.link['twitter-dave'] = 'https://twitter.com/dmolsen';
patternlab.data.link['twitter-brian'] = 'https://twitter.com/bmuenzenmeyer';

patternlab.data.brad = { url: "link.twitter-brad" }
patternlab.data.dave = { url: "link.twitter-dave" }
patternlab.data.brian = { url: "link.twitter-brian" }


var pattern;
for(var i = 0; i < patternlab.patterns.length; i++){
if(patternlab.patterns[i].key === 'test-nav'){
Expand All @@ -661,6 +666,10 @@
test.equals(pattern.jsonFileData.brad.url, "https://twitter.com/brad_frost");
test.equals(pattern.jsonFileData.dave.url, "https://twitter.com/dmolsen");
test.equals(pattern.jsonFileData.brian.url, "https://twitter.com/bmuenzenmeyer");

test.equals(patternlab.data.brad.url, "https://twitter.com/brad_frost");
test.equals(patternlab.data.dave.url, "https://twitter.com/dmolsen");
test.equals(patternlab.data.brian.url, "https://twitter.com/bmuenzenmeyer");
test.done();
}
};
Expand Down