From 5c259810433be8cdbe6fcac8776cdbf4a0c244b0 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Tue, 7 Apr 2020 15:31:00 +0300 Subject: fix: account for \r line endings, remove consoles --- src/lib/Markdown/Content.tsx | 6 ++---- src/lib/Markdown/Markdown.tsx | 2 +- src/lib/Markdown/SyntacticSpan.tsx | 1 - 3 files changed, 3 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/lib/Markdown/Content.tsx b/src/lib/Markdown/Content.tsx index aaea100..30f2a8a 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 => { @@ -53,7 +52,6 @@ const Content: React.FC = ({ rawLines }) => { } else if ((buffer = denotesSelfClosingHtml(line)) !== null) { const match = buffer[0]; const [before, after] = line.split(match); - console.log({ line, match, before, after}); buffer = ( <> diff --git a/src/lib/Markdown/Markdown.tsx b/src/lib/Markdown/Markdown.tsx index 09ad54a..0a71fd3 100644 --- a/src/lib/Markdown/Markdown.tsx +++ b/src/lib/Markdown/Markdown.tsx @@ -16,7 +16,7 @@ const Markdown: React.FC = ({ data, url }) => { }, [data, url]); if (url) axios.get(url).then(response => setMarkdown(response.data)); - return
+ return
}; diff --git a/src/lib/Markdown/SyntacticSpan.tsx b/src/lib/Markdown/SyntacticSpan.tsx index 299bf87..7fd9bc5 100644 --- a/src/lib/Markdown/SyntacticSpan.tsx +++ b/src/lib/Markdown/SyntacticSpan.tsx @@ -42,7 +42,6 @@ const splitter = new RegExp(Object.values(regex).map(pair => pair.global.source) const emojiList: Emoji[] = []; Object.keys(emojiLib).forEach(name => emojiList.push({ name, char: emojiLib[name].char })); -console.log({emojiList}) const useStyles = makeStyles(theme => ({ code: { -- cgit v1.2.3 From 8b737b42fff0c0e21f9173e047cae9ed488493f4 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Tue, 7 Apr 2020 19:33:28 +0300 Subject: feat: support images nested into links --- src/lib/Markdown/SyntacticSpan.tsx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/lib/Markdown/SyntacticSpan.tsx b/src/lib/Markdown/SyntacticSpan.tsx index 7fd9bc5..f3c2125 100644 --- a/src/lib/Markdown/SyntacticSpan.tsx +++ b/src/lib/Markdown/SyntacticSpan.tsx @@ -24,8 +24,8 @@ const enclosureRegex = (e: string): RegexPair => ({ const regex: Record = { conceal: { - global: /(!?\[.+?\]\(.+?\))/g, - local: /!?\[(.+?)\]\((.+?)\)/ + global: /(!?\[.+?\]\(.+?\))(?!])/g, + local: /!?\[(.*\]?.*)\]\((.+?)\)/ }, rawLink: { global: /((?:(?:[A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=+$,\w]+@)?[A-Za-z0-9.-]+|(?:www\.|[-;:&=+$,\w]+@)[A-Za-z0-9.-]+)(?:(?:\/[+~%/.\w-_]*)?\??(?:[-+=&;%@.\w_]*)#?(?:[.!/\\\w]*))?)/, @@ -63,7 +63,7 @@ const SyntacticSpan: React.FC = ({ span }) => { const matchConceal = regex.conceal.local.exec(span); if (matchConceal) { if (span[0] === '!') return {matchConceal[1]}; - return {matchConceal[1]}; + return ; } const matchEmoji = span.match(regex.emoji.local); -- cgit v1.2.3 From c851a68e15daedb8dbe572ce5927a8e61254d4c9 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Tue, 7 Apr 2020 20:31:45 +0300 Subject: feat: support \| symbol, fix html parsing --- src/lib/Markdown/Content.tsx | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/lib/Markdown/Content.tsx b/src/lib/Markdown/Content.tsx index 30f2a8a..5895df9 100644 --- a/src/lib/Markdown/Content.tsx +++ b/src/lib/Markdown/Content.tsx @@ -28,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 = ({ rawLines }) => { if (!rawLines.length) return null; @@ -46,7 +50,7 @@ const Content: React.FC = ({ rawLines }) => { } 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 htmlLines = rawLines.splice(0, closeIndex + 1); htmlLines.unshift(line); buffer =
; } else if ((buffer = denotesSelfClosingHtml(line)) !== null) { @@ -59,6 +63,14 @@ const Content: React.FC = ({ rawLines }) => { ); + } 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 =

{lineBreakLines.map(lineBreakLine => )}

; + } else if (denotesClosingHtml(line, '')) { + buffer = null; } else { buffer =

} -- cgit v1.2.3 From 46f410699037824408daf7c035458ba7bd958d44 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Tue, 7 Apr 2020 20:32:10 +0300 Subject: feat: include content before first header --- src/lib/Markdown/Section.tsx | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/lib/Markdown/Section.tsx b/src/lib/Markdown/Section.tsx index 5ce8954..987b771 100644 --- a/src/lib/Markdown/Section.tsx +++ b/src/lib/Markdown/Section.tsx @@ -30,7 +30,12 @@ const Section: React.FC = ({ rawLines, level = 0 }) => { const deeperLevelIndex = rawLines.findIndex(line => line.match(`^#{${level + 1},} .*$`)); const rawContent = rawLines.splice(0, (deeperLevelIndex < 0) ? rawLines.length : deeperLevelIndex); - if (!level) return ; + if (!level) return ( + <> + + + + ) const sectionName = rawContent.splice(0, 1)[0].slice(level).trim(); const deeperLevel = getHeaderLevel(rawLines[0]); -- cgit v1.2.3 From ae3228b9b3e95ace25292d0231e98e6087781c0a Mon Sep 17 00:00:00 2001 From: eug-vs Date: Thu, 9 Apr 2020 11:55:46 +0300 Subject: feat: resolve local urls --- src/lib/Markdown/Markdown.tsx | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/lib/Markdown/Markdown.tsx b/src/lib/Markdown/Markdown.tsx index 0a71fd3..c929c71 100644 --- a/src/lib/Markdown/Markdown.tsx +++ b/src/lib/Markdown/Markdown.tsx @@ -8,15 +8,23 @@ interface PropTypes { url?: string; } +const resolveUrls = (line: string, baseUrl: string = ''): string => { + return line.replace(/src="(?!http)(.*)"[\s>]/, (match, url, offset, string) => `src="${baseUrl}/${url}?sanitize=true"`) + .replace(/\[(.*\]?.*)\]\((?!http)(.+?)\)/, (match, text, url, offset, string) => `[${text}](${baseUrl}/${url})`); +} + const Markdown: React.FC = ({ data, url }) => { const [markdown, setMarkdown] = useState(data || ''); + if (url) axios.get(url).then(response => setMarkdown(response.data)); + useEffect(() => { if (!url) setMarkdown(data || ''); }, [data, url]); - if (url) axios.get(url).then(response => setMarkdown(response.data)); - return
+ const baseUrl = url?.slice(0, url.lastIndexOf('/')); + const lines = markdown.split(/\r?\n/).map(line => resolveUrls(line, baseUrl)); + return
}; -- cgit v1.2.3 From b74897afe41962312828ffd362b7dd5ee8336775 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Thu, 9 Apr 2020 12:05:35 +0300 Subject: feat: wrap no-section header into Typography --- src/lib/Markdown/Section.tsx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/lib/Markdown/Section.tsx b/src/lib/Markdown/Section.tsx index 987b771..1fcc46f 100644 --- a/src/lib/Markdown/Section.tsx +++ b/src/lib/Markdown/Section.tsx @@ -1,5 +1,6 @@ import React from 'react'; import ContentSection from '../ContentSection/ContentSection'; +import { Typography } from '@material-ui/core'; import Content from './Content'; import { ParserPropTypes } from './types'; @@ -32,7 +33,7 @@ const Section: React.FC = ({ rawLines, level = 0 }) => { if (!level) return ( <> - + ) -- cgit v1.2.3 From 6d36ca60373f4d89d79209d0c6324d6eda5a001d Mon Sep 17 00:00:00 2001 From: eug-vs Date: Thu, 9 Apr 2020 12:11:11 +0300 Subject: fix: correct linting errors --- src/lib/Markdown/Markdown.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/lib/Markdown/Markdown.tsx b/src/lib/Markdown/Markdown.tsx index c929c71..82e889c 100644 --- a/src/lib/Markdown/Markdown.tsx +++ b/src/lib/Markdown/Markdown.tsx @@ -8,7 +8,7 @@ interface PropTypes { url?: string; } -const resolveUrls = (line: string, baseUrl: string = ''): string => { +const resolveUrls = (line: string, baseUrl: string): string => { return line.replace(/src="(?!http)(.*)"[\s>]/, (match, url, offset, string) => `src="${baseUrl}/${url}?sanitize=true"`) .replace(/\[(.*\]?.*)\]\((?!http)(.+?)\)/, (match, text, url, offset, string) => `[${text}](${baseUrl}/${url})`); } @@ -22,7 +22,7 @@ const Markdown: React.FC = ({ data, url }) => { if (!url) setMarkdown(data || ''); }, [data, url]); - const baseUrl = url?.slice(0, url.lastIndexOf('/')); + const baseUrl = url?.slice(0, url.lastIndexOf('/')) || ''; const lines = markdown.split(/\r?\n/).map(line => resolveUrls(line, baseUrl)); return
}; -- cgit v1.2.3 From d1bcd3348c67cfbcd83178c9710cc54de9f776e8 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Thu, 9 Apr 2020 12:20:34 +0300 Subject: feat: consider 1-line html tag --- src/lib/Markdown/Content.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/lib/Markdown/Content.tsx b/src/lib/Markdown/Content.tsx index 5895df9..5816214 100644 --- a/src/lib/Markdown/Content.tsx +++ b/src/lib/Markdown/Content.tsx @@ -49,7 +49,7 @@ const Content: React.FC = ({ rawLines }) => { buffer =
    {dottedListLines.map(li =>
  • )}
; } else if ((buffer = denotesOpenHtml(line))) { const tag = buffer; - const closeIndex = rawLines.findIndex(line => denotesClosingHtml(line, tag)); + const closeIndex = denotesClosingHtml(line, tag) ? -1 : rawLines.findIndex(line => denotesClosingHtml(line, tag)); const htmlLines = rawLines.splice(0, closeIndex + 1); htmlLines.unshift(line); buffer =
; -- cgit v1.2.3