diff options
author | Eugene Sokolov <eug-vs@keemail.me> | 2021-01-14 18:57:11 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-01-14 18:57:11 +0200 |
commit | 0e024a9a0fd4ca25262976e78b6a58cf8e369f60 (patch) | |
tree | 0e3142abe7e3b57d07d9e8dcecb014ed555082fa | |
parent | 5f72950c56ea8853b41b64a092a2e98dbf53cf3f (diff) | |
parent | ce028df672325e2efaef2f9dbee05701328b0924 (diff) | |
download | react-benzin-0e024a9a0fd4ca25262976e78b6a58cf8e369f60.tar.gz |
Merge pull request #21 from eug-vs/refinements
Refinements
-rw-r--r-- | src/demo/content.md | 6 | ||||
-rw-r--r-- | src/index.tsx | 2 | ||||
-rw-r--r-- | src/lib/ContentSection/ContentSection.tsx | 51 | ||||
-rw-r--r-- | src/lib/Markdown/Markdown.tsx | 46 |
4 files changed, 34 insertions, 71 deletions
diff --git a/src/demo/content.md b/src/demo/content.md index 41607a9..01d848f 100644 --- a/src/demo/content.md +++ b/src/demo/content.md @@ -4,9 +4,9 @@ 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? ``` -import Markdown from \'react-benzin\'; -const data = \'# Header\\nHello, *world!*\'; -ReactDOM.render(<Markdown data={data}/>, document.getElementById(\'root\')); +import Markdown from 'react-benzin'; +const data = '# Header\nHello, *world!*'; +ReactDOM.render(<Markdown data={data}/>, document.getElementById('root')); ``` `$tryButton` diff --git a/src/index.tsx b/src/index.tsx index aa786ac..e37e7aa 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -133,7 +133,7 @@ const App: React.FC = () => { let primaryWindowContent = <Markdown url={url} />; if (page === 'custom') primaryWindowContent = <CustomPage />; else if (page === 'live preview') { - primaryWindowContent = <Markdown data={livePreviewData || '# Start typing in the right window!'} />; + primaryWindowContent = <Markdown source={livePreviewData || '# Start typing in the right window!'} />; } const tryButton = ( diff --git a/src/lib/ContentSection/ContentSection.tsx b/src/lib/ContentSection/ContentSection.tsx deleted file mode 100644 index f18684c..0000000 --- a/src/lib/ContentSection/ContentSection.tsx +++ /dev/null @@ -1,51 +0,0 @@ -import React from 'react'; - -import { - Typography, - Divider, - makeStyles, - useMediaQuery, - Theme, -} from '@material-ui/core'; - - -interface PropTypes { - sectionName: string; - level?: number; -} - -const useStyles = makeStyles(theme => ({ - content: { - [theme.breakpoints.up('md')]: { - padding: theme.spacing(2, 2, 1, 3), - }, - [theme.breakpoints.down('sm')]: { - padding: theme.spacing(2, 0), - }, - marginBottom: theme.spacing(1), - }, -})); - -const ContentSection: React.FC<PropTypes> = ({ sectionName, children, level = 0 }) => { - const classes = useStyles(); - const isMobile = useMediaQuery((theme: Theme) => theme.breakpoints.down('sm')); - - let adjustedLevel = level + 2; // Make everything smaller - if (adjustedLevel > 6) adjustedLevel = 6; - - type Variant = 'h3' | 'h4' | 'h5' | 'h6'; - const variant: Variant = `h${adjustedLevel}` as Variant; - - return ( - <> - <Typography variant={variant}>{sectionName}</Typography> - <Divider variant={isMobile ? 'fullWidth' : 'middle'} /> - <Typography component="div" className={classes.content}> - {children} - </Typography> - </> - ); -}; - - -export default ContentSection; diff --git a/src/lib/Markdown/Markdown.tsx b/src/lib/Markdown/Markdown.tsx index 4fa6a9a..c0389dc 100644 --- a/src/lib/Markdown/Markdown.tsx +++ b/src/lib/Markdown/Markdown.tsx @@ -10,10 +10,12 @@ 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>; + // eslint-disable-next-line @typescript-eslint/no-explicit-any + plugins?: any[] } const resolveUrls = (line: string, baseUrl: string): string => line.replace( @@ -24,33 +26,45 @@ 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> = ({ data, url, context = {} }) => { - const [markdown, setMarkdown] = useState<string>(data || ''); +const Markdown: React.FC<PropTypes> = ({ + children, + url, + source, + context = {}, + plugins = [], +}) => { + const [markdown, setMarkdown] = useState<string>(source || ''); - if (url) axios.get(url).then(response => setMarkdown(response.data)); + useEffect(() => { + if (url) axios.get(url).then(response => setMarkdown(response.data)); + }, [url]); useEffect(() => { - if (!url) setMarkdown(data || ''); - }, [data, url]); + if (source) setMarkdown(source); + }, [source]); + + useEffect(() => { + 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 ( @@ -58,7 +72,7 @@ const Markdown: React.FC<PropTypes> = ({ data, url, context = {} }) => { <ReactMarkdown source={sanitized} renderers={renderers} - plugins={[emoji]} + plugins={[emoji, ...plugins]} allowDangerousHtml /> </Typography> |