diff options
Diffstat (limited to 'src')
-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} /> </> ); |