Skip to content

simonguo/react-code-view

Repository files navigation

React Code View

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.

React Code View

Install

npm install react-code-view 

Configure Webpack

// webpack.config.jsexportdefault{module: {rules: [{test: /\.md$/,loader: 'react-code-view/webpack-md-loader'}]}};

Options

{"parseLanguages": [// Supported languages for highlight.js// default: "javascript", "bash", "xml", "css", "markdown", "less", "typescript"// See https://github.com/highlightjs/highlight.js/blob/main/SUPPORTED_LANGUAGES.md],"htmlOptions": {// HTML Loader options// See https://github.com/webpack-contrib/html-loader#options},"markedOptions": {// Pass options to marked// See https://marked.js.org/using_advanced#options}}

webpack.config.js

exportdefault{module: {rules: [{test: /\.md$/,use:[loader: 'react-code-view/webpack-md-loader',options:{parseLanguages: ['typescript','rust']}]}]}};

Usage

importCodeViewfrom'react-code-view';import{Button}from'rsuite';import'react-code-view/styles/react-code-view.css';return(<CodeViewdependencies={{ Button }}>{require('./example.md')}</CodeView>);

The source code is written in markdown, refer to example.md

Note: The code to be rendered must be placed between <!--start-code--> and <!--end-code-->

Props

<CodeView>

NameTypeDefault valueDescription
afterCompile(code: string) => stringExecuted after compiling the code
beforeCompile(code: string) => stringExecuted before compiling the code
childrenanyThe code to be rendered is executed. Usually imported via markdown-loader
compileOptionsobjectdefaultTransformOptionshttps://github.com/alangpierce/sucrase#transforms
dependenciesobjectDependent objects required by the executed code
editablebooleanfalseRenders a code editor that can modify the source code
editorobjectEditor properties
onChange(code?: string) => voidCallback triggered after code change
onCloseEditor() => voidCallback triggered when the editor is closed
onOpenEditor() => voidCallback triggered when the editor is opened
renderExtraFooter() => ReactNodeCustomize the rendering footer
renderToolbar(buttons: ReactNode) => ReactNodeCustomize the rendering toolbar
sourceCodestringThe code to be rendered is executed
theme'light' , 'dark''light'Code editor theme, applied to CodeMirror

About

A flexible playground for rendering Markdown and live editing React components.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •