diff options
author | eug-vs <eug-vs@keemail.me> | 2021-01-11 22:40:43 +0300 |
---|---|---|
committer | eug-vs <eug-vs@keemail.me> | 2021-01-11 22:45:38 +0300 |
commit | e3253a726d4bb71825e392cbde89d05bb3146dc1 (patch) | |
tree | 8a1d3d030704919e7940695c3c998438f6c41159 | |
parent | 53e41901558319e6063fdcbca4f6150101c57f10 (diff) | |
download | react-benzin-e3253a726d4bb71825e392cbde89d05bb3146dc1.tar.gz |
feat: use inline syntax for context
-rw-r--r-- | src/demo/content.md | 12 | ||||
-rw-r--r-- | src/lib/Markdown/Markdown.tsx | 19 |
2 files changed, 15 insertions, 16 deletions
diff --git a/src/demo/content.md b/src/demo/content.md index ad61d33..41607a9 100644 --- a/src/demo/content.md +++ b/src/demo/content.md @@ -1,9 +1,5 @@ ## Markdown -[Markdown file](${url}) *(... -```react -fileName -``` -)* that you can see on the left was parsed and rendered by **BENZIN**! :rocket: +[Markdown file](${url}) *(...`$fileName`)* that you can see on the left was parsed and rendered by **BENZIN**! :rocket: Switch between tabs on the header to explore other markdown templates. :recycle: Templates on the left are being loaded from the [GitHub](https://github.com), though this pane is generated from plaintext. :pen: ## How do I use this feature? @@ -12,6 +8,6 @@ import Markdown from \'react-benzin\'; const data = \'# Header\\nHello, *world!*\'; ReactDOM.render(<Markdown data={data}/>, document.getElementById(\'root\')); ``` -```react -tryButton -``` + +`$tryButton` + 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 |