|
318 | 318 | return o;
|
319 | 319 | }
|
320 | 320 |
|
| 321 | + function parseDataLinksHelper (patternlab, obj, key) { |
| 322 | + var linkRE, dataObjAsString, linkMatches, expandedLink; |
| 323 | + |
| 324 | + linkRE = /link\.[A-z0-9-_]+/g |
| 325 | + dataObjAsString = JSON.stringify(obj); |
| 326 | + linkMatches = dataObjAsString.match(linkRE) |
| 327 | + |
| 328 | + if(linkMatches) { |
| 329 | + for (var i = 0; i < linkMatches.length; i++) { |
| 330 | + expandedLink = patternlab.data.link[linkMatches[i].split('.')[1]]; |
| 331 | + if (expandedLink) { |
| 332 | + if(patternlab.config.debug){ |
| 333 | + console.log('expanded data link from ' + linkMatches[i] + ' to ' + expandedLink + ' inside ' + key); |
| 334 | + } |
| 335 | + dataObjAsString = dataObjAsString.replace(linkMatches[i], expandedLink); |
| 336 | + } |
| 337 | + } |
| 338 | + } |
| 339 | + return JSON.parse(dataObjAsString) |
| 340 | + } |
321 | 341 | //look for pattern links included in data files.
|
322 | 342 | //these will be in the form of link.* WITHOUT {{}}, which would still be there from direct pattern inclusion
|
323 |
| - function parseDataLinks(patternlab){ |
| 343 | + function parseDataLinks(patternlab) { |
| 344 | + //look for link.* such as link.pages-blog as a value |
| 345 | + |
| 346 | + patternlab.data = parseDataLinksHelper(patternlab, patternlab.data, 'data.json') |
324 | 347 |
|
325 | 348 | //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); |
| 349 | + for (var i = 0; i < patternlab.patterns.length; i++) { |
| 350 | + patternlab.patterns[i].jsonFileData = parseDataLinksHelper(patternlab, patternlab.patterns[i].jsonFileData, patternlab.patterns[i].key) |
348 | 351 | }
|
349 | 352 | }
|
350 | 353 |
|
|
0 commit comments