Skip to content

Commit f8f9cab

Browse files
authored
feat: add native markdown parsing support with parseHTML (#61)
* feat: add native markdown parsing support with parseHTML - Add parseHTML utility to natively parse markdown with code blocks - CodeView now automatically detects and renders markdown content - Split CodeView into CodeViewInternal and wrapper for proper hooks usage - Add useNativeParser option to unplugin (default: true) - Generate CodeView components in unplugin native parser mode - Add vite example with both runtime and build-time parsing demos - Update README with markdown parsing examples and documentation - Add MarkdownExample to docs site - Create demo.md and unplugin-demo.md examples - Simplify unplugin documentation to focus on default native parser mode Benefits: - Zero config markdown parsing at runtime - Consistent behavior between runtime and build-time - Smaller bundle size (no pre-rendered HTML) - Type-safe imports with unplugin - Backward compatible with existing code * feat: simplify example configs and add two-page structure - Remove alias configurations from all examples (Vite, Webpack, Rollup, esbuild, Next.js) - Rely on pnpm workspace for automatic local package linking - Fix ESM/CJS compatibility issues: - Use dynamic import() for shiki in @react-code-view/core - Add explicit CJS exports in @react-code-view/unplugin webpack plugin - Fix package.json exports to correctly point to .mjs files for ESM - Add TypeScript declarations for .md imports in unplugin package - Refactor all examples with two-page structure: - Unplugin Demo page: showcases direct markdown import feature - Basic Usage page: demonstrates traditional CodeView usage - Update documentation to highlight unplugin as primary usage method - Remove InstallationPage from docs (consolidated into QuickStart) * fix: remove debug images
1 parent 3d22cd6 commit f8f9cab

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+3819
-1618
lines changed

README.md

Lines changed: 116 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,19 @@
66

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

9+
**✨ Highlight:** Import `.md` files as React components - write markdown, get interactive demos instantly!
10+
911
[Docs](https://react-code-view-rsuite.vercel.app/)
1012

1113
## ✨ Features
1214

15+
- 📝 **Native Markdown Parsing** - Import `.md` files and render embedded code blocks as interactive components
1316
- 🎨 **Live Preview** - Execute and preview React code in real-time
1417
- ✏️ **Editable Code** - Built-in code editor with syntax highlighting
15-
- 📝 **Markdown Support** - Render markdown content with code blocks
1618
- 🔌 **Universal Plugin** - Works with Webpack, Vite, Rollup, esbuild, and Rspack
1719
- 🎯 **TypeScript** - Full TypeScript support out of the box
1820
- 📦 **Tree-shakeable** - Import only what you need
21+
-**Zero Config** - Works out of the box with sensible defaults
1922

2023
## ✅ Requirements
2124

@@ -37,27 +40,95 @@ yarn add react-code-view
3740

3841
## 🚀 Quick Start
3942

43+
### ⭐ Import Markdown as React Components
44+
45+
The most convenient way - configure once, use everywhere!
46+
47+
**1. Configure your build tool** (Vite example):
48+
49+
```js
50+
// vite.config.js
51+
import { defineConfig } from 'vite';
52+
import react from '@vitejs/plugin-react';
53+
import reactCodeView from '@react-code-view/unplugin/vite';
54+
55+
export default defineConfig({
56+
plugins: [
57+
react(),
58+
reactCodeView() // That's it!
59+
]
60+
});
61+
```
62+
63+
**2. Create your markdown file** (`demo.md`):
64+
65+
```markdown
66+
# Interactive Counter
67+
68+
Here's a live counter component:
69+
70+
<!--start-code-->
71+
\`\`\`jsx
72+
function Counter() {
73+
const [count, setCount] = useState(0);
74+
return (
75+
<button onClick={() => setCount(count + 1)}>
76+
Clicked {count} times
77+
</button>
78+
);
79+
}
80+
render(<Counter />);
81+
\`\`\`
82+
<!--end-code-->
83+
84+
The code above is **fully interactive**!
85+
```
86+
87+
**3. Import and use like any React component**:
88+
4089
```tsx
41-
import CodeView from 'react-code-view';
42-
import 'react-code-view/styles';
90+
import Demo from './demo.md';
4391

4492
function App() {
45-
const code = `
93+
return <Demo />;
94+
}
95+
```
96+
97+
**That's it!** 🎉 Your markdown is now a React component with:
98+
- ✅ Live, interactive code blocks
99+
- ✅ Automatic syntax highlighting
100+
- ✅ Type-safe imports
101+
- ✅ Full TypeScript support
102+
103+
### Alternative: Runtime Parsing (No Build Config)
104+
105+
If you prefer not to configure a build tool:
106+
107+
```tsx
108+
import CodeView from 'react-code-view';
109+
import markdown from './demo.md?raw';
110+
111+
<CodeView dependencies={{ useState: React.useState }}>
112+
{markdown}
113+
</CodeView>
114+
```
115+
116+
### Basic Code Preview
117+
118+
For simple code snippets without markdown:
119+
120+
```tsx
121+
import CodeView from 'react-code-view';
122+
123+
const code = `
46124
<button onClick={() => alert('Hello!')}>
47125
Click me
48126
</button>
49-
`.trim();
127+
`;
50128

51-
return (
52-
<CodeView
53-
language="jsx"
54-
editable
55-
renderPreview
56-
>
57-
{code}
58-
</CodeView>
59-
);
60-
}
129+
<CodeView language="jsx" editable renderPreview>
130+
{code}
131+
</CodeView>
61132
```
62133

63134
## 📚 Packages
@@ -73,7 +144,30 @@ This monorepo contains the following packages:
73144

74145
## 🔧 Build Tool Integration
75146

76-
React Code View supports all major build tools through [unplugin](https://github.com/unjs/unplugin):
147+
React Code View supports all major build tools through [unplugin](https://github.com/unjs/unplugin).
148+
149+
Once configured, you can **import `.md` files as React components** - the most convenient way to create interactive documentation!
150+
151+
**Why this is amazing:**
152+
- 📝 Write markdown files with code examples
153+
- 🎯 Import them like regular React components
154+
- ⚡ Get live, interactive demos automatically
155+
- 🔒 Full TypeScript support and type safety
156+
- 🎨 Pass props like `theme`, `dependencies`, etc.
157+
158+
**Example:**
159+
160+
```tsx
161+
import Demo from './example.md';
162+
163+
function App() {
164+
return (
165+
<div>
166+
<Demo theme="rcv-theme-dark" />
167+
</div>
168+
);
169+
}
170+
```
77171

78172
### Vite
79173

@@ -149,18 +243,22 @@ module.exports = {
149243

150244
| Prop | Type | Default | Description |
151245
|------|------|---------|-------------|
152-
| `children` | `string` | - | Source code to display |
153-
| `dependencies` | `object` | `{}` | Dependencies for code execution |
246+
| `children` | `string` | - | Source code or markdown content to display |
247+
| `dependencies` | `object` | `{}` | Dependencies for code execution (e.g., `{ useState: React.useState }`) |
154248
| `language` | `string` | `'jsx'` | Syntax highlighting language |
155249
| `editable` | `boolean` | `true` | Enable code editing |
156250
| `renderPreview` | `boolean` | `true` | Show live preview |
157251
| `showLineNumbers` | `boolean` | `true` | Show line numbers |
158252
| `showCopyButton` | `boolean` | `true` | Show copy button |
253+
| `defaultShowCode` | `boolean` | `false` | Initially show code section |
159254
| `theme` | `string` | `'rcv-theme-default'` | Theme class name |
160255
| `beforeCompile` | `function` | - | Transform code before compile |
161256
| `afterCompile` | `function` | - | Transform code after compile |
162257
| `onChange` | `function` | - | Callback when code changes |
163258
| `onError` | `function` | - | Callback when error occurs |
259+
| `emptyPreviewContent` | `ReactNode` | - | Content to display when preview is empty |
260+
261+
**Note:** When `children` contains markdown with `<!--start-code-->` markers, CodeView automatically parses and renders code blocks as interactive components.
164262

165263
### Other Components
166264

RELEASE_NOTES_v3.0.0.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# Release Notes — v3.0.0 (2025-12-29)
2+
3+
A major overhaul that migrates `react-code-view` into a PNPM/Turbo monorepo, unifies build integrations, and refreshes the API/docs. This release is **breaking** compared to 2.6.1. Usage examples below follow the latest README/docs.
4+
5+
## Highlights
6+
- Monorepo migration with PNPM + Turbo; updated CI (Node 18+, caching, docs from `docs/dist`).
7+
- Unified build plugin: new `@react-code-view/unplugin` for Vite/Webpack/Rollup/esbuild/Rspack.
8+
- Imports simplified: `CodeView` is the default export from `@react-code-view/react`; re-exported via `react-code-view` if needed.
9+
- Styles streamlined: use `import '@react-code-view/react/styles/index.css'` (CSS). Legacy Less entries removed.
10+
- `useCodeExecution` stabilized: stable `execute` ref and `updateCode` alias; hook examples refreshed.
11+
- Docs rewritten (installation, usage, migration) and Changesets added for versioning.
12+
13+
## Breaking Changes
14+
- **Tooling requirements:** Node >= 18, PNPM >= 8 (dev workflow). Update CI to match.
15+
- **Imports:** Prefer `import CodeView from 'react-code-view'`; adjust named imports if you targeted old paths.
16+
- **Styles:** Switch to CSS entries: `import 'react-code-view/styles'` (or specific CSS files). Remove Less imports.
17+
- **Build integration:** Legacy `webpack-md-loader` removed. Use the unified unplugin instead.
18+
19+
## Migration Guide (v2.6.1 → v3.0.0)
20+
Use `@react-code-view/react` as the primary entry and the new unplugin across bundlers.
21+
22+
1) **Install**
23+
```bash
24+
pnpm add @react-code-view/react @react-code-view/unplugin
25+
```
26+
27+
2) **Imports**
28+
```tsx
29+
import CodeView from '@react-code-view/react';
30+
import { Renderer, MarkdownRenderer } from '@react-code-view/react';
31+
// (Optional) re-exported convenience:
32+
// import CodeView from 'react-code-view';
33+
```
34+
35+
3) **Styles (CSS)**
36+
```tsx
37+
import '@react-code-view/react/styles/index.css';
38+
```
39+
40+
4) **Build plugin (Vite example)**
41+
```js
42+
// vite.config.js
43+
import { defineConfig } from 'vite';
44+
import react from '@vitejs/plugin-react';
45+
import reactCodeView from '@react-code-view/unplugin/vite';
46+
47+
export default defineConfig({
48+
plugins: [react(), reactCodeView()]
49+
});
50+
```
51+
Webpack: `@react-code-view/unplugin/webpack`
52+
Rollup: `@react-code-view/unplugin/rollup`
53+
esbuild: `@react-code-view/unplugin/esbuild`
54+
Rspack: `@react-code-view/unplugin/rspack`
55+
56+
5) **Hook usage (`useCodeExecution`)**
57+
```tsx
58+
import { useCodeExecution } from '@react-code-view/react';
59+
60+
const { element, error, code, updateCode, execute } = useCodeExecution(
61+
initialCode,
62+
{
63+
dependencies: { Button },
64+
transformOptions: { transforms: ['typescript', 'jsx'] },
65+
beforeCompile: (c) => c.trim(),
66+
afterCompile: (c) => c,
67+
onError: (e) => console.error('Execution error:', e)
68+
}
69+
);
70+
```
71+
72+
6) **Remove legacy `webpack-md-loader`**
73+
- Replace with the unified unplugin (see entries above).
74+
75+
## Feature Details
76+
- Monorepo structure with Changesets-driven versioning and consistent lint/format configs.
77+
- `useCodeExecution` stability improvements and docs examples (matches latest README snippets).
78+
- README/docs updated for new package layout, usage, and migration steps.
79+
- CI updated to use PNPM via Corepack; gh-pages publishes `docs/dist`.
80+
81+
## Links
82+
- PR: https://github.com/simonguo/react-code-view/pull/59
83+
- npm v3.0.0: https://www.npmjs.com/package/react-code-view/v/3.0.0
84+
- v2.6.1 (previous): https://www.npmjs.com/package/react-code-view/v/2.6.1

