From bdcc2edb38fb0e57604fa12d25b2a4b478261e18 Mon Sep 17 00:00:00 2001 From: Eug-VS Date: Sat, 4 Apr 2020 22:05:40 +0300 Subject: refactor: structurize Markdown component :recycle: --- src/lib/Markdown/Content.tsx | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 src/lib/Markdown/Content.tsx (limited to 'src/lib/Markdown/Content.tsx') diff --git a/src/lib/Markdown/Content.tsx b/src/lib/Markdown/Content.tsx new file mode 100644 index 0000000..b7829ed --- /dev/null +++ b/src/lib/Markdown/Content.tsx @@ -0,0 +1,34 @@ +import React from 'react'; + +import CodeBlock from './CodeBlock'; +import { ParserPropTypes } from './types'; + + +const denotesCodeBlock = (line: string): boolean => { + return line.slice(0, 3) === '```'; +} + +const Content: React.FC = ({ rawLines }) => { + if (!rawLines.length) return null; + + const line = rawLines.splice(0, 1)[0]; + + let result; + if (denotesCodeBlock(line)) { + const closeIndex = rawLines.findIndex(line => denotesCodeBlock(line)); + const codeBlockLines = rawLines.splice(0, closeIndex); + result = + } else { + result =

{line}

+ } + + return ( + <> + { result } + + + ) +} + +export default Content; + -- cgit v1.2.3 From a35cdfebf2d8c53c77900fee0826d4a967290e5d Mon Sep 17 00:00:00 2001 From: eug-vs Date: Sun, 5 Apr 2020 01:01:30 +0300 Subject: feat: add Paragraph component --- src/lib/Markdown/Content.tsx | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'src/lib/Markdown/Content.tsx') diff --git a/src/lib/Markdown/Content.tsx b/src/lib/Markdown/Content.tsx index b7829ed..2a9e2ee 100644 --- a/src/lib/Markdown/Content.tsx +++ b/src/lib/Markdown/Content.tsx @@ -1,6 +1,7 @@ import React from 'react'; import CodeBlock from './CodeBlock'; +import Paragraph from './Paragraph'; import { ParserPropTypes } from './types'; @@ -16,10 +17,10 @@ const Content: React.FC = ({ rawLines }) => { let result; if (denotesCodeBlock(line)) { const closeIndex = rawLines.findIndex(line => denotesCodeBlock(line)); - const codeBlockLines = rawLines.splice(0, closeIndex); + const codeBlockLines = rawLines.splice(0, closeIndex + 1).slice(0, closeIndex); result = } else { - result =

{line}

+ result = } return ( -- cgit v1.2.3 From 39e1d32c669545ccc30e0d424323c6a01317c4be Mon Sep 17 00:00:00 2001 From: eug-vs Date: Sun, 5 Apr 2020 12:56:41 +0300 Subject: refactor: move some logic to InlineSyntax.tsx --- src/lib/Markdown/Content.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/lib/Markdown/Content.tsx') diff --git a/src/lib/Markdown/Content.tsx b/src/lib/Markdown/Content.tsx index 2a9e2ee..d0a2193 100644 --- a/src/lib/Markdown/Content.tsx +++ b/src/lib/Markdown/Content.tsx @@ -20,7 +20,7 @@ const Content: React.FC = ({ rawLines }) => { const codeBlockLines = rawLines.splice(0, closeIndex + 1).slice(0, closeIndex); result = } else { - result = + result = } return ( -- cgit v1.2.3 From eda4a1dceeb3d8db5ce47a555f098dbaa1deb996 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Sun, 5 Apr 2020 20:02:05 +0300 Subject: feat: parse dotted lists --- src/lib/Markdown/Content.tsx | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'src/lib/Markdown/Content.tsx') diff --git a/src/lib/Markdown/Content.tsx b/src/lib/Markdown/Content.tsx index d0a2193..593b9e9 100644 --- a/src/lib/Markdown/Content.tsx +++ b/src/lib/Markdown/Content.tsx @@ -2,11 +2,16 @@ import React from 'react'; import CodeBlock from './CodeBlock'; import Paragraph from './Paragraph'; +import InlineSyntax from './InlineSyntax'; import { ParserPropTypes } from './types'; const denotesCodeBlock = (line: string): boolean => { - return line.slice(0, 3) === '```'; + return line.match(/^```.*$/) !== null; +} + +const denotesDottedList = (line: string): boolean => { + return line.match(/^ ?- .*$/) !== null; } const Content: React.FC = ({ rawLines }) => { @@ -19,6 +24,11 @@ const Content: React.FC = ({ rawLines }) => { const closeIndex = rawLines.findIndex(line => denotesCodeBlock(line)); const codeBlockLines = rawLines.splice(0, closeIndex + 1).slice(0, closeIndex); result = + } 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 =>
  • )}
