diff options
author | eug-vs <eug-vs@keemail.me> | 2020-04-05 07:44:13 +0300 |
---|---|---|
committer | eug-vs <eug-vs@keemail.me> | 2020-04-05 07:47:58 +0300 |
commit | 0389a542776f8f7fc3bc71a54b0aca7179f6fed3 (patch) | |
tree | 2cff2b360561a36b8a9211cfd61126566876c0d2 /src | |
parent | 351cbf596fdda867240d76b632f9d205f343153c (diff) | |
download | react-benzin-0389a542776f8f7fc3bc71a54b0aca7179f6fed3.tar.gz |
refactor: improve Markdown/Section :zap:
Diffstat (limited to 'src')
-rw-r--r-- | src/lib/Markdown/Section.tsx | 34 |
1 files changed, 20 insertions, 14 deletions
diff --git a/src/lib/Markdown/Section.tsx b/src/lib/Markdown/Section.tsx index c902379..5ae4a10 100644 --- a/src/lib/Markdown/Section.tsx +++ b/src/lib/Markdown/Section.tsx @@ -11,27 +11,33 @@ const matchHeaderLevel = (line: string, level: number): boolean => { return line.match(`^#{${level}} .*$`) !== null; } -const Section: React.FC<PropTypes> = ({ rawLines, level = 0 }) => { - const sectionName = rawLines.splice(0, 1)[0].slice(level).trim(); - const contentSize = rawLines.findIndex(line => matchHeaderLevel(line, level + 1)); - const rawContent = rawLines.splice(0, (contentSize < 0) ? rawLines.length : contentSize); - +const ChildrenSections: React.FC<PropTypes> = ({ rawLines, level = 1 }) => { const childrenSectionLines = rawLines.reduce((sections: string[][], line: string) => { - if (matchHeaderLevel(line, level + 1)) sections.push([]); + if (matchHeaderLevel(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 + 1}/>) + const children = childrenSectionLines.map(sectionLines => <Section rawLines={sectionLines} level={level}/>); + return <> {children} </>; +} + +const Section: React.FC<PropTypes> = ({ rawLines, level = 0 }) => { + if (!level) { + const beforeMarkdown = rawLines.splice(0, rawLines.findIndex(line => line.match(/^#.+$/g))); + console.log(`This content was found in original .md file but will not be shown: ${beforeMarkdown.join()}`); + while(rawLines[0][level + 1] === '#') level++; + return <ChildrenSections rawLines={rawLines} level={level + 1}/>; + } + + const sectionName = rawLines.splice(0, 1)[0].slice(level).trim(); + const contentSize = rawLines.findIndex(line => matchHeaderLevel(line, level + 1)); + const rawContent = rawLines.splice(0, (contentSize < 0) ? rawLines.length : contentSize); - return level ? ( - <ContentSection sectionName={sectionName}> + return ( + <ContentSection sectionName={sectionName} level={level}> <Content rawLines={rawContent} /> - {children} + <ChildrenSections rawLines={rawLines} level={level + 1} /> </ContentSection> - ) : ( - <> - {children} - </> ); } |