Skip to content

Commit

Permalink
Allow section content to be function
Browse files Browse the repository at this point in the history
  • Loading branch information
eragon512 committed May 18, 2019
1 parent f35b307 commit 4257858
Showing 1 changed file with 33 additions and 11 deletions.
44 changes: 33 additions & 11 deletions src/loaders/utils/getSections.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,38 @@ const slugger = require('./slugger');

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

function processSectionContent(section, config) {
const contentRelativePath = section.content;

if (!section.content) {
return {};
}

if (_.isFunction(section.content)) {
return {
content: [
{
type: 'markdown',
content: section.content(),
},
],
contentRelativePath,
};
}

// Try to load section content file
const contentAbsolutePath = path.resolve(config.configDir, contentRelativePath);
if (!fs.existsSync(contentAbsolutePath)) {
throw new Error(`Styleguidist: Section content file not found: ${contentAbsolutePath}`);
}
const content = requireIt(`!!${examplesLoader}!${contentAbsolutePath}`);

return {
content,
contentRelativePath,
};
}

/**
* Return object for one level of sections.
*
Expand Down Expand Up @@ -39,17 +71,7 @@ const getSectionComponents = (section, config) => {
* @returns {object}
*/
function processSection(section, config, parentDepth) {
const contentRelativePath = section.content;

// Try to load section content file
let content;
if (contentRelativePath) {
const contentAbsolutePath = path.resolve(config.configDir, contentRelativePath);
if (!fs.existsSync(contentAbsolutePath)) {
throw new Error(`Styleguidist: Section content file not found: ${contentAbsolutePath}`);
}
content = requireIt(`!!${examplesLoader}!${contentAbsolutePath}`);
}
const { content, contentRelativePath } = processSectionContent(section, config);

let sectionDepth;

Expand Down

0 comments on commit 4257858

Please sign in to comment.