import * as React from 'react'; import { Section, SectionType, ImportExampleSection, } from '../../typings/story-section'; import Markdown from '../../Markdown'; import styles from './styles.scss'; import { AnchoredTitle } from '../../AnchoredTitle/AnchoredTitle'; export const SIBLINGS = ['pretitle', 'title', 'subtitle', 'description']; const SECTIONS_WITHOUT_SIBLINGS = [ SectionType.Title, SectionType.Header, SectionType.Example, ]; const sectionPrepares = { [SectionType.ImportExample]: (section: ImportExampleSection) => ({ ...section, title: section.title || 'Import', }), }; const prepareSection = (section: Section) => { const preparedSection = ( sectionPrepares[section.type] || ((i: unknown) => i) )(section); const siblingsWithDiv = SIBLINGS.filter( sibling => preparedSection[sibling], ).reduce( (sections, key) => ({ ...sections, [key]: ( ), }), {}, ); return { ...preparedSection, ...siblingsWithDiv }; }; export const sectionWithSiblings = ( section: Section, children: React.ReactNode, isAnchored?: boolean, ) => { const preparedSection = prepareSection(section); const siblings = SIBLINGS.filter(row => preparedSection[row]); const shouldShowSiblings = siblings.length > 0 && !SECTIONS_WITHOUT_SIBLINGS.includes(section.type); return (
{shouldShowSiblings ? (
{isAnchored ? ( ) : ( siblings.map(row => preparedSection[row]) )}
) : null} {children}
); };