-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMarkdownView.tsx
More file actions
105 lines (96 loc) · 2.42 KB
/
Copy pathMarkdownView.tsx
File metadata and controls
105 lines (96 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import React, {HTMLAttributes} from 'react'
import ReactMarkdown from 'react-markdown'
import {SpecialComponents} from 'react-markdown/lib/ast-to-react'
import {NormalComponents} from 'react-markdown/lib/complex-types'
import styled from 'styled-components'
import SyntaxHighlighter from '../SyntaxHighlighter'
import Heading from './Heading'
import Text from './Text'
type Props = {
children: string
className?: string
customComponents?:
| Partial<
Omit<NormalComponents, keyof SpecialComponents> & SpecialComponents
>
| undefined
} & HTMLAttributes<HTMLElement>
const MarkdownView = ({
children,
customComponents,
className,
...props
}: Props) => {
return (
<StyledMarkdown
className={className}
children={children}
linkTarget="_blank"
components={{
...{
h1: ({children}) => (
<StyledHeading
variant="h1"
normalWeight
children={children}
marginBottomPx={12}
/>
),
h2: ({children}) => (
<StyledHeading
variant="h2"
normalWeight
children={children}
marginBottomPx={8}
/>
),
h3: ({children}) => (
<StyledHeading
variant="h3"
normalWeight
children={children}
marginBottomPx={4}
/>
),
p: ({children}) => <Text children={children} />,
code: ({className, children}) => (
<SyntaxHighlighter
language={className?.split('-')[1] || ''}
children={children[0] as string}
customStyle={{background: 'rgba(0,0,0,0)', border: 'none'}}
/>
),
pre: ({children}) => <Pre children={children} />,
},
...customComponents,
}}
{...props}
/>
)
}
const Pre = styled.pre`
margin: 0;
padding: 0;
max-width: 100%;
`
const StyledHeading = styled(Heading)<{marginBottomPx?: number}>`
margin-bottom: ${(props) =>
props.marginBottomPx && `${props.marginBottomPx}px`};
`
const StyledMarkdown = styled(ReactMarkdown)`
display: flex;
flex-direction: column;
gap: 12px;
ul {
margin: 0;
list-style-type: '- ';
li {
margin-bottom: 8px;
font-size: 16px;
:last-child {
margin-bottom: 0;
}
}
}
`
export default MarkdownView