diff options
author | Michael Muré <batolettre@gmail.com> | 2021-04-12 19:26:29 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-12 19:26:29 +0200 |
commit | 62fb09a53cc626ac581f33b466a1cdf14eb6ed89 (patch) | |
tree | c887d96fab77a6c312cf7047daa4d6944b53c89e /webui/src/components/Content | |
parent | 491f0ea9513e8080f6b38885b1a05f47fb56d9dc (diff) | |
parent | acfaf6b50f2ecb16add94874c0f8dfc401788d9b (diff) | |
download | git-bug-62fb09a53cc626ac581f33b466a1cdf14eb6ed89.tar.gz |
Merge pull request #624 from GlancingMind/upstream-fix-and-improve-rendering-of-markdown-elements
WebUI: Fix and improve rendering of markdown elements
Diffstat (limited to 'webui/src/components/Content')
-rw-r--r-- | webui/src/components/Content/AnchorTag.tsx | 38 | ||||
-rw-r--r-- | webui/src/components/Content/BlockQuoteTag.tsx | 21 | ||||
-rw-r--r-- | webui/src/components/Content/ImageTag.tsx | 9 | ||||
-rw-r--r-- | webui/src/components/Content/index.tsx | 14 |
4 files changed, 75 insertions, 7 deletions
diff --git a/webui/src/components/Content/AnchorTag.tsx b/webui/src/components/Content/AnchorTag.tsx new file mode 100644 index 00000000..47d5e2fa --- /dev/null +++ b/webui/src/components/Content/AnchorTag.tsx @@ -0,0 +1,38 @@ +import React from 'react'; +import { Link } from 'react-router-dom'; + +import { makeStyles } from '@material-ui/core/styles'; + +const useStyles = makeStyles((theme) => ({ + tag: { + color: theme.palette.text.secondary, + }, +})); + +const AnchorTag = ({ children, href }: React.HTMLProps<HTMLAnchorElement>) => { + const classes = useStyles(); + const origin = window.location.origin; + const destination = href === undefined ? '' : href; + const isInternalLink = + destination.startsWith('/') || destination.startsWith(origin); + const internalDestination = destination.replace(origin, ''); + const internalLink = ( + <Link className={classes.tag} to={internalDestination}> + {children} + </Link> + ); + const externalLink = ( + <a + className={classes.tag} + href={destination} + target="_blank" + rel="noopener noreferrer" + > + {children} + </a> + ); + + return isInternalLink ? internalLink : externalLink; +}; + +export default AnchorTag; diff --git a/webui/src/components/Content/BlockQuoteTag.tsx b/webui/src/components/Content/BlockQuoteTag.tsx new file mode 100644 index 00000000..18c34d8a --- /dev/null +++ b/webui/src/components/Content/BlockQuoteTag.tsx @@ -0,0 +1,21 @@ +import React from 'react'; + +import { makeStyles } from '@material-ui/core/styles'; + +const useStyles = makeStyles((theme) => ({ + tag: { + color: theme.palette.text.secondary, + borderLeftWidth: '0.5ch', + borderLeftStyle: 'solid', + borderLeftColor: theme.palette.text.secondary, + marginLeft: 0, + paddingLeft: '0.5rem', + }, +})); + +const BlockQuoteTag = (props: React.HTMLProps<HTMLPreElement>) => { + const classes = useStyles(); + return <blockquote className={classes.tag} {...props} />; +}; + +export default BlockQuoteTag; diff --git a/webui/src/components/Content/ImageTag.tsx b/webui/src/components/Content/ImageTag.tsx index 70ee1bc0..29b01da3 100644 --- a/webui/src/components/Content/ImageTag.tsx +++ b/webui/src/components/Content/ImageTag.tsx @@ -14,9 +14,12 @@ const ImageTag = ({ }: React.ImgHTMLAttributes<HTMLImageElement>) => { const classes = useStyles(); return ( - <a href={props.src} target="_blank" rel="noopener noreferrer nofollow"> - <img className={classes.tag} alt={alt} {...props} /> - </a> + <> + <a href={props.src} target="_blank" rel="noopener noreferrer nofollow"> + <img className={classes.tag} alt={alt} {...props} /> + </a> + <br /> + </> ); }; diff --git a/webui/src/components/Content/index.tsx b/webui/src/components/Content/index.tsx index 56e52e1e..e4018809 100644 --- a/webui/src/components/Content/index.tsx +++ b/webui/src/components/Content/index.tsx @@ -1,26 +1,32 @@ import React from 'react'; +import gemoji from 'remark-gemoji'; import html from 'remark-html'; import parse from 'remark-parse'; import remark2react from 'remark-react'; import unified from 'unified'; +import AnchorTag from './AnchorTag'; +import BlockQuoteTag from './BlockQuoteTag'; import ImageTag from './ImageTag'; import PreTag from './PreTag'; type Props = { markdown: string }; const Content: React.FC<Props> = ({ markdown }: Props) => { - const processor = unified() + const content = unified() .use(parse) + .use(gemoji) .use(html) .use(remark2react, { remarkReactComponents: { img: ImageTag, pre: PreTag, + a: AnchorTag, + blockquote: BlockQuoteTag, }, - }); + }) + .processSync(markdown).result; - const contents: React.ReactNode = processor.processSync(markdown).contents; - return <>{contents}</>; + return <>{content}</>; }; export default Content; |