diff options
author | eug-vs <eug-vs@keemail.me> | 2020-04-05 20:42:25 +0300 |
---|---|---|
committer | eug-vs <eug-vs@keemail.me> | 2020-04-05 20:45:55 +0300 |
commit | b6e58c0f8eebb0447e5330b983ec387dbf2a59c0 (patch) | |
tree | 9395e7ef5cd8f9763ff5b3040f8ac10c9e1b6e5e /src/lib/Markdown | |
parent | eda4a1dceeb3d8db5ce47a555f098dbaa1deb996 (diff) | |
download | react-benzin-b6e58c0f8eebb0447e5330b983ec387dbf2a59c0.tar.gz |
feat: parse html :rocket:
Diffstat (limited to 'src/lib/Markdown')
-rw-r--r-- | src/lib/Markdown/Content.tsx | 27 |
1 files changed, 22 insertions, 5 deletions
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} /> </> ); |