|
340 | 340 | return o;
|
341 | 341 | }
|
342 | 342 |
|
| 343 | + function parseDataLinksHelper (patternlab, obj, key) { |
| 344 | + var linkRE, dataObjAsString, linkMatches, expandedLink; |
| 345 | + |
| 346 | + linkRE = /link\.[A-z0-9-_]+/g |
| 347 | + dataObjAsString = JSON.stringify(obj); |
| 348 | + linkMatches = dataObjAsString.match(linkRE) |
| 349 | + |
| 350 | + if(linkMatches) { |
| 351 | + for (var i = 0; i < linkMatches.length; i++) { |
| 352 | + expandedLink = patternlab.data.link[linkMatches[i].split('.')[1]]; |
| 353 | + if (expandedLink) { |
| 354 | + if(patternlab.config.debug){ |
| 355 | + console.log('expanded data link from ' + linkMatches[i] + ' to ' + expandedLink + ' inside ' + key); |
| 356 | + } |
| 357 | + dataObjAsString = dataObjAsString.replace(linkMatches[i], expandedLink); |
| 358 | + } |
| 359 | + } |
| 360 | + } |
| 361 | + return JSON.parse(dataObjAsString) |
| 362 | + } |
343 | 363 | //look for pattern links included in data files.
|
344 | 364 | //these will be in the form of link.* WITHOUT {{}}, which would still be there from direct pattern inclusion
|
345 |
| - function parseDataLinks(patternlab){ |
| 365 | + function parseDataLinks(patternlab) { |
| 366 | + //look for link.* such as link.pages-blog as a value |
| 367 | + |
| 368 | + patternlab.data = parseDataLinksHelper(patternlab, patternlab.data, 'data.json') |
346 | 369 |
|
347 | 370 | //loop through all patterns
|
348 |
| - for (var i = 0; i < patternlab.patterns.length; i++){ |
349 |
| - var pattern = patternlab.patterns[i]; |
350 |
| - //look for link.* such as link.pages-blog as a value |
351 |
| - var linkRE = /link.[A-z0-9-_]+/g; |
352 |
| - //convert to string for easier searching |
353 |
| - var dataObjAsString = JSON.stringify(pattern.jsonFileData); |
354 |
| - var linkMatches = dataObjAsString.match(linkRE); |
355 |
| - |
356 |
| - //if no matches found, escape current loop iteration |
357 |
| - if(linkMatches === null) { continue; } |
358 |
| - |
359 |
| - for(var i = 0; i < linkMatches.length; i++){ |
360 |
| - //for each match, find the expanded link within the already constructed patternlab.data.link object |
361 |
| - var expandedLink = patternlab.data.link[linkMatches[i].split('.')[1]]; |
362 |
| - if(patternlab.config.debug){ |
363 |
| - console.log('expanded data link from ' + linkMatches[i] + ' to ' + expandedLink + ' inside ' + pattern.key); |
364 |
| - } |
365 |
| - //replace value with expandedLink on the pattern |
366 |
| - dataObjAsString = dataObjAsString.replace(linkMatches[i], expandedLink); |
367 |
| - } |
368 |
| - //write back to data on the pattern |
369 |
| - pattern.jsonFileData = JSON.parse(dataObjAsString); |
| 371 | + for (var i = 0; i < patternlab.patterns.length; i++) { |
| 372 | + patternlab.patterns[i].jsonFileData = parseDataLinksHelper(patternlab, patternlab.patterns[i].jsonFileData, patternlab.patterns[i].key) |
370 | 373 | }
|
371 | 374 | }
|
372 | 375 |
|
|
0 commit comments