aboutsummaryrefslogtreecommitdiff
path: root/src/lib/Markdown/Content.tsx
blob: 5816214991165e02e86fb1abf388a49ff8150071 (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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import React from 'react';

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


const denotesCodeBlock = (line: string): boolean => {
  return line.match(/^\s*```.*$/) !== null; }

const denotesDottedList = (line: string): boolean => {
  return line.match(/^ ?[-*] .*$/) !== null;
}

const denotesOpenHtml= (line: string): string => {
  const regex = /<([^/\s]*)[^<]*[^/]>/g;
  const match = regex.exec(line);
  return match ? match[1] : '';
}

const denotesClosingHtml= (line: string, tag: string): boolean => {
  const regex = new RegExp(`</${tag}[^<]*>`);
  return line.match(regex) !== null;
}

const denotesSelfClosingHtml = (line: string): string[] | null => {
  const regex = /(<[^/\s]*[^<]*\/>)/g;
  return line.match(regex);
}

const declaresNoLineBreak = (line: string): boolean => {
  return line.match(/\\\|$/) !== null;
}

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

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

  let buffer;
  if (denotesCodeBlock(line)) {
    const closeIndex = rawLines.findIndex(line => denotesCodeBlock(line));
    const codeBlockLines = rawLines.splice(0, closeIndex + 1).slice(0, closeIndex);
    buffer = <CodeBlock rawLines={codeBlockLines} />
  } else if (denotesDottedList(line)) {
    const closeIndex = rawLines.findIndex(line => !denotesDottedList(line));
    const dottedListLines = rawLines.splice(0, closeIndex).slice(0, closeIndex);
    dottedListLines.unshift(line);
    buffer = <ul>{dottedListLines.map(li => <li><Text line={li.slice(2)} /></li>)}</ul>;
  } else if ((buffer = denotesOpenHtml(line))) {
    const tag = buffer;
    const closeIndex = denotesClosingHtml(line, tag) ? -1 : rawLines.findIndex(line => denotesClosingHtml(line, tag));
    const htmlLines = rawLines.splice(0, closeIndex + 1);
    htmlLines.unshift(line);
    buffer = <div dangerouslySetInnerHTML={{ __html: htmlLines.join('\n') }}></div>;
  } else if ((buffer = denotesSelfClosingHtml(line)) !== null) {
    const match = buffer[0];
    const [before, after] = line.split(match);
    buffer = (
      <>
        <Text line={before} />
        <div dangerouslySetInnerHTML={{ __html: match }}></div>
        <Text line={after} />
      </>
    );
  } else if (declaresNoLineBreak(line)) {
    const closeIndex = rawLines.findIndex(line => !declaresNoLineBreak(line));
    const lineBreakLines = rawLines.splice(0, closeIndex).map(line => line.slice(0, -2));
    lineBreakLines.unshift(line.slice(0, -2));
    lineBreakLines.push(rawLines.splice(0, 1)[0]);
    buffer = <p>{lineBreakLines.map(lineBreakLine => <Text line={lineBreakLine} />)}</p>;
  } else if (denotesClosingHtml(line, '')) {
    buffer = null;
  } else {
    buffer = <p><Text line={line} /></p>
  }

  return (
    <>
      { buffer }
      <Content rawLines={rawLines} />
    </>
  );
}

export default Content;