From b6e58c0f8eebb0447e5330b983ec387dbf2a59c0 Mon Sep 17 00:00:00 2001 From: eug-vs 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') 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(``); + return line.match(regex) !== null; +} + const Content: React.FC = ({ 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 = + buffer = } else if (denotesDottedList(line)) { const closeIndex = rawLines.findIndex(line => !denotesDottedList(line)); const dottedListLines = rawLines.splice(0, closeIndex).slice(0, closeIndex); dottedListLines.unshift(line); - result =
    {dottedListLines.map(li =>
  • )}
; + buffer =
    {dottedListLines.map(li =>
  • )}
; + } 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 =
; } else { - result = + buffer = } return ( <> - { result } + { buffer } ); -- cgit v1.2.3