From 351cbf596fdda867240d76b632f9d205f343153c Mon Sep 17 00:00:00 2001 From: eug-vs Date: Sun, 5 Apr 2020 05:06:34 +0300 Subject: feat: parse links and images --- src/lib/Markdown/Paragraph.tsx | 42 +++++++++++++++++++++++++++++------------- 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/src/lib/Markdown/Paragraph.tsx b/src/lib/Markdown/Paragraph.tsx index 5ecedfc..a473142 100644 --- a/src/lib/Markdown/Paragraph.tsx +++ b/src/lib/Markdown/Paragraph.tsx @@ -1,23 +1,25 @@ import React from 'react'; +import { Link } from '@material-ui/core'; interface PropTypes { data: string; } -type RuleName = 'bold' | 'italic' | 'inlineCode' | 'strikeThrough'; +type InlineRuleName = 'bold' | 'italic' | 'code' | 'strikeThrough'; -interface RuleProperties { +interface InlineRuleProperties { enclosure: string; style: React.CSSProperties; pattern?: RegExp; } -const rules: Record= { // Order matters - lowest property has highest priority +const inlineRules: Record= { + // Order matters - lowest property has highest priority strikeThrough: { enclosure: '~~', style: { textDecoration: 'line-through' }, }, - inlineCode: { + code: { enclosure: '`', style: { background: '#444444', padding: '4px' }, }, @@ -31,28 +33,41 @@ const rules: Record= { // Order matters - lowest prop }, }; -const ruleNames = Object.keys(rules) as RuleName[]; +const inlineRuleNames = Object.keys(inlineRules) as InlineRuleName[]; -const capture = (enclosure: string): RegExp => { +const captureInline = (enclosure: string): RegExp => { return new RegExp(enclosure + '([^' + enclosure + ']+)' + enclosure); } -const captureSplit = (enclosure: string): string => { +const captureInlineSplit = (enclosure: string): string => { return '(' + enclosure + '[^' + enclosure + ']+' + enclosure + ')'; } +const concealRegex = /!?\[(.+?)\]\((.+?)\)/; +const concealRegexSplit = /(!?\[.+?\]\(.+?\))/g; +const rawLinkRegex = /((?:(?:[A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=+$,\w]+@)?[A-Za-z0-9.-]+|(?:www\.|[-;:&=+$,\w]+@)[A-Za-z0-9.-]+)(?:(?:\/[+~%/.\w-_]*)?\??(?:[-+=&;%@.\w_]*)#?(?:[.!/\\\w]*))?)/; -const ruleSplitPatterns: string[] = []; -ruleNames.forEach(name => { - rules[name].pattern = capture(rules[name].enclosure); - ruleSplitPatterns.push(captureSplit(rules[name].enclosure)); +const ruleSplitPatterns: string[] = [concealRegexSplit.source, rawLinkRegex.source]; +inlineRuleNames.forEach(name => { + const enclosure = inlineRules[name].enclosure; + inlineRules[name].pattern = captureInline(enclosure); + ruleSplitPatterns.push(captureInlineSplit(enclosure)); }); const splitter = new RegExp(ruleSplitPatterns.join('|')); const SyntaxSpan: React.FC = ({ data }) => { if (!data) return null; + + const conceal = concealRegex.exec(data); + if (conceal) { + if (data[0] === '!') return {conceal[1]}; + return {conceal[1]}; + } + + if (data.match(rawLinkRegex)) return {data}; + let span = <>{data}; - ruleNames.forEach(name => { - const rule = rules[name]; + inlineRuleNames.forEach(name => { + const rule = inlineRules[name]; const match = data.match(rule.pattern || ''); if (match) span = {match[1]}; }); @@ -66,3 +81,4 @@ const Paragraph: React.FC = ({ data }) => { export default Paragraph; + -- cgit v1.2.3