From e3253a726d4bb71825e392cbde89d05bb3146dc1 Mon Sep 17 00:00:00 2001
From: eug-vs <eug-vs@keemail.me>
Date: Mon, 11 Jan 2021 22:40:43 +0300
Subject: feat: use inline syntax for context

---
 src/lib/Markdown/Markdown.tsx | 19 +++++++++++--------
 1 file changed, 11 insertions(+), 8 deletions(-)

(limited to 'src/lib/Markdown/Markdown.tsx')

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
-- 
cgit v1.2.3

