diff options
author | Eugene Sokolov <eug-vs@keemail.me> | 2020-11-14 12:59:07 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-11-14 12:59:07 +0300 |
commit | 83f400f3c760b17cb71be259b3d322db38139d4a (patch) | |
tree | 9dece0e0f04c2722579cb3926b44f23120116789 /src/lib | |
parent | 0b1bbde30d4d396f8e760c59d35689353ea012a2 (diff) | |
parent | b9d7793ff4c419fd6d8fd139eb6ae33b2f6c2e43 (diff) | |
download | react-benzin-83f400f3c760b17cb71be259b3d322db38139d4a.tar.gz |
Merge pull request #17 from eug-vs/chore/upgrade-dependencies
Chore/upgrade dependencies
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/Markdown/Section.tsx | 29 |
1 files changed, 18 insertions, 11 deletions
diff --git a/src/lib/Markdown/Section.tsx b/src/lib/Markdown/Section.tsx index fc208b1..fb2933d 100644 --- a/src/lib/Markdown/Section.tsx +++ b/src/lib/Markdown/Section.tsx @@ -8,6 +8,10 @@ interface PropTypes extends ParserPropTypes { level?: number; } +interface MapperPropTypes extends PropTypes { + SectionComponent: React.FC<PropTypes>; +} + const getHeaderLevel = (header: string): number => { if (!header) return 0; let level = 0; @@ -15,18 +19,21 @@ const getHeaderLevel = (header: string): number => { return level; }; -const ChildrenSections: React.FC<PropTypes> = ({ rawLines, level = 0 }) => { - const childrenSectionLines = rawLines.reduce((sections: string[][], line: string) => { - if (line) { - if (getHeaderLevel(line) === level) sections.push([]); - if (sections.length) sections[sections.length - 1].push(line); - } - return sections; - }, []); - const children = childrenSectionLines.map(sectionLines => <Section rawLines={sectionLines} level={level} />); +const SectionMapper: React.FC<MapperPropTypes> = ({ rawLines, level = 0, SectionComponent }) => { + const children = rawLines + .reduce((sections: string[][], line: string) => { + if (line) { + if (getHeaderLevel(line) === level) sections.push([]); + if (sections.length) sections[sections.length - 1].push(line); + } + return sections; + }, []) + .map(sectionLines => <SectionComponent rawLines={sectionLines} level={level} />); + return <>{children}</>; }; + const Section: React.FC<PropTypes> = ({ rawLines, level = 0 }) => { const deeperLevelIndex = rawLines.findIndex(line => line.match(`^#{${level + 1},} .*$`)); const rawContent = rawLines.splice(0, (deeperLevelIndex < 0) ? rawLines.length : deeperLevelIndex); @@ -35,7 +42,7 @@ const Section: React.FC<PropTypes> = ({ rawLines, level = 0 }) => { return ( <> <Typography><Content rawLines={rawContent} /></Typography> - <ChildrenSections rawLines={rawLines} level={getHeaderLevel(rawLines[0])} /> + <SectionMapper rawLines={rawLines} level={getHeaderLevel(rawLines[0])} SectionComponent={Section} /> </> ); } @@ -45,7 +52,7 @@ const Section: React.FC<PropTypes> = ({ rawLines, level = 0 }) => { return ( <ContentSection sectionName={sectionName} level={level}> <Content rawLines={rawContent} /> - <ChildrenSections rawLines={rawLines} level={deeperLevel} /> + <SectionMapper rawLines={rawLines} level={deeperLevel} SectionComponent={Section} /> </ContentSection> ); }; |