Skip to content

Commit 7763bed

Browse files
fix default behavior of nested patterns (#1232)
Co-authored-by: Salem Ghoweri <me@salemghoweri.com>
1 parent 5d09bb0 commit 7763bed

File tree

4 files changed

+22
-21
lines changed

4 files changed

+22
-21
lines changed

packages/core/src/lib/object_factory.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ const Pattern = function(
4141
const info = this.getPatternInfo(
4242
pathObj,
4343
patternlab,
44-
isPromoteToFlatPatternRun
44+
isPromoteToFlatPatternRun ||
45+
(patternlab &&
46+
patternlab.config &&
47+
patternlab.config.allPatternsAreDeeplyNested)
4548
);
4649

4750
this.fileName = pathObj.name; // '00-colors'
@@ -258,7 +261,7 @@ Pattern.prototype = {
258261
*
259262
* @param {Patternlab} patternlab Current patternlab instance
260263
*/
261-
promoteFromFlatPatternToDirectory: function(patternlab) {
264+
promoteFromDirectoryToFlatPattern: function(patternlab) {
262265
const p = new Pattern(this.relPath, this.jsonFileData, patternlab, true);
263266
// Only reset the specific fields, not everything
264267
Object.assign(this, {
@@ -287,7 +290,7 @@ Pattern.prototype = {
287290
const info = {
288291
// 00-colors(.mustache) is deeply nested in 00-atoms-/00-global/00-colors
289292
patternlab: patternlab,
290-
patternHasOwnDir: !isPromoteToFlatPatternRun
293+
patternHasOwnDir: isPromoteToFlatPatternRun
291294
? path.basename(pathObj.dir).replace(prefixMatcher, '') ===
292295
pathObj.name.replace(prefixMatcher, '') ||
293296
path.basename(pathObj.dir).replace(prefixMatcher, '') ===

packages/core/src/lib/readDocumentation.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,11 @@ module.exports = function(pattern, patternlab) {
5656
}
5757

5858
if (
59-
!markdownObject.hasOwnProperty('deeplyNested') ||
60-
(markdownObject.hasOwnProperty('deeplyNested') &&
61-
!markdownObject.deeplyNested)
59+
markdownObject.hasOwnProperty('deeplyNested') &&
60+
markdownObject.deeplyNested
6261
) {
6362
// Reset to pattern without own pattern-directory
64-
pattern.promoteFromFlatPatternToDirectory(patternlab);
63+
pattern.promoteFromDirectoryToFlatPattern(patternlab);
6564
}
6665
} else {
6766
logger.warning(`error processing markdown for ${pattern.patternPartial}`);

packages/core/test/object_factory_tests.js

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ tap.test(
7979
path.sep +
8080
'colors.mustache'
8181
);
82-
test.equals(p.name, '00-atoms-00-global-00-colors');
82+
test.equals(p.name, '00-atoms-00-global-colors');
8383
test.equals(p.subdir, path.join('00-atoms', '00-global', '00-colors'));
8484
test.equals(p.fileName, 'colors');
8585
test.equals(p.fileExtension, '.mustache');
@@ -88,9 +88,9 @@ tap.test(
8888
test.equals(p.patternName, 'Colors');
8989
test.equals(
9090
p.getPatternLink(pl),
91-
'00-atoms-00-global-00-colors' +
91+
'00-atoms-00-global-colors' +
9292
path.sep +
93-
'00-atoms-00-global-00-colors.rendered.html'
93+
'00-atoms-00-global-colors.rendered.html'
9494
);
9595
test.equals(p.patternGroup, 'atoms');
9696
test.equals(p.patternSubGroup, 'global');
@@ -117,8 +117,8 @@ tap.test('test Pattern name for variants correctly initialzed', function(test) {
117117
d: 123,
118118
}
119119
);
120-
test.equals(p1.name, '00-atoms-00-global-00-colors-variant');
121-
test.equals(p2.name, '00-atoms-00-global-00-colors-variant-minus');
120+
test.equals(p1.name, '00-atoms-00-global-colors-variant');
121+
test.equals(p2.name, '00-atoms-00-global-colors-variant-minus');
122122
test.end();
123123
});
124124

@@ -153,10 +153,10 @@ tap.test('test Pattern with own-directory gets resetted as expected', function(
153153
test
154154
) {
155155
var p = new Pattern('00-atoms/00-button/button.mustache', { d: 123 }, pl);
156-
p.promoteFromFlatPatternToDirectory(pl);
156+
p.promoteFromDirectoryToFlatPattern(pl);
157157

158158
test.equals(p.relPath, path.join('00-atoms', '00-button', 'button.mustache'));
159-
test.equals(p.name, '00-atoms-00-button-button');
159+
test.equals(p.name, '00-atoms-00-button');
160160
test.equals(p.subdir, path.join('00-atoms', '00-button'));
161161
test.equals(p.fileName, 'button');
162162
test.equals(p.fileExtension, '.mustache');
@@ -165,13 +165,10 @@ tap.test('test Pattern with own-directory gets resetted as expected', function(
165165
test.equals(p.patternName, 'Button');
166166
test.equals(
167167
p.getPatternLink(pl),
168-
path.join(
169-
'00-atoms-00-button-button',
170-
'00-atoms-00-button-button.rendered.html'
171-
)
168+
path.join('00-atoms-00-button', '00-atoms-00-button.rendered.html')
172169
);
173170
test.equals(p.patternGroup, 'atoms');
174-
test.equals(p.flatPatternPath, '00-atoms-00-button');
171+
test.equals(p.flatPatternPath, '00-atoms');
175172
test.equals(p.patternPartial, 'atoms-button');
176173
test.equals(p.template, '');
177174
test.equals(p.lineage.length, 0);
@@ -255,7 +252,7 @@ tap.test(
255252
'00-atoms/00-global/00-colors-alt/colors-alt~variant.mustache',
256253
{ d: 123 }
257254
);
258-
test.equals(p.name, '00-atoms-00-global-00-colors-alt-variant');
255+
test.equals(p.name, '00-atoms-00-global-colors-alt-variant');
259256
test.equals(p.flatPatternPath, '00-atoms-00-global');
260257
test.equals(p.patternBaseName, 'colors-alt-variant');
261258

@@ -295,7 +292,7 @@ tap.test('test Patterns that are nested deeper without own directory', function(
295292
'00-atoms/00-global/00-random-folder/00-another-folder/00-colors-alt/colors-alt.mustache',
296293
{ d: 123 }
297294
);
298-
test.equals(p.name, '00-atoms-00-global-00-colors-alt');
295+
test.equals(p.name, '00-atoms-00-global-colors-alt');
299296
test.equals(p.flatPatternPath, '00-atoms-00-global');
300297

301298
var p = new Pattern(

packages/docs/src/docs/pattern-organization.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,5 @@ The `deeplyNested` attribute is used to toggle the pattern building behavior and
5959

6060
- **deeplyNested not set or false** - Pattern won't be handled as a deeply nested pattern
6161
- **deeplyNested: true** - Pattern will be handled like mentioned under [Deeper Nesting](#heading-deeper-nesting)
62+
63+
To turn on this behavior globally, just add `"allPatternsAreDeeplyNested": true` to your `patternlab-config.json`.

0 commit comments

Comments
 (0)