Add markdown rendering

This commit is contained in:
Kamran Ahmed
2021-08-20 17:06:26 +02:00
parent c37297f822
commit 197f1f6824
28 changed files with 18619 additions and 4587 deletions

View File

@ -1,6 +1,6 @@
import { Box, Container, Flex, Image, Link, Stack } from '@chakra-ui/react';
export function Header() {
export function GlobalHeader() {
return (
<Box bg='gray.900' p='20px 30px'>
<Container maxW='container.md'>

View File

@ -0,0 +1,3 @@
<svg width="29" height="29">
<path d="M23.2 5H5.8a.8.8 0 0 0-.8.8V23.2c0 .44.35.8.8.8h9.3v-7.13h-2.38V13.9h2.38v-2.38c0-2.45 1.55-3.66 3.74-3.66 1.05 0 1.95.08 2.2.11v2.57h-1.5c-1.2 0-1.48.57-1.48 1.4v1.96h2.97l-.6 2.97h-2.37l.05 7.12h5.1a.8.8 0 0 0 .79-.8V5.8a.8.8 0 0 0-.8-.79"></path>
</svg>

After

Width:  |  Height:  |  Size: 298 B

View File

@ -0,0 +1,5 @@
<svg xmlns="http://www.w3.org/2000/svg" aria-label="GitHub" role="img" viewBox="0 0 512 512" width="22px" height="20px">
<rect width="512" height="512" rx="15%"/>
<path fill="#fff"
d="M335 499c14 0 12 17 12 17H165s-2-17 12-17c13 0 16-6 16-12l-1-50c-71 16-86-28-86-28-12-30-28-37-28-37-24-16 1-16 1-16 26 2 40 26 40 26 22 39 59 28 74 22 2-17 9-28 16-35-57-6-116-28-116-126 0-28 10-51 26-69-3-6-11-32 3-67 0 0 21-7 70 26 42-12 86-12 128 0 49-33 70-26 70-26 14 35 6 61 3 67 16 18 26 41 26 69 0 98-60 120-117 126 10 8 18 24 18 48l-1 70c0 6 3 12 16 12z"/>
</svg>

After

Width:  |  Height:  |  Size: 576 B

View File

@ -0,0 +1,4 @@
<svg viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true">
<path fill-rule="evenodd"
d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path>
</svg>

After

Width:  |  Height:  |  Size: 474 B

View File

@ -0,0 +1,3 @@
<svg width="29" height="29">
<path d="M22.05 7.54a4.47 4.47 0 0 0-3.3-1.46 4.53 4.53 0 0 0-4.53 4.53c0 .35.04.7.08 1.05A12.9 12.9 0 0 1 5 6.89a5.1 5.1 0 0 0-.65 2.26c.03 1.6.83 2.99 2.02 3.79a4.3 4.3 0 0 1-2.02-.57v.08a4.55 4.55 0 0 0 3.63 4.44c-.4.08-.8.13-1.21.16l-.81-.08a4.54 4.54 0 0 0 4.2 3.15 9.56 9.56 0 0 1-5.66 1.94l-1.05-.08c2 1.27 4.38 2.02 6.94 2.02 8.3 0 12.86-6.9 12.84-12.85.02-.24 0-.43 0-.65a8.68 8.68 0 0 0 2.26-2.34c-.82.38-1.7.62-2.6.72a4.37 4.37 0 0 0 1.95-2.51c-.84.53-1.81.9-2.83 1.13z"></path>
</svg>

After

Width:  |  Height:  |  Size: 529 B

View File

@ -0,0 +1,16 @@
import React from 'react';
// @ts-ignore
import { MDXProvider } from '@mdx-js/react';
import MdxComponents from './mdx-components';
type MdRendererType = {
children: React.ReactNode
};
export default function MdRenderer(props: MdRendererType) {
return (
<MDXProvider components={MdxComponents}>
{props.children}
</MDXProvider>
);
};

View File

@ -0,0 +1,18 @@
import React from 'react';
import { Link } from '@chakra-ui/react';
type EnrichedLinkProps = {
href: string;
children: React.ReactNode
}
export default function EnrichedLink(props: EnrichedLinkProps) {
// Is external URL or is a media URL
const isExternalUrl = /(^http(s)?:\/\/)|(\.(png|svg|jpeg|jpg)$)/.test(props.href);
return (
<Link fontWeight={600} href={props.href} target={isExternalUrl ? '_blank' : '_self'}>
{props.children}
</Link>
);
};

View File

@ -0,0 +1,22 @@
import React from 'react';
type BadgeLinkType = {
target: string;
variant: string;
badgeText: string;
href: string;
children: React.ReactNode
};
export function BadgeLink(props: BadgeLinkType) {
const { target = '_blank', variant = 'success', badgeText, href, children } = props;
return (
<p className='mb-0'>
<a href={href} target={target}>
<span style={{ position: 'relative', top: '-2px' }}
className={`badge badge-${variant}`}>{badgeText}</span> {children}
</a>
</p>
);
}

View File

@ -0,0 +1,26 @@
import styled from 'styled-components';
const BlockQuote = styled.blockquote`
padding: 16px 20px;
position: relative;
background: #e8e8e8;
border-radius: 5px;
h1, h2, h3, h4, h5, h6 {
margin-top: 0;
}
p + h4 {
margin-top: 15px;
}
p {
margin: 0;
& + p {
margin-top: 10px;
}
}
`;
export default BlockQuote;

View File

@ -0,0 +1,80 @@
import React from 'react';
import styled from 'styled-components';
import LinkIcon from 'components/icons/link.svg';
const linkify = (Component: React.FunctionComponent<any>) => {
return function EnrichedHeading(props: { children: string }): React.ReactNode {
const text = props.children;
const id = text.toLowerCase && text
.toLowerCase()
.replace(/[^\x00-\x7F]/g, '')
.replace(/\s+/g, '-')
.replace(/[?!]/g, '');
return (
<Component id={id}>
<HeaderLink href={`#${id}`}>
<LinkIcon />
</HeaderLink>
{props.children}
</Component>
);
};
};
const HeaderLink = styled.a`
position: absolute;
top: 0;
left: -25px;
width: 25px;
display: none;
height: 100%;
align-items: center;
justify-content: flex-start;
`;
const H1 = styled.h1`
position: relative;
font-size: 42px;
font-weight: 700;
margin: 32px 0 10px !important;
&:hover ${HeaderLink} {
display: flex;
}
`;
const H2 = styled(H1).attrs({ as: 'h2' })`
font-size: 32px;
`;
const H3 = styled(H1).attrs({ as: 'h3' })`
margin: 22px 0 8px;
font-size: 30px;
`;
const H4 = styled(H1).attrs({ as: 'h4' })`
margin: 18px 0 8px;
font-size: 24px;
`;
const H5 = styled(H1).attrs({ as: 'h5' })`
margin: 14px 0 8px;
font-size: 18px;
`;
const H6 = styled(H1).attrs({ as: 'h6' })`
margin: 12px 0 8px;
font-size: 18px;
`;
const Headings = {
h1: linkify(H1),
h2: linkify(H2),
h3: linkify(H3),
h4: linkify(H4),
h5: linkify(H5),
h6: linkify(H6)
};
export default Headings;

View File

@ -0,0 +1,8 @@
import styled from 'styled-components';
export const IFrame = styled.iframe`
display: block;
width: 100%;
border: none;
margin: 30px auto;
`;

View File

@ -0,0 +1,7 @@
import styled from 'styled-components';
export const Img = styled.img`
max-width: 100%;
margin: 25px auto;
display: block;
`;

View File

@ -0,0 +1,23 @@
import P from './p';
import Headings from './heading';
import { Pre } from './pre';
import BlockQuote from './blockquote';
import { Table } from './table';
import { IFrame } from './iframe';
import { Img } from './img';
import EnrichedLink from './a';
import { BadgeLink } from './badge-link';
const MdxComponents = {
p: P,
...Headings,
pre: Pre,
blockquote: BlockQuote,
a: EnrichedLink,
table: Table,
iframe: IFrame,
img: Img,
BadgeLink: BadgeLink
};
export default MdxComponents;

View File

@ -0,0 +1,10 @@
import React from 'react';
import { Text } from '@chakra-ui/react';
type EnrichedTextType = {
children: React.ReactNode;
}
export default function EnrichedText(props: EnrichedTextType) {
return <Text lineHeight='27px' color='black' mb='18px'>{props.children}</Text>;
}

View File

@ -0,0 +1,12 @@
import styled from 'styled-components';
export const Pre = styled.pre`
margin: 25px -25px 25px -25px !important;
padding: 20px 25px !important;
border-radius: 10px;
line-height: 1.5 !important;
code {
background: transparent;
}
`;

View File

@ -0,0 +1,25 @@
import styled from 'styled-components';
export const Table = styled.table`
border-collapse: separate;
width: 100%;
border-spacing: 0;
margin: 20px 0;
th {
color: #666;
font-size: 12px;
font-weight: 400;
background: #FAFAFA;
text-transform: uppercase;
height: 40px;
vertical-align: middle;
padding: 5px 10px;
}
td {
font-size: 14px;
padding: 10px;
border-bottom: 1px solid #EAEAEA;
}
`;