From b6e58c0f8eebb0447e5330b983ec387dbf2a59c0 Mon Sep 17 00:00:00 2001
From: eug-vs <eug-vs@keemail.me>
Date: Sun, 5 Apr 2020 20:42:25 +0300
Subject: feat: parse html :rocket:

---
 src/lib/Markdown/Content.tsx | 27 ++++++++++++++++++++++-----
 1 file changed, 22 insertions(+), 5 deletions(-)

(limited to 'src/lib/Markdown/Content.tsx')

diff --git a/src/lib/Markdown/Content.tsx b/src/lib/Markdown/Content.tsx
index 593b9e9..1f0fcfc 100644
--- a/src/lib/Markdown/Content.tsx
+++ b/src/lib/Markdown/Content.tsx
@@ -14,28 +14,45 @@ const denotesDottedList = (line: string): boolean => {
   return line.match(/^ ?- .*$/) !== null;
 }
 
+const denotesOpenHtmlTag = (line: string): string => {
+  const regex = /<([^/\s]*)[^<]*[^/]>/g;
+  const match = regex.exec(line);
+  return match ? match[1] : '';
+}
+
+const denotesClosingHtmlTag = (line: string, tag: string): boolean => {
+  const regex = new RegExp(`</${tag}[^<]*>`);
+  return line.match(regex) !== null;
+}
+
 const Content: React.FC<ParserPropTypes> = ({ rawLines }) => {
   if (!rawLines.length) return null;
 
   const line = rawLines.splice(0, 1)[0];
 
-  let result;
+  let buffer;
   if (denotesCodeBlock(line)) {
     const closeIndex = rawLines.findIndex(line => denotesCodeBlock(line));
     const codeBlockLines = rawLines.splice(0, closeIndex + 1).slice(0, closeIndex);
-    result = <CodeBlock rawLines={codeBlockLines} />
+    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);
-    result = <ul>{dottedListLines.map(li => <li><InlineSyntax line={li} /></li>)}</ul>;
+    buffer = <ul>{dottedListLines.map(li => <li><InlineSyntax line={li} /></li>)}</ul>;
+  } else if (denotesOpenHtmlTag(line)) {
+    const tag = denotesOpenHtmlTag(line);
+    const closeIndex = rawLines.findIndex(line => denotesClosingHtmlTag(line, tag));
+    const htmlLines = rawLines.splice(0, closeIndex + 1).slice(0, closeIndex);
+    htmlLines.unshift(line);
+    buffer = <div dangerouslySetInnerHTML={{ __html: htmlLines.join('\n') }}></div>;
   } else {
-    result = <Paragraph line={line} />
+    buffer = <Paragraph line={line} />
   }
 
   return (
     <>
-      { result }
+      { buffer }
       <Content rawLines={rawLines} />
     </>
   );
-- 
cgit v1.2.3