; } else { result = } @@ -28,7 +38,7 @@ const Content: React.FC = ({ rawLines }) => { { result } - ) + ); } export default Content; -- cgit v1.2.3 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/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(``); + 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 From 09f8c6865fb9a7e239cc907e313db44cccb7b5e8 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Sun, 5 Apr 2020 21:57:13 +0300 Subject: feat: Paragraph -> Text, parse selfClosingHtml --- src/lib/Markdown/Content.tsx | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) (limited to 'src/lib/Markdown/Content.tsx') diff --git a/src/lib/Markdown/Content.tsx b/src/lib/Markdown/Content.tsx index 1f0fcfc..e27b63f 100644 --- a/src/lib/Markdown/Content.tsx +++ b/src/lib/Markdown/Content.tsx @@ -1,7 +1,7 @@ import React from 'react'; import CodeBlock from './CodeBlock'; -import Paragraph from './Paragraph'; +import Text from './Text'; import InlineSyntax from './InlineSyntax'; import { ParserPropTypes } from './types'; @@ -14,17 +14,22 @@ const denotesDottedList = (line: string): boolean => { return line.match(/^ ?- .*$/) !== null; } -const denotesOpenHtmlTag = (line: string): string => { +const denotesOpenHtml= (line: string): string => { const regex = /<([^/\s]*)[^<]*[^/]>/g; const match = regex.exec(line); return match ? match[1] : ''; } -const denotesClosingHtmlTag = (line: string, tag: string): boolean => { +const denotesClosingHtml= (line: string, tag: string): boolean => { const regex = new RegExp(``); return line.match(regex) !== null; } +const denotesSelfClosingHtml = (line: string): any => { + const regex = /(<[^/\s]*[^<]*\/>)/g; + return line.match(regex); +} + const Content: React.FC = ({ rawLines }) => { if (!rawLines.length) return null; @@ -40,14 +45,25 @@ const Content: React.FC = ({ rawLines }) => { const dottedListLines = rawLines.splice(0, closeIndex).slice(0, closeIndex); dottedListLines.unshift(line); buffer =
    {dottedListLines.map(li =>
  • )}
; - } else if (denotesOpenHtmlTag(line)) { - const tag = denotesOpenHtmlTag(line); - const closeIndex = rawLines.findIndex(line => denotesClosingHtmlTag(line, tag)); + } else if (denotesOpenHtml(line)) { + const tag = denotesOpenHtml(line); + const closeIndex = rawLines.findIndex(line => denotesClosingHtml(line, tag)); const htmlLines = rawLines.splice(0, closeIndex + 1).slice(0, closeIndex); htmlLines.unshift(line); buffer =
; + } else if (denotesSelfClosingHtml(line) !== null) { + const match = denotesSelfClosingHtml(line)[0]; + const [before, after] = line.split(match); + console.log({ line, match, before, after}); + buffer = ( + <> + +
+ + + ); } else { - buffer = + buffer =

} return ( -- cgit v1.2.3 From 828946eb02bdfaa7ec5631f1e854881f874f7b7e Mon Sep 17 00:00:00 2001 From: eug-vs Date: Sun, 5 Apr 2020 22:06:54 +0300 Subject: refactor: rename InlineSyntax -> SyntacticSpan --- src/lib/Markdown/Content.tsx | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'src/lib/Markdown/Content.tsx') diff --git a/src/lib/Markdown/Content.tsx b/src/lib/Markdown/Content.tsx index e27b63f..caac91c 100644 --- a/src/lib/Markdown/Content.tsx +++ b/src/lib/Markdown/Content.tsx @@ -2,7 +2,6 @@ import React from 'react'; import CodeBlock from './CodeBlock'; import Text from './Text'; -import InlineSyntax from './InlineSyntax'; import { ParserPropTypes } from './types'; @@ -44,7 +43,7 @@ const Content: React.FC = ({ rawLines }) => { const closeIndex = rawLines.findIndex(line => !denotesDottedList(line)); const dottedListLines = rawLines.splice(0, closeIndex).slice(0, closeIndex); dottedListLines.unshift(line); - buffer =
    {dottedListLines.map(li =>
  • )}
; + buffer =
    {dottedListLines.map(li =>
  • )}
; } else if (denotesOpenHtml(line)) { const tag = denotesOpenHtml(line); const closeIndex = rawLines.findIndex(line => denotesClosingHtml(line, tag)); -- cgit v1.2.3 From 43d65a56bbe91de967fa2ff86b25ec216bfd5ce1 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Sun, 5 Apr 2020 22:16:02 +0300 Subject: style: fix linting errors :rotating_light: --- src/lib/Markdown/Content.tsx | 10 +++++----- 1 file changed, 5 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 caac91c..aaea100 100644 --- a/src/lib/Markdown/Content.tsx +++ b/src/lib/Markdown/Content.tsx @@ -24,7 +24,7 @@ const denotesClosingHtml= (line: string, tag: string): boolean => { return line.match(regex) !== null; } -const denotesSelfClosingHtml = (line: string): any => { +const denotesSelfClosingHtml = (line: string): string[] | null => { const regex = /(<[^/\s]*[^<]*\/>)/g; return line.match(regex); } @@ -44,14 +44,14 @@ const Content: React.FC = ({ rawLines }) => { const dottedListLines = rawLines.splice(0, closeIndex).slice(0, closeIndex); dottedListLines.unshift(line); buffer =
    {dottedListLines.map(li =>
  • )}
; - } else if (denotesOpenHtml(line)) { - const tag = denotesOpenHtml(line); + } 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); htmlLines.unshift(line); buffer =
; - } else if (denotesSelfClosingHtml(line) !== null) { - const match = denotesSelfClosingHtml(line)[0]; + } else if ((buffer = denotesSelfClosingHtml(line)) !== null) { + const match = buffer[0]; const [before, after] = line.split(match); console.log({ line, match, before, after}); buffer = ( -- cgit v1.2.3