Skip to content

Commit 92ff85d

Browse files
eragon512sapegin
authored andcommitted
Fix: Allow section content to be function (styleguidist#1368)
1 parent 0ef7ff3 commit 92ff85d

File tree

5 files changed

+70
-21
lines changed

5 files changed

+70
-21
lines changed

src/client/rsg-components/Section/SectionRenderer.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ SectionRenderer.propTypes = {
4949
name: PropTypes.string,
5050
description: PropTypes.string,
5151
slug: PropTypes.string,
52-
filepath: PropTypes.string,
5352
content: PropTypes.node,
5453
components: PropTypes.node,
5554
sections: PropTypes.node,

src/loaders/utils/__tests__/__snapshots__/getSections.spec.js.snap

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ Array [
1010
"description": undefined,
1111
"exampleMode": "collapse",
1212
"external": undefined,
13-
"filepath": "components/Button/Readme.md",
1413
"href": undefined,
1514
"name": "Readme",
1615
"sectionDepth": 0,
@@ -92,7 +91,6 @@ Array [
9291
"description": undefined,
9392
"exampleMode": "collapse",
9493
"external": undefined,
95-
"filepath": undefined,
9694
"href": undefined,
9795
"name": "Components",
9896
"sectionDepth": 0,
@@ -174,12 +172,27 @@ Array [
174172
"description": undefined,
175173
"exampleMode": "collapse",
176174
"external": undefined,
177-
"filepath": undefined,
178175
"href": undefined,
179176
"name": "Ignore",
180177
"sectionDepth": 0,
181178
"sections": Array [],
182-
"slug": "section-ignore-1",
179+
"slug": "section-ignore-2",
180+
"usageMode": "collapse",
181+
},
182+
Object {
183+
"components": Array [],
184+
"content": Object {
185+
"content": "Hello World",
186+
"type": "markdown",
187+
},
188+
"description": undefined,
189+
"exampleMode": "collapse",
190+
"external": undefined,
191+
"href": undefined,
192+
"name": "Ignore",
193+
"sectionDepth": 0,
194+
"sections": Array [],
195+
"slug": "section-ignore-3",
183196
"usageMode": "collapse",
184197
},
185198
]
@@ -260,7 +273,6 @@ Object {
260273
"description": undefined,
261274
"exampleMode": "collapse",
262275
"external": undefined,
263-
"filepath": undefined,
264276
"href": undefined,
265277
"name": "Components",
266278
"sectionDepth": 0,
@@ -279,7 +291,6 @@ Object {
279291
"description": undefined,
280292
"exampleMode": "collapse",
281293
"external": undefined,
282-
"filepath": "components/Button/Readme.md",
283294
"href": undefined,
284295
"name": "Readme",
285296
"sectionDepth": 0,
@@ -289,6 +300,25 @@ Object {
289300
}
290301
`;
291302

303+
exports[`processSection() should return an object for section with content as function 1`] = `
304+
Object {
305+
"components": Array [],
306+
"content": Object {
307+
"content": "Hello World",
308+
"type": "markdown",
309+
},
310+
"description": undefined,
311+
"exampleMode": "collapse",
312+
"external": undefined,
313+
"href": undefined,
314+
"name": "Ignore",
315+
"sectionDepth": 0,
316+
"sections": Array [],
317+
"slug": "section-ignore-1",
318+
"usageMode": "collapse",
319+
}
320+
`;
321+
292322
exports[`processSection() should return an object for section without ignored components 1`] = `
293323
Object {
294324
"components": Array [
@@ -364,7 +394,6 @@ Object {
364394
"description": undefined,
365395
"exampleMode": "collapse",
366396
"external": undefined,
367-
"filepath": undefined,
368397
"href": undefined,
369398
"name": "Ignore",
370399
"sectionDepth": 0,

src/loaders/utils/__tests__/getSections.spec.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ const sections = [
2323
components: 'components/**/*.js',
2424
ignore: '**/components/Annotation/*',
2525
},
26+
{
27+
name: 'Ignore',
28+
content: () => 'Hello World',
29+
},
2630
];
2731
const sectionsWithDepth = [
2832
{
@@ -102,6 +106,12 @@ it('processSection() should return an object for section without ignored compone
102106
expect(result).toMatchSnapshot();
103107
});
104108

109+
it('processSection() should return an object for section with content as function', () => {
110+
const result = processSection(sections[3], config);
111+
112+
expect(result).toMatchSnapshot();
113+
});
114+
105115
it('getSections() should return an array', () => {
106116
const result = getSections(sections, config);
107117

src/loaders/utils/getComponentFiles.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ module.exports = function getComponentFiles(components, rootDir, ignore) {
4747

4848
// Resolve list of components from globs
4949
const componentFiles = getFilesMatchingGlobs(componentGlobs, rootDir, ignore);
50-
50+
5151
// Get absolute component file paths with correct slash separator format
5252
const resolvedComponentFiles = componentFiles.map(file => path.resolve(file));
5353

src/loaders/utils/getSections.js

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,28 @@ const slugger = require('./slugger');
1010

1111
const examplesLoader = path.resolve(__dirname, '../examples-loader.js');
1212

13+
function processSectionContent(section, config) {
14+
const contentRelativePath = section.content;
15+
16+
if (!section.content) {
17+
return undefined;
18+
}
19+
20+
if (_.isFunction(section.content)) {
21+
return {
22+
type: 'markdown',
23+
content: section.content(),
24+
};
25+
}
26+
27+
// Try to load section content file
28+
const contentAbsolutePath = path.resolve(config.configDir, contentRelativePath);
29+
if (!fs.existsSync(contentAbsolutePath)) {
30+
throw new Error(`Styleguidist: Section content file not found: ${contentAbsolutePath}`);
31+
}
32+
return requireIt(`!!${examplesLoader}!${contentAbsolutePath}`);
33+
}
34+
1335
/**
1436
* Return object for one level of sections.
1537
*
@@ -39,17 +61,7 @@ const getSectionComponents = (section, config) => {
3961
* @returns {object}
4062
*/
4163
function processSection(section, config, parentDepth) {
42-
const contentRelativePath = section.content;
43-
44-
// Try to load section content file
45-
let content;
46-
if (contentRelativePath) {
47-
const contentAbsolutePath = path.resolve(config.configDir, contentRelativePath);
48-
if (!fs.existsSync(contentAbsolutePath)) {
49-
throw new Error(`Styleguidist: Section content file not found: ${contentAbsolutePath}`);
50-
}
51-
content = requireIt(`!!${examplesLoader}!${contentAbsolutePath}`);
52-
}
64+
const content = processSectionContent(section, config);
5365

5466
let sectionDepth;
5567

@@ -67,7 +79,6 @@ function processSection(section, config, parentDepth) {
6779
description: section.description,
6880
slug: `section-${slugger.slug(section.name)}`,
6981
sections: getSections(section.sections || [], config, sectionDepth),
70-
filepath: contentRelativePath,
7182
href: section.href,
7283
components: getSectionComponents(section, config),
7384
content,

0 commit comments

Comments
 (0)