From fa16b78695099fe28d233edc8cb6fce67e5e2648 Mon Sep 17 00:00:00 2001
From: eug-vs <eug-vs@keemail.me>
Date: Thu, 14 Jan 2021 19:51:16 +0300
Subject: refactor: rename data -> source prop

---
 src/lib/Markdown/Markdown.tsx | 41 ++++++++++++++++++++++++++---------------
 1 file changed, 26 insertions(+), 15 deletions(-)

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

diff --git a/src/lib/Markdown/Markdown.tsx b/src/lib/Markdown/Markdown.tsx
index 4fa6a9a..32732ec 100644
--- a/src/lib/Markdown/Markdown.tsx
+++ b/src/lib/Markdown/Markdown.tsx
@@ -10,7 +10,7 @@ import Heading from './Heading';
 import Image from './Image';
 
 interface PropTypes {
-  data?: string;
+  source?: string;
   url?: string;
   // eslint-disable-next-line @typescript-eslint/no-explicit-any
   context?: Record<string, any>;
@@ -24,33 +24,44 @@ const resolveUrls = (line: string, baseUrl: string): string => line.replace(
   (match, text, url) => `[${text}](${baseUrl}/${url})`,
 );
 
+// eslint-disable-next-line @typescript-eslint/no-explicit-any
+const WrappedInlineCode = (context: Record<string, any>): React.FC => ({ children }) => {
+  if (typeof children === 'string' && children?.startsWith('$')) {
+    const symbol = children.slice(1);
+    return context[symbol] || null;
+  }
+  return <InlineCode>{children}</InlineCode>;
+};
+
+const Markdown: React.FC<PropTypes> = ({
+  children,
+  url,
+  source,
+  context = {},
+}) => {
+  const [markdown, setMarkdown] = useState<string>(source || '');
 
-const Markdown: React.FC<PropTypes> = ({ data, url, context = {} }) => {
-  const [markdown, setMarkdown] = useState<string>(data || '');
+  useEffect(() => {
+    if (url) axios.get(url).then(response => setMarkdown(response.data));
+  }, [url]);
 
-  if (url) axios.get(url).then(response => setMarkdown(response.data));
+  useEffect(() => {
+    if (source) setMarkdown(source);
+  }, [source]);
 
   useEffect(() => {
-    if (!url) setMarkdown(data || '');
-  }, [data, url]);
+    if (children && typeof children === 'string') setMarkdown(children);
+  }, [children]);
 
   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,
     code: CodeBlock,
     link: Link,
     image: Image,
-    inlineCode: WrappedInlineCode,
+    inlineCode: WrappedInlineCode(context),
   };
 
   return (
-- 
cgit v1.2.3


From ce028df672325e2efaef2f9dbee05701328b0924 Mon Sep 17 00:00:00 2001
From: eug-vs <eug-vs@keemail.me>
Date: Thu, 14 Jan 2021 19:54:17 +0300
Subject: feat: allow passing plugins to Markdown

---
 src/lib/Markdown/Markdown.tsx | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

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

diff --git a/src/lib/Markdown/Markdown.tsx b/src/lib/Markdown/Markdown.tsx
index 32732ec..c0389dc 100644
--- a/src/lib/Markdown/Markdown.tsx
+++ b/src/lib/Markdown/Markdown.tsx
@@ -14,6 +14,8 @@ interface PropTypes {
   url?: string;
   // eslint-disable-next-line @typescript-eslint/no-explicit-any
   context?: Record<string, any>;
+  // eslint-disable-next-line @typescript-eslint/no-explicit-any
+  plugins?: any[]
 }
 
 const resolveUrls = (line: string, baseUrl: string): string => line.replace(
@@ -38,6 +40,7 @@ const Markdown: React.FC<PropTypes> = ({
   url,
   source,
   context = {},
+  plugins = [],
 }) => {
   const [markdown, setMarkdown] = useState<string>(source || '');
 
@@ -69,7 +72,7 @@ const Markdown: React.FC<PropTypes> = ({
       <ReactMarkdown
         source={sanitized}
         renderers={renderers}
-        plugins={[emoji]}
+        plugins={[emoji, ...plugins]}
         allowDangerousHtml
       />
     </Typography>
-- 
cgit v1.2.3

