Skip to content
This repository was archived by the owner on Mar 4, 2020. It is now read-only.

Commit 4052e02

Browse files
committed
docs(prettier): add prettier support
1 parent 5d20026 commit 4052e02

29 files changed

+755
-907
lines changed

.prettierrc.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"$schema": "http://json.schemastore.org/prettierrc",
3+
"htmlWhitespaceSensitivity": "ignore",
34
"printWidth": 100,
45
"tabWidth": 2,
56
"semi": false,

docs/src/components/CodeSnippet.tsx

Lines changed: 50 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,66 @@
1+
import * as _ from 'lodash'
12
import * as React from 'react'
3+
4+
import formatCode from '../utils/formatCode'
25
import Editor, { EDITOR_BACKGROUND_COLOR } from './Editor'
36

47
export interface CodeSnippetProps {
8+
fitted?: boolean
59
label?: string
610
mode?: 'jsx' | 'html' | 'sh'
711
value: string
812
style?: React.CSSProperties
913
}
1014

11-
const CodeSnippet = ({ label, value, mode = 'jsx', style, ...rest }: CodeSnippetProps) => (
12-
<div
13-
style={{
14-
position: 'relative',
15-
padding: '1rem',
16-
marginBottom: '2rem',
17-
background: EDITOR_BACKGROUND_COLOR,
18-
...style,
19-
}}
20-
>
15+
const formatters = {
16+
sh: (val: string = ''): string => val.replace(/^/g, '$ '),
17+
html: (val: string = ''): string => formatCode(val, 'html'),
18+
jsx: (val: string = ''): string => formatCode(val, 'babylon'),
19+
}
20+
21+
const CodeSnippet = ({ fitted, label, value, mode = 'jsx', ...rest }: CodeSnippetProps) => {
22+
const format = formatters[mode]
23+
const formattedValue = format(value)
24+
// remove eof line break, they are not helpful for snippets
25+
.replace(/\n$/, '')
26+
27+
return (
2128
<div
2229
style={{
23-
position: 'absolute',
24-
padding: '0.2rem 0.35rem',
25-
top: '1rem',
26-
right: '1rem',
27-
lineHeight: 1,
28-
color: '#899',
29-
fontFamily: 'monospace',
30-
fontSize: '0.8rem',
31-
border: '1px solid #566',
32-
zIndex: 100,
30+
position: 'relative',
31+
padding: '1rem',
32+
marginBottom: fitted ? 0 : '2rem',
33+
background: EDITOR_BACKGROUND_COLOR,
34+
...rest.style,
3335
}}
3436
>
35-
{label || mode}
37+
<div
38+
style={{
39+
position: 'absolute',
40+
padding: '0.2rem 0.35rem',
41+
top: '1rem',
42+
right: '1rem',
43+
lineHeight: 1,
44+
color: '#899',
45+
fontFamily: 'monospace',
46+
fontSize: '0.8rem',
47+
border: '1px solid #566',
48+
zIndex: 100,
49+
}}
50+
>
51+
{label || mode}
52+
</div>
53+
<Editor
54+
highlightActiveLine={false}
55+
highlightGutterLine={false}
56+
mode={mode}
57+
readOnly
58+
showGutter={false}
59+
showCursor={false}
60+
value={formattedValue}
61+
{...rest}
62+
/>
3663
</div>
37-
<Editor
38-
id={btoa(value)}
39-
highlightActiveLine={false}
40-
highlightGutterLine={false}
41-
mode={mode}
42-
readOnly
43-
showGutter={false}
44-
showCursor={false}
45-
value={mode === 'sh' ? value.replace(/^/g, '$ ') : value}
46-
{...rest}
47-
/>
48-
</div>
49-
)
64+
)
65+
}
5066
export default CodeSnippet

0 commit comments

Comments
 (0)