diff options
author | Eugene Sokolov <eug-vs@keemail.me> | 2020-04-09 16:42:12 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-09 16:42:12 +0300 |
commit | ad42e742c04c8302c2bcaf67c07ef215bba04f3b (patch) | |
tree | da29af275ec03232d81938e77c36d894c7093c2f /src/lib/Markdown/Content.tsx | |
parent | b808381b02097a25eb51b14246f7a239a785b347 (diff) | |
parent | d1bcd3348c67cfbcd83178c9710cc54de9f776e8 (diff) | |
download | react-benzin-ad42e742c04c8302c2bcaf67c07ef215bba04f3b.tar.gz |
Merge pull request #9 from eug-vs/md-improve
Markdown extensive features
Diffstat (limited to 'src/lib/Markdown/Content.tsx')
-rw-r--r-- | src/lib/Markdown/Content.tsx | 22 |
1 files changed, 16 insertions, 6 deletions
diff --git a/src/lib/Markdown/Content.tsx b/src/lib/Markdown/Content.tsx index aaea100..5816214 100644 --- a/src/lib/Markdown/Content.tsx +++ b/src/lib/Markdown/Content.tsx @@ -6,11 +6,10 @@ import { ParserPropTypes } from './types'; const denotesCodeBlock = (line: string): boolean => { - return line.match(/^```.*$/) !== null; -} + return line.match(/^\s*```.*$/) !== null; } const denotesDottedList = (line: string): boolean => { - return line.match(/^ ?- .*$/) !== null; + return line.match(/^ ?[-*] .*$/) !== null; } const denotesOpenHtml= (line: string): string => { @@ -29,6 +28,10 @@ const denotesSelfClosingHtml = (line: string): string[] | null => { return line.match(regex); } +const declaresNoLineBreak = (line: string): boolean => { + return line.match(/\\\|$/) !== null; +} + const Content: React.FC<ParserPropTypes> = ({ rawLines }) => { if (!rawLines.length) return null; @@ -46,14 +49,13 @@ const Content: React.FC<ParserPropTypes> = ({ rawLines }) => { buffer = <ul>{dottedListLines.map(li => <li><Text line={li.slice(2)} /></li>)}</ul>; } else if ((buffer = denotesOpenHtml(line))) { const tag = buffer; - const closeIndex = rawLines.findIndex(line => denotesClosingHtml(line, tag)); - const htmlLines = rawLines.splice(0, closeIndex + 1).slice(0, closeIndex); + 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); - console.log({ line, match, before, after}); buffer = ( <> <Text line={before} /> @@ -61,6 +63,14 @@ const Content: React.FC<ParserPropTypes> = ({ rawLines }) => { <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> } |