aboutsummaryrefslogtreecommitdiff
path: root/src/lib/Markdown/Markdown.tsx
diff options
context:
space:
mode:
authoreug-vs <eug-vs@keemail.me>2021-01-11 22:29:03 +0300
committereug-vs <eug-vs@keemail.me>2021-01-11 22:29:03 +0300
commit53e41901558319e6063fdcbca4f6150101c57f10 (patch)
tree0ee56a21b898e880c65406baf2c48c47bfda84f0 /src/lib/Markdown/Markdown.tsx
parent4ad1f101101771350b253762c5b02d57df6f4ddd (diff)
downloadreact-benzin-53e41901558319e6063fdcbca4f6150101c57f10.tar.gz
feat: allow passing variable context
Diffstat (limited to 'src/lib/Markdown/Markdown.tsx')
-rw-r--r--src/lib/Markdown/Markdown.tsx27
1 files changed, 16 insertions, 11 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>
);
};