|
1 | 1 | /* |
2 | | - * patternlab-node - v0.16.0 - 2015 |
| 2 | + * patternlab-node - v1.0.0 - 2015 |
3 | 3 | * |
4 | 4 | * Brian Muenzenmeyer, and the web community. |
5 | 5 | * Licensed under the MIT license. |
|
54 | 54 | } |
55 | 55 |
|
56 | 56 | function addPattern(pattern, patternlab){ |
| 57 | + //add the link to the global object |
57 | 58 | patternlab.data.link[pattern.patternGroup + '-' + pattern.patternName] = '/patterns/' + pattern.patternLink; |
58 | 59 |
|
59 | 60 | //only push to array if the array doesn't contain this pattern |
|
317 | 318 | return o; |
318 | 319 | } |
319 | 320 |
|
| 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 | + |
320 | 351 | return { |
321 | 352 | find_pattern_partials: function(pattern){ |
322 | 353 | return findPartials(pattern); |
|
356 | 387 | }, |
357 | 388 | is_object_empty: function(obj){ |
358 | 389 | return isObjectEmpty(obj); |
| 390 | + }, |
| 391 | + parse_data_links: function(patternlab){ |
| 392 | + parseDataLinks(patternlab); |
359 | 393 | } |
360 | 394 | }; |
361 | 395 |
|
|
0 commit comments