Skip to content

Commit 8f9d004

Browse files
committed
Fix: Isolated mode should work for nested sections
1 parent eb86d7b commit 8f9d004

File tree

3 files changed

+36
-29
lines changed

3 files changed

+36
-29
lines changed

src/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
getInfoFromHash,
88
filterComponentExamples,
99
filterComponentsInSectionsByExactName,
10-
filterSections,
10+
findSection,
1111
processSections,
1212
setSlugs,
1313
slugger,
@@ -41,7 +41,8 @@ function renderStyleguide() {
4141
sections = [{ components: filteredComponents }];
4242
isolatedComponent = true;
4343
} else {
44-
sections = [filterSections(sections, targetName)];
44+
const section = findSection(sections, targetName);
45+
sections = section ? [section] : [];
4546
isolatedSection = true;
4647
}
4748

src/utils/__tests__/utils.spec.js

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -255,30 +255,20 @@ describe('getInfoFromHash', () => {
255255
});
256256
});
257257

258-
describe('filterSections', () => {
259-
const sections = [
260-
{
261-
name: 'General',
262-
sections: [],
263-
},
264-
{
265-
name: 'Forms',
266-
sections: [],
267-
},
268-
{
269-
name: 'Lists',
270-
sections: [],
271-
},
272-
];
258+
describe('findSection', () => {
259+
it('should return top level section', () => {
260+
const result = utils.findSection(sections, 'General');
261+
expect(result).toEqual(sections[0]);
262+
});
273263

274-
it('should return the Forms section', () => {
275-
const result = utils.filterSections(sections, 'Forms');
276-
expect(result).toEqual(sections[1]);
264+
it('should return nested sections', () => {
265+
const result = utils.findSection(sections, 'Particles');
266+
expect(result).toEqual(sections[0].sections[0]);
277267
});
278268

279-
it('should return the Lists section', () => {
280-
const result = utils.filterSections(sections, 'Lists');
281-
expect(result).toEqual(sections[2]);
269+
it('should return undefined when no sections found', () => {
270+
const result = utils.findSection(sections, 'Pizza');
271+
expect(result).toBeFalsy();
282272
});
283273
});
284274

src/utils/utils.js

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,13 +150,29 @@ export function filterComponentsInSectionsByExactName(sections, name) {
150150
}
151151

152152
/**
153-
* Filters the sections to find the one with the matching name
154-
* @param {Array} sections The styleguide sections
155-
* @param {string} name The name to match
156-
* @return {object} The section found
153+
* Recursively finds a section with a given name (exact match)
154+
*
155+
* @param {Array} sections
156+
* @param {string} name
157+
* @return {object}
157158
*/
158-
export function filterSections(sections, name) {
159-
return sections.find(section => section.name === name);
159+
export function findSection(sections, name) {
160+
const found = sections.find(section => section.name === name);
161+
if (found) {
162+
return found;
163+
}
164+
165+
for (const section of sections) {
166+
if (!section.sections || section.sections.length === 0) {
167+
continue;
168+
}
169+
const found = findSection(section.sections, name);
170+
if (found) {
171+
return found;
172+
}
173+
}
174+
175+
return undefined;
160176
}
161177

162178
/**

0 commit comments

Comments
 (0)