aboutsummaryrefslogtreecommitdiff
path: root/src/lib/Markdown
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/Markdown')
-rw-r--r--src/lib/Markdown/Markdown.tsx27
-rw-r--r--src/lib/Markdown/remark-gemoji.d.ts2
-rw-r--r--src/lib/Markdown/types.d.ts6
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;
+}
+