diff options
author | eug-vs <eug-vs@keemail.me> | 2021-01-11 22:29:03 +0300 |
---|---|---|
committer | eug-vs <eug-vs@keemail.me> | 2021-01-11 22:29:03 +0300 |
commit | 53e41901558319e6063fdcbca4f6150101c57f10 (patch) | |
tree | 0ee56a21b898e880c65406baf2c48c47bfda84f0 /src/lib/Markdown | |
parent | 4ad1f101101771350b253762c5b02d57df6f4ddd (diff) | |
download | react-benzin-53e41901558319e6063fdcbca4f6150101c57f10.tar.gz |
feat: allow passing variable context
Diffstat (limited to 'src/lib/Markdown')
-rw-r--r-- | src/lib/Markdown/Markdown.tsx | 27 | ||||
-rw-r--r-- | src/lib/Markdown/remark-gemoji.d.ts | 2 | ||||
-rw-r--r-- | src/lib/Markdown/types.d.ts | 6 |
3 files changed, 22 insertions, 13 deletions
diff --git a/src/lib/Markdown/Markdown.tsx b/src/lib/Markdown/Markdown.tsx index 955aeda..9bc05a0 100644 --- a/src/lib/Markdown/Markdown.tsx +++ b/src/lib/Markdown/Markdown.tsx @@ -12,6 +12,7 @@ import Image from './Image'; interface PropTypes { data?: string; url?: string; + context?: any; } const resolveUrls = (line: string, baseUrl: string): string => line.replace( @@ -22,15 +23,8 @@ const resolveUrls = (line: string, baseUrl: string): string => line.replace( (match, text, url) => `[${text}](${baseUrl}/${url})`, ); -const renderers = { - heading: Heading, - inlineCode: InlineCode, - code: CodeBlock, - link: Link, - image: Image, -}; -const Markdown: React.FC<PropTypes> = ({ data, url }) => { +const Markdown: React.FC<PropTypes> = ({ data, url, context = {} }) => { const [markdown, setMarkdown] = useState<string>(data || ''); if (url) axios.get(url).then(response => setMarkdown(response.data)); @@ -43,15 +37,26 @@ const Markdown: React.FC<PropTypes> = ({ data, url }) => { const sanitized = resolveUrls(markdown, baseUrl); + const renderers = { + heading: Heading, + inlineCode: InlineCode, + link: Link, + image: Image, + code: ({ language, value }: any) => { + if (language === 'react') return context[value] || null; + return CodeBlock({ value }); + }, + }; + + return ( <Typography> <ReactMarkdown + source={sanitized} renderers={renderers} plugins={[emoji]} allowDangerousHtml - > - {sanitized} - </ReactMarkdown> + /> </Typography> ); }; diff --git a/src/lib/Markdown/remark-gemoji.d.ts b/src/lib/Markdown/remark-gemoji.d.ts deleted file mode 100644 index d4b4bf6..0000000 --- a/src/lib/Markdown/remark-gemoji.d.ts +++ /dev/null @@ -1,2 +0,0 @@ -declare module 'remark-gemoji'; - diff --git a/src/lib/Markdown/types.d.ts b/src/lib/Markdown/types.d.ts new file mode 100644 index 0000000..f39e4f2 --- /dev/null +++ b/src/lib/Markdown/types.d.ts @@ -0,0 +1,6 @@ +declare module 'remark-gemoji'; +declare module '*.md' { + let Markdown: string; + export default Markdown; +} + |