百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
R

react-code-view

> 前端框架
开源

这是一个功能强大、灵活的代码编辑器和语法高亮器,专用于 React 应用程序。它使用了现代工具,如 CodeMirror 6 和 Shiki。

200 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

这是一个功能强大、灵活的代码编辑器和语法高亮器,专用于 React 应用程序。它使用了现代工具,如 CodeMirror 6 和 Shiki。

React Code View

A React component library for rendering code with live preview and syntax highlighting.

✨ Highlight: Import .md files as React components - write markdown, get interactive demos instantly!

Docs

✨ Features

  • Native Markdown Parsing - Import .md files and render embedded code blocks as interactive components
  • Live Preview - Execute and preview React code in real-time
  • ✏️ Editable Code - Built-in code editor with syntax highlighting
  • Universal Plugin - Works with Webpack, Vite, Rollup, esbuild, and Rspack
  • TypeScript - Full TypeScript support out of the box
  • Tree-shakeable - Import only what you need
  • ⚡ Zero Config - Works out of the box with sensible defaults

✅ Requirements

  • Node.js >= 18
  • PNPM >= 8 (monorepo managed via PNPM + Turbo)

Installation

bash
# npm
npm install @react-code-view/react @react-code-view/unplugin

# pnpm
pnpm add @react-code-view/react @react-code-view/unplugin

# yarn
yarn add @react-code-view/react @react-code-view/unplugin

Note: @react-code-view/unplugin is needed if you want to import .md files directly as React components. For basic CodeView usage without markdown imports, you only need @react-code-view/react.

Quick Start

⭐ Import Markdown as React Components

The most convenient way - configure once, use everywhere!

1. Configure your build tool (Vite example):

javascript
// vite.config.js
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import reactCodeView from '@react-code-view/unplugin/vite';

export default defineConfig({
  plugins: [
    react(),
    reactCodeView() // That's it!
  ]
});

2. Create your markdown file (demo.md):

markdown
# Interactive Counter

Here's a live counter component:

\`\`\`jsx
function Counter() {
  const [count, setCount] = useState(0);
  return (
     setCount(count + 1)}>
      Clicked {count} times
    
  );
}
render();
\`\`\`

The code above is **fully interactive**!

3. Import and use like any React component:

typescript
import Demo from './demo.md';

function App() {
  return ;
}

That's it! Your markdown is now a React component with:

  • ✅ Live, interactive code blocks
  • ✅ Automatic syntax highlighting
  • ✅ Type-safe imports
  • ✅ Full TypeScript support

Alternative: Runtime Parsing (No Build Config)

If you prefer not to configure a build tool:

typescript
import { CodeView } from '@react-code-view/react';
import markdown from './demo.md?raw';

  {markdown}

Basic Code Preview

For simple code snippets without markdown:

typescript
import { CodeView } from '@react-code-view/react';

const code = `
 alert('Hello!')}>
  Click me

`;

  {code}

Packages

This monorepo contains the following packages:

Package Version Description
@react-code-view/react React components
@react-code-view/core Core transformation utilities
@react-code-view/unplugin Build tool plugins

Build Tool Integration

React Code View supports all major build tools through unplugin.

Once configured, you can import .md files as React components - the most convenient way to create interactive documentation!

Why this is amazing:

  • Write markdown files with code examples
  • Import them like regular React components
  • ⚡ Get live, interactive demos automatically
  • Full TypeScript support and type safety
  • Pass props like theme, dependencies, etc.

Example:

typescript
import Demo from './example.md';

function App() {
  return (
    

  );
}

Vite

javascript
// vite.config.js
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import reactCodeView from '@react-code-view/unplugin/vite';

export default defineConfig({
  plugins: [
    react(),
    reactCodeView()
  ]
});

Webpack

javascript
// webpack.config.js
const ReactCodeViewPlugin = require('@react-code-view/unplugin/webpack');

module.exports = {
  plugins: [
    ReactCodeViewPlugin()
  ]
};

Rollup

javascript
// rollup.config.js
import reactCodeView from '@react-code-view/unplugin/rollup';

export default {
  plugins: [
    reactCodeView()
  ]
};

esbuild

javascript
import * as esbuild from 'esbuild';
import reactCodeView from '@react-code-view/unplugin/esbuild';

await esbuild.build({
  plugins: [
    reactCodeView()
  ]
});

Rspack

javascript
// rspack.config.js
const ReactCodeViewPlugin = require('@react-code-view/unplugin/rspack');

module.exports = {
  plugins: [
    ReactCodeViewPlugin()
  ]
};

API Reference

CodeView Props

Prop Type Default Description
children string - Source code or markdown content to display
dependencies object {} Dependencies for code execution (e.g., { useState: React.useState })
language string 'jsx' Syntax highlighting language
editable boolean true Enable code editing
renderPreview boolean true Show live preview
showLineNumbers boolean true Show line numbers
showCopyButton boolean true Show copy button
defaultShowCode boolean false Initially show code section
theme string 'rcv-theme-default' Theme class name
beforeCompile function - Transform code before compile
afterCompile function - Transform code after compile
onChange function - Callback when code changes
onError function - Callback when error occurs
emptyPreviewContent ReactNode - Content to display when preview is empty

Note: When children contains markdown with `` markers, CodeView automatically parses and renders code blocks as interactive components.

Other Components

  • Renderer - Syntax-highlighted code display
  • MarkdownRenderer - Render markdown with syntax highlighting
  • CodeEditor - Editable code component
  • Preview - Display executed code output
  • CopyCodeButton - Copy code to clipboard button
  • ErrorBoundary - Error boundary for code execution

Hooks

  • useCodeExecution - Execute code and capture a rendered element

    Example:

    typescript
    import { useCodeExecution } from '@react-code-view/react';
    
    export function LivePreview({ source }: { source: string }) {
      const { element, error, code, updateCode } = useCodeExecution(source, {
        // Optional: inject deps into runtime scope
        dependencies: { alert },
        // Optional: configure transforms (e.g. TS + JSX)
        transformOptions: { transforms: ['typescript', 'jsx'] },
        beforeCompile: (c) => c.trim(),
        afterCompile: (c) => c,
        onError: (e) => console.error('Execution error:', e)
      });
    
      return (

: element}

);

}


##  Theming

Import the base styles:

```tsx
import 'react-code-view/styles';

Use theme classes:

typescript
// Light theme (default)
...

// Dark theme
...

Customize with CSS variables:

css
.rcv-code-view {
  --rcv-color-bg: #ffffff;
  --rcv-color-bg-code: #f6f8fa;
  --rcv-color-text: #24292f;
  --rcv-color-border: #d0d7de;
  --rcv-color-primary: #0969da;
  --rcv-color-error: #cf222e;
}

️ Development

…

Changelog

See CHANGELOG.md for release history.

Contributing

Contributions are welcome! Please read our Contributing Guide for details.

License

MIT © Simon Guo

Issues· 3 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

TypeScriptcodeeditorcodemirrormarkdownmarkdown-loader

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类前端框架
定价开源

> 相关工具

R
React
用于构建用户界面的 JavaScript 库
V
Vue.js
渐进式 JavaScript 框架
N
Next.js
基于 React 的全栈 Web 框架