Skip to content
This repository has been archived by the owner on Jul 23, 2024. It is now read-only.

Commit

Permalink
Merge pull request #156 from parodos-dev/dl/markdown-notification
Browse files Browse the repository at this point in the history
Render notifications with markdown
  • Loading branch information
wKich authored Jun 6, 2023
2 parents 302b1e9 + c2bebc4 commit 80e54db
Show file tree
Hide file tree
Showing 4 changed files with 252 additions and 4 deletions.
2 changes: 2 additions & 0 deletions plugins/parodos/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@
"luxon": "^3.2.1",
"mobx": "^5.15.4",
"mobx-react": "^6.2.0",
"react-markdown": "^8.0.7",
"react-use": "^17.2.4",
"remark-gfm": "^3.0.1",
"use-immer": "^0.8.1",
"zod": "^3.21.4",
"zustand": "^4.3.6"
Expand Down
215 changes: 215 additions & 0 deletions plugins/parodos/src/components/markdown/renderers.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
import React, { ComponentPropsWithoutRef } from 'react';
import {
Checkbox,
List,
ListItem,
makeStyles,
Table,
TableBody,
TableCell,
TableHead,
TableRow,
Typography,
} from '@material-ui/core';
import { Components } from 'react-markdown';
import { Link } from '@backstage/core-components';

const useStyles = makeStyles(theme => ({
p: {
paddingTop: theme.spacing(1),
paddingBottom: theme.spacing(1),
},
strong: {
fontWeight: theme.typography.fontWeightBold as number,
},
em: {
fontStyle: 'italic',
},
ol: {
listStyleType: 'decimal',
'& &': {
listStyleType: 'lower-alpha',
paddingLeft: theme.spacing(2),
},
},
ul: {
listStyleType: 'disc',
'& &': {
listStyleType: 'circle',
paddingLeft: theme.spacing(2),
},
},
li: {
display: 'list-item',
padding: `${theme.spacing(0.5)}px 0`,
'&.task-list-item': {
listStyleType: 'none',
},
},
code: {
padding: `${theme.spacing(0.25)}px ${theme.spacing(0.5)}px`,
backgroundColor: theme.palette.background.default,
},
blockquote: {
borderLeft: `4px solid ${theme.palette.divider}`,
padding: `0 ${theme.spacing(1)}px`,
marginBottom: theme.spacing(1),
'& > p': {
padding: 0,
},
},
table: {
marginBottom: theme.spacing(1),
},
pre: {
backgroundColor: theme.palette.background.default,
padding: theme.spacing(1),
},
checkbox: {
padding: 0,
},
}));

const P = (props: ComponentPropsWithoutRef<'p'>) => {
const classes = useStyles();
return (
<Typography
className={classes.p}
variant="body1"
children={props.children}
/>
);
};

const Strong = (props: ComponentPropsWithoutRef<'strong'>) => {
const classes = useStyles();
return (
<Typography
className={classes.strong}
variant="body1"
component="strong"
children={props.children}
/>
);
};

const Em = (props: ComponentPropsWithoutRef<'em'>) => {
const classes = useStyles();
return (
<Typography
className={classes.em}
variant="body1"
component="em"
children={props.children}
/>
);
};

const Ol = (props: ComponentPropsWithoutRef<'ol'>) => {
const classes = useStyles();
return (
<List className={classes.ol} component="ol" children={props.children} />
);
};

const Ul = (props: ComponentPropsWithoutRef<'ul'>) => {
const classes = useStyles();
return (
<List className={classes.ul} component="ul" children={props.children} />
);
};

const Li = (props: ComponentPropsWithoutRef<'li'>) => {
const classes = useStyles();
return (
<ListItem
className={`${classes.li} ${props.className}`}
children={props.children}
/>
);
};

const Code = (props: ComponentPropsWithoutRef<'code'>) => {
const classes = useStyles();
return (
<Typography
className={classes.code}
variant="body1"
component="code"
children={props.children}
/>
);
};

const Blockquote = (props: ComponentPropsWithoutRef<'blockquote'>) => {
const classes = useStyles();
return (
<Typography
className={classes.blockquote}
variant="body1"
component="blockquote"
children={props.children}
/>
);
};

const StyledTable = (props: ComponentPropsWithoutRef<'table'>) => {
const classes = useStyles();
return <Table className={classes.table} children={props.children} />;
};

const Pre = (props: ComponentPropsWithoutRef<'pre'>) => {
const classes = useStyles();
return (
<Typography
className={classes.pre}
variant="body1"
component="pre"
children={props.children}
/>
);
};

const StyledCheckbox = (props: ComponentPropsWithoutRef<'input'>) => {
const classes = useStyles();
return (
<Checkbox
className={classes.checkbox}
disabled={props.disabled}
checked={props.checked}
/>
);
};

