2.4.0 • Published 24 days ago

react-code-view v2.4.0

Weekly downloads
90
License
MIT
Repository
github
Last release
24 days ago

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.js
export default {
  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

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

Usage

import CodeView from 'react-code-view';
import { Button } from 'rsuite';

import 'react-code-view/styles/react-code-view.css';

return (
  <CodeView
    dependencies={{
      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
2.4.0

24 days ago

2.3.0

4 months ago

2.3.1

4 months ago

2.2.1

2 years ago

2.2.0

2 years ago

2.0.0

2 years ago

2.0.0-beta.11

2 years ago

2.0.0-beta.10

2 years ago

2.0.0-beta.15

2 years ago

2.0.0-beta.13

2 years ago

2.0.0-beta.12

2 years ago

2.0.0-beta.17

2 years ago

2.0.0-beta.16

2 years ago

2.0.0-beta.8

2 years ago

2.0.0-beta.7

2 years ago

2.0.0-beta.2

2 years ago

2.0.0-beta.1

2 years ago

2.0.0-beta.6

2 years ago

2.0.0-beta.5

2 years ago

2.1.0

2 years ago

2.0.0-beta.4

2 years ago

2.0.0-beta.3

2 years ago

1.3.0-alpha.1

3 years ago

1.2.6

3 years ago

1.2.5

4 years ago

1.2.5-beta

4 years ago

1.2.4

4 years ago

1.2.4-beta.1

4 years ago

1.2.4-beta

4 years ago

1.2.3

4 years ago

1.2.2

4 years ago

1.2.0

5 years ago

1.1.13

6 years ago

1.1.12

6 years ago

1.1.11

6 years ago

1.1.10

6 years ago

1.1.9

6 years ago

1.1.8

6 years ago

1.1.7

6 years ago

1.1.5

6 years ago

1.1.4

6 years ago

1.1.3

6 years ago

1.1.2

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.14

6 years ago

1.0.13

6 years ago

1.0.12

6 years ago

1.0.11

6 years ago

1.0.10

6 years ago

1.0.9

6 years ago

1.0.7

6 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago