Skip to content

Commit ea69bdc

Browse files
committed
fix(list_item_hunter): Add async support and fix tests
1 parent e0b9143 commit ea69bdc

File tree

4 files changed

+184
-206
lines changed

4 files changed

+184
-206
lines changed

core/lib/decompose.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@ module.exports = function (pattern, patternlab, ignoreLineage) {
7575
pattern.extendedTemplate = pattern.template;
7676

7777
//find how many partials there may be for the given pattern
78-
var foundPatternPartials = pattern.findPartials();
78+
const foundPatternPartials = pattern.findPartials();
7979

8080
//find any listItem blocks that within the pattern, even if there are no partials
81-
list_item_hunter.process_list_item_partials(pattern, patternlab);
81+
const listItemPromise = list_item_hunter.process_list_item_partials(pattern, patternlab);
8282

8383
// expand any partials present in this pattern; that is, drill down into
8484
// the template and replace their calls in this template with rendered
@@ -112,7 +112,7 @@ module.exports = function (pattern, patternlab, ignoreLineage) {
112112
addPattern(pattern, patternlab);
113113
});
114114

115-
return Promise.all([expandPartialPromise, lineagePromise, addPromise])
115+
return Promise.all([listItemPromise, expandPartialPromise, lineagePromise, addPromise])
116116
.catch((reason) => {
117117
logger.error(reason);
118118
});

core/lib/list_item_hunter.js

