diff options
author | eug-vs <eug-vs@keemail.me> | 2021-01-11 22:40:43 +0300 |
---|---|---|
committer | eug-vs <eug-vs@keemail.me> | 2021-01-11 22:45:38 +0300 |
commit | e3253a726d4bb71825e392cbde89d05bb3146dc1 (patch) | |
tree | 8a1d3d030704919e7940695c3c998438f6c41159 /src/lib | |
parent | 53e41901558319e6063fdcbca4f6150101c57f10 (diff) | |
download | react-benzin-e3253a726d4bb71825e392cbde89d05bb3146dc1.tar.gz |
feat: use inline syntax for context
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/Markdown/Markdown.tsx | 19 |
1 files changed, 11 insertions, 8 deletions
diff --git a/src/lib/Markdown/Markdown.tsx b/src/lib/Markdown/Markdown.tsx index 9bc05a0..c91bc14 100644 --- a/src/lib/Markdown/Markdown.tsx +++ b/src/lib/Markdown/Markdown.tsx @@ -12,7 +12,7 @@ import Image from './Image'; interface PropTypes { data?: string; url?: string; - context?: any; + context?: Record<string, any>; } const resolveUrls = (line: string, baseUrl: string): string => line.replace( @@ -34,21 +34,24 @@ const Markdown: React.FC<PropTypes> = ({ data, url, context = {} }) => { }, [data, url]); const baseUrl = url?.slice(0, url.lastIndexOf('/')) || ''; - const sanitized = resolveUrls(markdown, baseUrl); + const WrappedInlineCode: React.FC = ({ children }) => { + if (typeof children === 'string' && children?.startsWith('$')) { + const symbol = children.slice(1); + return context[symbol] || null; + } + return <InlineCode>{children}</InlineCode>; + }; + const renderers = { heading: Heading, - inlineCode: InlineCode, + code: CodeBlock, link: Link, image: Image, - code: ({ language, value }: any) => { - if (language === 'react') return context[value] || null; - return CodeBlock({ value }); - }, + inlineCode: WrappedInlineCode, }; - return ( <Typography> <ReactMarkdown |