footer
`; 27 | 28 | const result = parseHTML(html); 29 | 30 | expect(result.length).toBe(3); 31 | expect(result[0].type).toBe('html'); 32 | expect(result[1].type).toBe('code'); 33 | expect(result[2].type).toBe('html'); 34 | expect(trim(result[0].content)).toContain('footer
'); 37 | }); 38 | 39 | it('Parse into two pieces of code and three pieces of html', () => { 40 | const html = `footer
`; 49 | 50 | const result = parseHTML(html); 51 | 52 | expect(result.length).toBe(5); 53 | expect(result[0].type).toBe('html'); 54 | expect(result[1].type).toBe('code'); 55 | expect(result[2].type).toBe('html'); 56 | expect(result[3].type).toBe('code'); 57 | expect(result[4].type).toBe('html'); 58 | 59 | expect(trim(result[0].content)).toBe('footer
'); 64 | }); 65 | -------------------------------------------------------------------------------- /babel.config.js: -------------------------------------------------------------------------------- 1 | module.exports = (api, options) => { 2 | const { NODE_ENV } = options || process.env; 3 | const dev = NODE_ENV === 'development'; 4 | const modules = NODE_ENV === 'esm' ? false : 'commonjs'; 5 | 6 | if (api) { 7 | api.cache(() => NODE_ENV); 8 | } 9 | 10 | return { 11 | presets: [ 12 | ['@babel/preset-env', { modules, loose: true }], 13 | ['@babel/preset-react', { development: dev, runtime: 'automatic' }], 14 | '@babel/typescript' 15 | ], 16 | plugins: [ 17 | ['@babel/plugin-proposal-class-properties', { loose: true }], 18 | '@babel/plugin-proposal-optional-chaining', 19 | '@babel/plugin-proposal-export-namespace-from', 20 | '@babel/plugin-proposal-export-default-from', 21 | ['@babel/plugin-transform-runtime', { useESModules: !modules }] 22 | ] 23 | }; 24 | }; 25 | -------------------------------------------------------------------------------- /docs/example.md: -------------------------------------------------------------------------------- 1 | # React Code View 2 | 3 | --- 4 | 5 | **React Code View** can render source code in markdown documents. And brings you the ability to render React components with editable source code and live preview. 6 | 7 | ## Install 8 | 9 | ```bash 10 | npm install react-code-view 11 | ``` 12 | 13 | ## Usage 14 | 15 | ```jsx 16 | import CodeView from 'react-code-view'; 17 | import { Button } from 'rsuite'; 18 | 19 | import 'react-code-view/styles/react-code-view.css'; 20 | 21 | return{errorMessage}; 19 | } 20 | return children; 21 | } 22 | } 23 | 24 | export default ErrorBoundary; 25 | -------------------------------------------------------------------------------- /src/MarkdownRenderer.tsx: -------------------------------------------------------------------------------- 1 | import React, { useEffect } from 'react'; 2 | import classNames from 'classnames'; 3 | import copy from 'copy-to-clipboard'; 4 | import mergeRefs from './utils/mergeRefs'; 5 | import { iconPath as copyPath, svgTpl } from './icons/Copy'; 6 | import { iconPath as checkPath } from './icons/Check'; 7 | 8 | interface MarkdownRendererProps extends React.HTMLAttributes
${value}