Lines changed: 97 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const list_item_hunter = function () {
1010
const logger = require('./log');
1111
const parseLink = require('./parseLink');
1212
const getPartial = require('./get');
13+
const render = require('./render');
1314

1415
const style_modifier_hunter = new smh();
1516
const items = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen', 'twenty'];
@@ -19,123 +20,141 @@ const list_item_hunter = function () {
1920
const matches = pattern.findListItems();
2021

2122
if (matches !== null) {
22-
matches.forEach(function (liMatch) {
2323

24-
logger.debug(`found listItem of size ${liMatch} inside ${pattern.patternPartial}`);
24+
return matches.reduce((previousMatchPromise, liMatch) => {
2525

26-
//find the boundaries of the block
27-
const loopNumberString = liMatch.split('.')[1].split('}')[0].trim();
28-
const end = liMatch.replace('#', '/');
29-
const patternBlock = pattern.template.substring(pattern.template.indexOf(liMatch) + liMatch.length, pattern.template.indexOf(end)).trim();
26+
return previousMatchPromise.then(() => {
27+
logger.debug(`found listItem of size ${liMatch} inside ${pattern.patternPartial}`);
3028

31-
//build arrays that repeat the block, however large we need to
32-
const repeatedBlockTemplate = [];
29+
//find the boundaries of the block
30+
const loopNumberString = liMatch.split('.')[1].split('}')[0].trim();
31+
const end = liMatch.replace('#', '/');
32+
const patternBlock = pattern.template.substring(pattern.template.indexOf(liMatch) + liMatch.length, pattern.template.indexOf(end)).trim();
3333

34-
//let repeatedBlockHtml = ''; //todo
35-
for (let i = 0; i < items.indexOf(loopNumberString); i++) {
34+
//build arrays that repeat the block, however large we need to
35+
const repeatedBlockTemplate = [];
3636

37-
logger.debug(`list item(s) in pattern ${pattern.patternPartial}, adding ${patternBlock} to repeatedBlockTemplate`);
38-
repeatedBlockTemplate.push(patternBlock);
39-
}
37+
//what we will eventually replace our template's listitems block with
38+
let repeatedBlockHtml = '';
4039

41-
//check for a local listitems.json file
42-
let listData;
43-
try {
44-
listData = jsonCopy(patternlab.listitems, 'config.paths.source.data listitems');
45-
} catch (err) {
46-
logger.warning(`There was an error parsing JSON for ${pattern.relPath}`);
47-
logger.warning(err);
48-
}
40+
for (let i = 0; i < items.indexOf(loopNumberString); i++) {
4941

50-
listData = _.merge(listData, pattern.listitems);
51-
listData = parseLink(patternlab, listData, 'listitems.json + any pattern listitems.json');
52-
53-
//iterate over each copied block, rendering its contents along with pattenlab.listitems[i]
54-
for (let i = 0; i < repeatedBlockTemplate.length; i++) {
55-
56-
let thisBlockTemplate = repeatedBlockTemplate[i];
57-
58-
//let thisBlockHTML = ""; //todo
42+
logger.debug(`list item(s) in pattern ${pattern.patternPartial}, adding ${patternBlock} to repeatedBlockTemplate`);
43+
repeatedBlockTemplate.push(patternBlock);
44+
}
5945

60-
//combine listItem data with pattern data with global data
61-
const itemData = listData['' + items.indexOf(loopNumberString)]; //this is a property like "2"
62-
let globalData;
63-
let localData;
46+
//check for a local listitems.json file
47+
let listData;
6448
try {
65-
globalData = jsonCopy(patternlab.data, 'config.paths.source.data global data');
66-
localData = jsonCopy(pattern.jsonFileData, `${pattern.patternPartial} data`);
49+
listData = jsonCopy(patternlab.listitems, 'config.paths.source.data listitems');
6750
} catch (err) {
6851
logger.warning(`There was an error parsing JSON for ${pattern.relPath}`);
6952
logger.warning(err);
7053
}
7154

72-
let allData = _.merge(globalData, localData);
73-
allData = _.merge(allData, itemData !== undefined ? itemData[i] : {}); //itemData could be undefined if the listblock contains no partial, just markup
74-
allData.link = extend({}, patternlab.data.link);
55+
listData = _.merge(listData, pattern.listitems);
56+
listData = parseLink(patternlab, listData, 'listitems.json + any pattern listitems.json');
7557

76-
//check for partials within the repeated block
77-
const foundPartials = Pattern.createEmpty({'template': thisBlockTemplate}).findPartials();
58+
//iterate over each copied block, rendering its contents
59+
const allBlocks = repeatedBlockTemplate.reduce((previousPromise, currentBlockTemplate, index) => {
7860

79-
if (foundPartials && foundPartials.length > 0) {
61+
let thisBlockTemplate = currentBlockTemplate;
8062

81-
for (let j = 0; j < foundPartials.length; j++) {
63+
return previousPromise.then(() => {
8264

83-
//get the partial
84-
const partialName = foundPartials[j].match(/([\w\-\.\/~]+)/g)[0];
85-
const partialPattern = getPartial(partialName, patternlab);
86-
87-
//create a copy of the partial so as to not pollute it after the get_pattern_by_key call.
88-
let cleanPartialPattern;
65+
//combine listItem data with pattern data with global data
66+
const itemData = listData['' + items.indexOf(loopNumberString)]; //this is a property like "2"
67+
let globalData;
68+
let localData;
8969
try {
90-
cleanPartialPattern = JSON.parse(JSON.stringify(partialPattern));
91-
cleanPartialPattern = jsonCopy(partialPattern, `partial pattern ${partialName}`);
70+
globalData = jsonCopy(patternlab.data, 'config.paths.source.data global data');
71+
localData = jsonCopy(pattern.jsonFileData, `${pattern.patternPartial} data`);
9272
} catch (err) {
9373
logger.warning(`There was an error parsing JSON for ${pattern.relPath}`);
9474
logger.warning(err);
9575
}
9676

97-
//if we retrieved a pattern we should make sure that its extendedTemplate is reset. looks to fix #356
98-
cleanPartialPattern.extendedTemplate = cleanPartialPattern.template;
77+
let allData = _.merge(globalData, localData);
78+
allData = _.merge(allData, itemData !== undefined ? itemData[index] : {}); //itemData could be undefined if the listblock contains no partial, just markup
79+
allData.link = extend({}, patternlab.data.link);
9980

100-
//if partial has style modifier data, replace the styleModifier value
101-
if (foundPartials[j].indexOf(':') > -1) {
102-
style_modifier_hunter.consume_style_modifier(cleanPartialPattern, foundPartials[j], patternlab);
103-
}
81+
//check for partials within the repeated block
82+
const foundPartials = Pattern.createEmpty({'template': thisBlockTemplate}).findPartials();
10483

105-
//replace its reference within the block with the extended template
106-
thisBlockTemplate = thisBlockTemplate.replace(foundPartials[j], cleanPartialPattern.extendedTemplate);
107-
}
84+
let renderPromise = undefined;
10885

109-
//render with data
110-
//TODO
111-
//thisBlockHTML = pattern_assembler.renderPattern(thisBlockTemplate, allData, patternlab.partials);
86+
if (foundPartials && foundPartials.length > 0) {
11287

113-
} else {
88+
for (let j = 0; j < foundPartials.length; j++) {
11489

115-
//just render with mergedData
116-
//TODO
117-
//thisBlockHTML = pattern_assembler.renderPattern(thisBlockTemplate, allData, patternlab.partials);
118-
}
90+
//get the partial
91+
const partialName = foundPartials[j].match(/([\w\-\.\/~]+)/g)[0];
92+
const partialPattern = getPartial(partialName, patternlab);
93+
94+
//create a copy of the partial so as to not pollute it after the get_pattern_by_key call.
95+
let cleanPartialPattern;
96+
try {
97+
cleanPartialPattern = JSON.parse(JSON.stringify(partialPattern));
98+
cleanPartialPattern = jsonCopy(partialPattern, `partial pattern ${partialName}`);
99+
} catch (err) {
100+
logger.warning(`There was an error parsing JSON for ${pattern.relPath}`);
101+
logger.warning(err);
102+
}
103+
104+
//if we retrieved a pattern we should make sure that its extendedTemplate is reset. looks to fix #356
105+
cleanPartialPattern.extendedTemplate = cleanPartialPattern.template;
106+
107+
//if partial has style modifier data, replace the styleModifier value
108+
if (foundPartials[j].indexOf(':') > -1) {
109+
style_modifier_hunter.consume_style_modifier(cleanPartialPattern, foundPartials[j], patternlab);
110+
}
111+
112+
//replace its reference within the block with the extended template
113+
thisBlockTemplate = thisBlockTemplate.replace(foundPartials[j], cleanPartialPattern.extendedTemplate);
114+
}
115+
116+
//render with data
117+
renderPromise = render(Pattern.createEmpty({'template': thisBlockTemplate}), allData, patternlab.partials);
118+
} else {
119+
//just render with mergedData
120+
renderPromise = render(Pattern.createEmpty({'template': thisBlockTemplate}), allData, patternlab.partials);
121+
}
122+
123+
return renderPromise.then((thisBlockHTML) => {
124+
125+
//add the rendered HTML to our string
126+
repeatedBlockHtml = repeatedBlockHtml + thisBlockHTML;
127+
}).catch((reason) => {
128+
logger.error(reason);
129+
});
130+
}).catch((reason) => {
131+
logger.error(reason);
132+
});
133+
}, Promise.resolve());
119134

120-
//add the rendered HTML to our string
121-
//repeatedBlockHtml = repeatedBlockHtml + thisBlockHTML; //todo
122-
}
135+
return allBlocks.then(() => {
123136

124-
//replace the block with our generated HTML
125-
//const repeatingBlock = pattern.extendedTemplate.substring(pattern.extendedTemplate.indexOf(liMatch), pattern.extendedTemplate.indexOf(end) + end.length);
137+
//replace the block with our generated HTML
138+
const repeatingBlock = pattern.extendedTemplate.substring(pattern.extendedTemplate.indexOf(liMatch), pattern.extendedTemplate.indexOf(end) + end.length);
139+
pattern.extendedTemplate = pattern.extendedTemplate.replace(repeatingBlock, repeatedBlockHtml);
126140

127-
//pattern.extendedTemplate = pattern.extendedTemplate.replace(repeatingBlock, repeatedBlockHtml); //todo
141+
//update the extendedTemplate in the partials object in case this pattern is consumed later
142+
patternlab.partials[pattern.patternPartial] = pattern.extendedTemplate;
143+
}).catch((reason) => {
144+
logger.error(reason);
145+
});
146+
});
128147

129-
//update the extendedTemplate in the partials object in case this pattern is consumed later
130-
patternlab.partials[pattern.patternPartial] = pattern.extendedTemplate;
148+
}, Promise.resolve());
131149

132-
});
150+
} else {
151+
return Promise.resolve();
133152
}
134153
}
135154

136155
return {
137156
process_list_item_partials: function (pattern, patternlab) {
138-
processListItemPartials(pattern, patternlab);
157+
return processListItemPartials(pattern, patternlab);
139158
}
140159
};
141160
};

core/lib/pattern_assembler.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"use strict";
22

33
const path = require('path');
4-
const _ = require('lodash');
54

65
const Pattern = require('./object_factory').Pattern;
76
const mp = require('./markdown_parser');

0 commit comments

Comments
 (0)