|
1 |
| -import Highlight, { defaultProps, Language, PrismTheme } from 'prism-react-renderer' |
2 |
| -import { HTMLAttributes } from 'react' |
| 1 | +import { HTMLAttributes, ReactNode } from 'react' |
3 | 2 |
|
4 |
| -import { BorderRadius, FontFamily, Spacing } from '@edgeandnode/gds' |
| 3 | +import { Code, Spacing } from '@edgeandnode/gds' |
5 | 4 |
|
6 |
| -export type CodeBlockProps = HTMLAttributes<HTMLPreElement> |
| 5 | +export type CodeBlockProps = { |
| 6 | + children?: |
| 7 | + | ReactNode |
| 8 | + | { |
| 9 | + props: { |
| 10 | + children: string |
| 11 | + className?: string |
| 12 | + } |
| 13 | + } |
| 14 | +} & Omit<HTMLAttributes<HTMLPreElement>, 'children'> |
7 | 15 | export type CodeInlineProps = HTMLAttributes<HTMLElement>
|
8 | 16 |
|
9 |
| -import theme from 'prism-react-renderer/themes/duotoneDark' |
10 |
| - |
11 | 17 | export const CodeBlock = ({ children, ...props }: CodeBlockProps) => {
|
12 |
| - const data = ( |
13 |
| - children as { |
14 |
| - props: { |
15 |
| - children: string |
16 |
| - className?: string |
17 |
| - } |
18 |
| - } |
19 |
| - ).props |
20 |
| - const code = data.children.trim() |
21 |
| - let language = data.className?.substring('language-'.length) |
| 18 | + const code = |
| 19 | + children && typeof children === 'object' && 'props' in children |
| 20 | + ? children.props.children.trim() |
| 21 | + : (children as string) |
| 22 | + let language = |
| 23 | + children && typeof children === 'object' && 'props' in children |
| 24 | + ? children.props.className?.substring('language-'.length) |
| 25 | + : null |
22 | 26 |
|
23 | 27 | if (!language || language === 'sh') {
|
24 | 28 | language = 'bash'
|
25 | 29 | }
|
26 | 30 |
|
27 | 31 | return (
|
28 |
| - <div sx={{ my: Spacing['24px'] }}> |
29 |
| - <Highlight {...defaultProps} code={code} language={language as Language} theme={theme}> |
30 |
| - {({ className, tokens, getLineProps, getTokenProps }) => ( |
31 |
| - <pre |
32 |
| - className={className} |
33 |
| - sx={{ |
34 |
| - overflowX: 'auto', |
35 |
| - p: Spacing['16px'], |
36 |
| - borderRadius: BorderRadius.M, |
37 |
| - border: 'White4', |
38 |
| - bg: 'White4', |
39 |
| - fontFamily: FontFamily.MONOSPACE, |
40 |
| - fontSize: '16px', |
41 |
| - lineHeight: '24px', |
42 |
| - }} |
43 |
| - {...props} |
44 |
| - > |
45 |
| - {tokens.map((line, i) => ( |
46 |
| - // eslint-disable-next-line react/jsx-key |
47 |
| - <div {...getLineProps({ line, key: i })}> |
48 |
| - {line.map((token, key) => ( |
49 |
| - // eslint-disable-next-line react/jsx-key |
50 |
| - <span {...getTokenProps({ token, key })} /> |
51 |
| - ))} |
52 |
| - </div> |
53 |
| - ))} |
54 |
| - </pre> |
55 |
| - )} |
56 |
| - </Highlight> |
57 |
| - </div> |
58 |
| - ) |
59 |
| -} |
60 |
| - |
61 |
| -export const CodeInline = ({ children, ...props }: CodeInlineProps) => { |
62 |
| - return ( |
63 |
| - <code |
| 32 | + <Code.Block |
| 33 | + language={language} |
64 | 34 | sx={{
|
65 |
| - px: Spacing['4px'], |
66 |
| - py: Spacing['2px'], |
67 |
| - borderRadius: BorderRadius.S, |
68 |
| - border: 'White4', |
69 |
| - bg: 'White4', |
70 |
| - fontFamily: FontFamily.MONOSPACE, |
71 |
| - fontSize: '0.75em', |
| 35 | + my: Spacing['24px'], |
| 36 | + '&:nth-child(1 of :not(style))': { mt: 0 }, |
| 37 | + '&:nth-last-child(1 of :not(style))': { mb: 0 }, |
72 | 38 | }}
|
73 | 39 | {...props}
|
74 | 40 | >
|
75 |
| - {children} |
76 |
| - </code> |
| 41 | + {code} |
| 42 | + </Code.Block> |
77 | 43 | )
|
78 | 44 | }
|
| 45 | + |
| 46 | +export const CodeInline = ({ children, ...props }: CodeInlineProps) => { |
| 47 | + return <Code.Inline {...props}>{children as string}</Code.Inline> |
| 48 | +} |
0 commit comments