diff options
author | eug-vs <eug-vs@keemail.me> | 2021-01-07 13:22:21 +0300 |
---|---|---|
committer | eug-vs <eug-vs@keemail.me> | 2021-01-07 15:17:43 +0300 |
commit | f5c21698a09d9214f57ce6dab2595f3d61e1ff11 (patch) | |
tree | 900b605f147a5a024377b42be16a6f677a10a387 /src/lib/Markdown/Section.tsx | |
parent | afa7e4abe02959798665949193d832907d97153a (diff) | |
download | react-benzin-f5c21698a09d9214f57ce6dab2595f3d61e1ff11.tar.gz |
feat!: use react-markdown
Diffstat (limited to 'src/lib/Markdown/Section.tsx')
-rw-r--r-- | src/lib/Markdown/Section.tsx | 61 |
1 files changed, 0 insertions, 61 deletions
diff --git a/src/lib/Markdown/Section.tsx b/src/lib/Markdown/Section.tsx deleted file mode 100644 index fb2933d..0000000 --- a/src/lib/Markdown/Section.tsx +++ /dev/null @@ -1,61 +0,0 @@ -import React from 'react'; -import { Typography } from '@material-ui/core'; -import ContentSection from '../ContentSection/ContentSection'; -import Content from './Content'; -import { ParserPropTypes } from './types'; - -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; - while (header[level] === '#') level += 1; - return 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); - - if (!level) { - return ( - <> - <Typography><Content rawLines={rawContent} /></Typography> - <SectionMapper rawLines={rawLines} level={getHeaderLevel(rawLines[0])} SectionComponent={Section} /> - </> - ); - } - - const sectionName = rawContent.splice(0, 1)[0].slice(level).trim(); - const deeperLevel = getHeaderLevel(rawLines[0]); - return ( - <ContentSection sectionName={sectionName} level={level}> - <Content rawLines={rawContent} /> - <SectionMapper rawLines={rawLines} level={deeperLevel} SectionComponent={Section} /> - </ContentSection> - ); -}; - -export default Section; - |