Skip to content

Commit

Permalink
chore: handle pure tutorial steps
Browse files Browse the repository at this point in the history
  • Loading branch information
jerelmiller committed Oct 14, 2020
1 parent 9ac661c commit 82334ac
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions src/components/Tutorial.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ const Tutorial = ({ children }) => {
validateChildren(children);
});

const isSectioned = children.some((child) =>
isMdxType(child, 'TutorialSection')
);
const projectElement = isMdxType(children[0], 'Project') ? children[0] : null;

if (projectElement) {
Expand All @@ -23,13 +26,27 @@ const Tutorial = ({ children }) => {
? parseProjectStateFromConfig(projectElement)
: parseProjectStateFromChildren(children);

if (isSectioned) {
return gatherSections(children, initialProjectState);
}

const { steps } = gatherSteps(children, initialProjectState);

return steps;
};

Tutorial.propTypes = {
children: PropTypes.node,
};

const gatherSections = (children, initialProjectState) => {
const { elements } = children.reduce(
(memo, child) => {
const { elements, currentProjectState } = memo;

if (isMdxType(child, 'TutorialSection')) {
const { steps, currentProjectState: projectState } = gatherSteps(
child,
child.props.children,
currentProjectState
);

Expand All @@ -47,13 +64,9 @@ const Tutorial = ({ children }) => {
return elements;
};

Tutorial.propTypes = {
children: PropTypes.node,
};

const gatherSteps = (parentElement, initialProjectState) => {
return Children.toArray(parentElement.props.children).reduce(
({ steps, currentProjectState }, stepElement, idx, children) => {
const gatherSteps = (children, initialProjectState) => {
return Children.toArray(children).reduce(
({ steps, currentProjectState }, stepElement, idx) => {
const sharedProps = { stepNumber: idx + 1, totalSteps: children.length };
const codeBlock = Children.toArray(stepElement.props.children).find(
(child) => isCodeBlock(child) && !isShellCommand(child)
Expand Down

0 comments on commit 82334ac

Please sign in to comment.