export const renderers: Components = {
h1: props => <Typography variant="h1" children={props.children} />,
h2: props => <Typography variant="h2" children={props.children} />,
h3: props => <Typography variant="h3" children={props.children} />,
h4: props => <Typography variant="h4" children={props.children} />,
h5: props => <Typography variant="h5" children={props.children} />,
h6: props => <Typography variant="h6" children={props.children} />,
p: P,
strong: Strong,
em: Em,
ol: Ol,
ul: Ul,
li: Li,
code: Code,
a: props => <Link to={props.href as string} children={props.children} />,
blockquote: Blockquote,
table: StyledTable,
thead: props => <TableHead children={props.children} />,
tbody: props => <TableBody children={props.children} />,
tr: props => <TableRow children={props.children} />,
th: props => <TableCell children={props.children} />,
td: props => <TableCell children={props.children} />,
pre: Pre,
input: props => {
if (props.type === 'checkbox') {
return (
<StyledCheckbox disabled={props.disabled} checked={props.checked} />
);
}
return null;
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import React, {
useState,
SyntheticEvent,
} from 'react';
import ReactMarkdown from 'react-markdown';
import remarkGfm from 'remark-gfm';
import Collapse from '@material-ui/core/Collapse';
import IconButton from '@material-ui/core/IconButton';
import Table from '@material-ui/core/Table';
Expand All @@ -20,6 +22,7 @@ import Checkbox from '@material-ui/core/Checkbox';
import { fetchApiRef, useApi } from '@backstage/core-plugin-api';
import { useStore } from '../../stores/workflowStore/workflowStore';
import cs from 'classnames';
import { renderers } from '../markdown/renderers';

const useRowStyles = makeStyles(theme => ({
root: {
Expand Down Expand Up @@ -139,7 +142,14 @@ export function NotificationListItem({
<Table>
<TableBody>
<TableRow className={styles.nested}>
<TableCell>{notification.body}</TableCell>
<TableCell>
<ReactMarkdown
remarkPlugins={[remarkGfm]}
components={renderers}
>
{notification.body}
</ReactMarkdown>
</TableCell>
</TableRow>
</TableBody>
</Table>
Expand Down
27 changes: 24 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7519,9 +7519,9 @@
integrity sha512-EEhsLsD6UsDM1yFhAvy0Cjr6VwmpMWqFBCb9w07wVugF7w9nfajxLuVmngTIpgS6svCnm6Vaw+MZhoDCKnOfsw==

"@types/react-dom@*", "@types/react-dom@<18.0.0", "@types/react-dom@^17":
version "17.0.19"
resolved "https://registry.yarnpkg.com/@types/react-dom/-/react-dom-17.0.19.tgz#36feef3aa35d045cacd5ed60fe0eef5272f19492"
integrity sha512-PiYG40pnQRdPHnlf7tZnp0aQ6q9tspYr72vD61saO6zFCybLfMqwUCN0va1/P+86DXn18ZWeW30Bk7xlC5eEAQ==
version "17.0.20"
resolved "https://registry.yarnpkg.com/@types/react-dom/-/react-dom-17.0.20.tgz#e0c8901469d732b36d8473b40b679ad899da1b53"
integrity sha512-4pzIjSxDueZZ90F52mU3aPoogkHIoSIDG+oQ+wQK7Cy2B9S+MvOqY0uEA/qawKz381qrEDkvpwyt8Bm31I8sbA==
dependencies:
"@types/react" "^17"

Expand Down Expand Up @@ -18975,6 +18975,27 @@ react-markdown@^8.0.0:
unist-util-visit "^4.0.0"
vfile "^5.0.0"

react-markdown@^8.0.7:
version "8.0.7"
resolved "https://registry.yarnpkg.com/react-markdown/-/react-markdown-8.0.7.tgz#c8dbd1b9ba5f1c5e7e5f2a44de465a3caafdf89b"
integrity sha512-bvWbzG4MtOU62XqBx3Xx+zB2raaFFsq4mYiAzfjXJMEz2sixgeAfraA3tvzULF02ZdOMUOKTBFFaZJDDrq+BJQ==
dependencies:
"@types/hast" "^2.0.0"
"@types/prop-types" "^15.0.0"
"@types/unist" "^2.0.0"
comma-separated-tokens "^2.0.0"
hast-util-whitespace "^2.0.0"
prop-types "^15.0.0"
property-information "^6.0.0"
react-is "^18.0.0"
remark-parse "^10.0.0"
remark-rehype "^10.0.0"
space-separated-tokens "^2.0.0"
style-to-object "^0.4.0"
unified "^10.0.0"
unist-util-visit "^4.0.0"
vfile "^5.0.0"

react-measure@^2.3.0:
version "2.5.2"
resolved "https://registry.yarnpkg.com/react-measure/-/react-measure-2.5.2.tgz#4ffc410e8b9cb836d9455a9ff18fc1f0fca67f89"
Expand Down

0 comments on commit 80e54db

Please sign in to comment.