aboutsummaryrefslogtreecommitdiff
path: root/src/lib/Markdown/Content.tsx
blob: d0a2193a87bc74157192b575db073d9896bfc483 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import React from 'react';

import CodeBlock from './CodeBlock';
import Paragraph from './Paragraph';
import { ParserPropTypes } from './types';


const denotesCodeBlock = (line: string): boolean => {
  return line.slice(0, 3) === '```';
}

const Content: React.FC<ParserPropTypes> = ({ rawLines }) => {
  if (!rawLines.length) return null;

  const line = rawLines.splice(0, 1)[0];

  let result;
  if (denotesCodeBlock(line)) {
    const closeIndex = rawLines.findIndex(line => denotesCodeBlock(line));
    const codeBlockLines = rawLines.splice(0, closeIndex + 1).slice(0, closeIndex);
    result = <CodeBlock rawLines={codeBlockLines} />
  } else {
    result = <Paragraph line={line} />
  }

  return (
    <>
      { result }
      <Content rawLines={rawLines} />
    </>
  )
}

export default Content;