|
1 | | -import { CodeSnippet as StardustCodeSnippet } from '@stardust-ui/docs-components' |
| 1 | +import _ from 'lodash' |
| 2 | +import * as Prism from 'prismjs/components/prism-core' |
| 3 | +import prettier from 'prettier/standalone' |
| 4 | +import babel from 'prettier/parser-babel' |
| 5 | +import html from 'prettier/parser-html' |
| 6 | +import PropTypes from 'prop-types' |
2 | 7 | import React from 'react' |
3 | 8 |
|
4 | | -import NoSSR from '../NoSSR' |
| 9 | +// Order of PrismJS imports there is sensitive |
| 10 | +import 'prismjs/components/prism-clike' |
| 11 | +import 'prismjs/components/prism-json' |
| 12 | +import 'prismjs/components/prism-markup' |
| 13 | +import 'prismjs/components/prism-bash' |
| 14 | +import 'prismjs/components/prism-javascript' |
| 15 | +import 'prismjs/components/prism-jsx' |
5 | 16 |
|
6 | | -const CodeSnippet = (props) => ( |
7 | | - <NoSSR> |
8 | | - <StardustCodeSnippet {...props} /> |
9 | | - </NoSSR> |
10 | | -) |
| 17 | +import CodeSnippetLabel from './CodeSnippetLabel' |
| 18 | + |
| 19 | +const prettierConfig = { |
| 20 | + htmlWhitespaceSensitivity: 'ignore', |
| 21 | + printWidth: 100, |
| 22 | + tabWidth: 2, |
| 23 | + semi: false, |
| 24 | + singleQuote: true, |
| 25 | + trailingComma: 'all', |
| 26 | + plugins: { |
| 27 | + babel, |
| 28 | + html, |
| 29 | + }, |
| 30 | +} |
| 31 | + |
| 32 | +const normalizeToString = (value) => { |
| 33 | + if (Array.isArray(value)) { |
| 34 | + return value.join('\n') |
| 35 | + } |
| 36 | + |
| 37 | + return _.isObject(value) ? JSON.stringify(value, null, 2) : value |
| 38 | +} |
| 39 | + |
| 40 | +export const prettifyCode = (code, parser) => { |
| 41 | + const formatted = prettier.format(code, { |
| 42 | + ...prettierConfig, |
| 43 | + // a narrower print width is more friendly to doc examples |
| 44 | + parser, |
| 45 | + }) |
| 46 | + |
| 47 | + return formatted.replace(/^;</m, '<') // remove beginning semi in JSX/HTML |
| 48 | +} |
| 49 | + |
| 50 | +const formatters = { |
| 51 | + bash: (val = '') => val.replace(/^/g, '$ '), |
| 52 | + json: (val) => prettifyCode(val, 'json'), |
| 53 | + js: (val = '') => prettifyCode(val, 'babel'), |
| 54 | + jsx: (val = '') => prettifyCode(val, 'babel'), |
| 55 | + html: (val = '') => prettifyCode(val, 'html'), |
| 56 | +} |
| 57 | + |
| 58 | +export const formatCode = (code, mode) => { |
| 59 | + if (!code) return '' |
| 60 | + const formatter = formatters[mode] |
| 61 | + |
| 62 | + return ( |
| 63 | + formatter(normalizeToString(code)) |
| 64 | + // remove eof line break, they are not helpful for snippets |
| 65 | + .replace(/\n$/, '') |
| 66 | + ) |
| 67 | +} |
| 68 | + |
| 69 | +const CodeSnippet = React.memo((props) => { |
| 70 | + const { |
| 71 | + className, |
| 72 | + copyable = true, |
| 73 | + fitted, |
| 74 | + formattable = true, |
| 75 | + label, |
| 76 | + mode = 'jsx', |
| 77 | + value, |
| 78 | + } = props |
| 79 | + |
| 80 | + const codeClassName = `language-${mode}` |
| 81 | + const code = formattable ? formatCode(value, mode) : value |
| 82 | + const codeRef = React.useRef(null) |
| 83 | + |
| 84 | + React.useLayoutEffect(() => { |
| 85 | + Prism.highlightElement(codeRef.current) |
| 86 | + }) |
| 87 | + |
| 88 | + return ( |
| 89 | + <div |
| 90 | + className={className} |
| 91 | + style={{ |
| 92 | + fontSize: '12px', |
| 93 | + position: 'relative', |
| 94 | + ...props.style, |
| 95 | + }} |
| 96 | + > |
| 97 | + <CodeSnippetLabel copyable={copyable} label={label} mode={mode} value={code} /> |
| 98 | + |
| 99 | + <pre style={{ margin: fitted ? '0' : undefined }}> |
| 100 | + <code className={codeClassName} ref={codeRef}> |
| 101 | + {code} |
| 102 | + </code> |
| 103 | + </pre> |
| 104 | + </div> |
| 105 | + ) |
| 106 | +}) |
| 107 | + |
| 108 | +CodeSnippet.propTypes = { |
| 109 | + className: PropTypes.string, |
| 110 | + copyable: PropTypes.bool, |
| 111 | + fitted: PropTypes.bool, |
| 112 | + formattable: PropTypes.bool, |
| 113 | + label: PropTypes.oneOfType([PropTypes.element, PropTypes.bool]), |
| 114 | + mode: PropTypes.oneOf(['bash', 'json', 'js', 'jsx', 'html']), |
| 115 | + style: PropTypes.object, |
| 116 | + value: PropTypes.oneOfType([PropTypes.object, PropTypes.string]), |
| 117 | +} |
11 | 118 |
|
12 | 119 | export default CodeSnippet |
0 commit comments