docs/components/Sidebar.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ export const Sidebar: React.FC = () => {
1313
title: 'Getting Started',
1414
items: [
1515
{ path: '/', label: 'Overview' },
16-
{ path: '/installation', label: 'Installation' },
1716
{ path: '/quick-start', label: 'Quick Start' },
1817
]
1918
},
@@ -44,6 +43,7 @@ export const Sidebar: React.FC = () => {
4443
{ path: '/examples/typescript', label: 'TypeScript' },
4544
{ path: '/examples/theme', label: 'Theme Switcher' },
4645
{ path: '/examples/components', label: 'Custom Components' },
46+
{ path: '/examples/markdown', label: 'Markdown with Code' },
4747
{ path: '/examples/use-code-execution', label: 'useCodeExecution' },
4848
]
4949
}

docs/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import { Sidebar } from './components/Sidebar';
1111

1212
// Pages
1313
import { OverviewPage } from './pages/OverviewPage';
14-
import { InstallationPage } from './pages/InstallationPage';
1514
import { QuickStartPage } from './pages/QuickStartPage';
1615
import { ComponentsPage } from './pages/ComponentsPage';
1716
import { BuildToolsPage } from './pages/BuildToolsPage';
@@ -24,6 +23,7 @@ import { TypeScriptExample } from './pages/examples/TypeScriptExample';
2423
import { ThemeExample } from './pages/examples/ThemeExample';
2524
import { ComponentsExample } from './pages/examples/ComponentsExample';
2625
import { UseCodeExecutionExample } from './pages/examples/UseCodeExecutionExample';
26+
import { MarkdownExample } from './pages/examples/MarkdownExample';
2727

2828
// Pre-initialize Shiki for faster first render
2929
initHighlighter();
@@ -60,7 +60,6 @@ const App: React.FC = () => {
6060
<main className="docs-main">
6161
<Routes>
6262
<Route path="/" element={<OverviewPage theme={theme} />} />
63-
<Route path="/installation" element={<InstallationPage theme={theme} />} />
6463
<Route path="/quick-start" element={<QuickStartPage theme={theme} />} />
6564
<Route path="/components/:component" element={<ComponentsPage theme={theme} />} />
6665
<Route path="/components/use-code-execution" element={<UseCodeExecutionPage theme={theme} />} />
@@ -71,6 +70,7 @@ const App: React.FC = () => {
7170
<Route path="/examples/theme" element={<ThemeExample theme={theme} />} />
7271
<Route path="/examples/components" element={<ComponentsExample theme={theme} />} />
7372
<Route path="/examples/use-code-execution" element={<UseCodeExecutionExample theme={theme} />} />
73+
<Route path="/examples/markdown" element={<MarkdownExample theme={theme} />} />
7474
</Routes>
7575
</main>
7676
</div>

0 commit comments

Comments
 (0)