Skip to content

Commit d410af8

Browse files
committed
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
1 parent 3d22cd6 commit d410af8

File tree

23 files changed

+1014
-30
lines changed

23 files changed

+1014
-30
lines changed

README.md

Lines changed: 80 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ A React component library for rendering code with **live preview** and syntax hi
1212

1313
- 🎨 **Live Preview** - Execute and preview React code in real-time
1414
- ✏️ **Editable Code** - Built-in code editor with syntax highlighting
15-
- 📝 **Markdown Support** - Render markdown content with code blocks
15+
- 📝 **Native Markdown Parsing** - Parse and render markdown with embedded code blocks at runtime
1616
- 🔌 **Universal Plugin** - Works with Webpack, Vite, Rollup, esbuild, and Rspack
1717
- 🎯 **TypeScript** - Full TypeScript support out of the box
1818
- 📦 **Tree-shakeable** - Import only what you need
19+
-**Zero Config** - Works out of the box with sensible defaults
1920

2021
## ✅ Requirements
2122

@@ -37,6 +38,8 @@ yarn add react-code-view
3738

3839
## 🚀 Quick Start
3940

41+
### Basic Code Preview
42+
4043
```tsx
4144
import CodeView from 'react-code-view';
4245
import 'react-code-view/styles';
@@ -60,6 +63,45 @@ function App() {
6063
}
6164
```
6265

66+
### Markdown with Code Blocks (New!)
67+
68+
CodeView now supports **native markdown parsing** - no build configuration needed!
69+
70+
```tsx
71+
import CodeView from 'react-code-view';
72+
import markdown from './example.md?raw'; // Import as raw text
73+
74+
function App() {
75+
return (
76+
<CodeView dependencies={{ useState: React.useState }}>
77+
{markdown}
78+
</CodeView>
79+
);
80+
}
81+
```
82+
83+
Your markdown file (`example.md`):
84+
85+
```markdown
86+
# Interactive Example
87+
88+
Here's a live counter component:
89+
90+
<!--start-code-->
91+
\`\`\`jsx
92+
function Counter() {
93+
const [count, setCount] = useState(0);
94+
return <button onClick={() => setCount(count + 1)}>Count: {count}</button>;
95+
}
96+
render(<Counter />);
97+
\`\`\`
98+
<!--end-code-->
99+
100+
More markdown content here...
101+
```
102+
103+
The code blocks are automatically extracted and rendered as interactive components!
104+
63105
## 📚 Packages
64106

65107
This monorepo contains the following packages:
@@ -73,7 +115,37 @@ This monorepo contains the following packages:
73115

74116
## 🔧 Build Tool Integration
75117

76-
React Code View supports all major build tools through [unplugin](https://github.com/unjs/unplugin):
118+
React Code View supports all major build tools through [unplugin](https://github.com/unjs/unplugin).
119+
120+
The plugin uses **native parseHTML** by default, generating CodeView components that parse markdown at runtime:
121+
122+
```js
123+
// vite.config.js
124+
import { defineConfig } from 'vite';
125+
import react from '@vitejs/plugin-react';
126+
import reactCodeView from '@react-code-view/unplugin/vite';
127+
128+
export default defineConfig({
129+
plugins: [
130+
react(),
131+
reactCodeView() // Uses native parseHTML by default
132+
]
133+
});
134+
```
135+
136+
Then import markdown files directly:
137+
138+
```tsx
139+
import Demo from './example.md';
140+
141+
<Demo dependencies={{ useState: React.useState }} theme="rcv-theme-default" />
142+
```
143+
144+
**Benefits:**
145+
- ✅ Consistent with runtime `parseHTML` behavior
146+
- ✅ Interactive code blocks automatically rendered
147+
- ✅ Smaller bundle size (no pre-rendered HTML)
148+
- ✅ Type-safe imports from build tools
77149

78150
### Vite
79151

@@ -149,18 +221,22 @@ module.exports = {
149221

150222
| Prop | Type | Default | Description |
151223
|------|------|---------|-------------|
152-
| `children` | `string` | - | Source code to display |
153-
| `dependencies` | `object` | `{}` | Dependencies for code execution |
224+
| `children` | `string` | - | Source code or markdown content to display |
225+
| `dependencies` | `object` | `{}` | Dependencies for code execution (e.g., `{ useState: React.useState }`) |
154226
| `language` | `string` | `'jsx'` | Syntax highlighting language |
155227
| `editable` | `boolean` | `true` | Enable code editing |
156228
| `renderPreview` | `boolean` | `true` | Show live preview |
157229
| `showLineNumbers` | `boolean` | `true` | Show line numbers |
158230
| `showCopyButton` | `boolean` | `true` | Show copy button |
231+
| `defaultShowCode` | `boolean` | `false` | Initially show code section |
159232
| `theme` | `string` | `'rcv-theme-default'` | Theme class name |
160233
| `beforeCompile` | `function` | - | Transform code before compile |
161234
| `afterCompile` | `function` | - | Transform code after compile |
162235
| `onChange` | `function` | - | Callback when code changes |
163236
| `onError` | `function` | - | Callback when error occurs |
237+
| `emptyPreviewContent` | `ReactNode` | - | Content to display when preview is empty |
238+
239+
**Note:** When `children` contains markdown with `<!--start-code-->` markers, CodeView automatically parses and renders code blocks as interactive components.
164240

165241
### Other Components
166242

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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export const Sidebar: React.FC = () => {
4444
{ path: '/examples/typescript', label: 'TypeScript' },
4545
{ path: '/examples/theme', label: 'Theme Switcher' },
4646
{ path: '/examples/components', label: 'Custom Components' },
47+
{ path: '/examples/markdown', label: 'Markdown with Code' },
4748
{ path: '/examples/use-code-execution', label: 'useCodeExecution' },
4849
]
4950
}

docs/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { TypeScriptExample } from './pages/examples/TypeScriptExample';
2424
import { ThemeExample } from './pages/examples/ThemeExample';
2525
import { ComponentsExample } from './pages/examples/ComponentsExample';
2626
import { UseCodeExecutionExample } from './pages/examples/UseCodeExecutionExample';
27+
import { MarkdownExample } from './pages/examples/MarkdownExample';
2728

2829
// Pre-initialize Shiki for faster first render
2930
initHighlighter();
@@ -71,6 +72,7 @@ const App: React.FC = () => {
7172
<Route path="/examples/theme" element={<ThemeExample theme={theme} />} />
7273
<Route path="/examples/components" element={<ComponentsExample theme={theme} />} />
7374
<Route path="/examples/use-code-execution" element={<UseCodeExecutionExample theme={theme} />} />
75+
<Route path="/examples/markdown" element={<MarkdownExample theme={theme} />} />
7476
</Routes>
7577
</main>
7678
</div>
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import React from 'react';
2+
import { Section } from '../../components/Section';
3+
import { CodeView } from '@react-code-view/react';
4+
import demoMarkdown from './demo.md?raw';
5+
6+
interface MarkdownExampleProps {
7+
theme: string;
8+
}
9+
10+
export const MarkdownExample: React.FC<MarkdownExampleProps> = ({ theme }) => {
11+
return (
12+
<div className="page-content">
13+
<Section id="markdown" title="Markdown with Code Blocks">
14+
<p>
15+
CodeView natively supports parsing markdown files with embedded code blocks.
16+
Use <code>&lt;!--start-code--&gt;</code> and <code>&lt;!--end-code--&gt;</code>
17+
comments to mark executable code sections.
18+
</p>
19+
20+
<div className="example-demo">
21+
<CodeView
22+
theme={theme}
23+
dependencies={{ useState: React.useState }}
24+
>
25+
{demoMarkdown}
26+
</CodeView>
27+
</div>
28+
29+
<h3>How it works</h3>
30+
<ul>
31+
<li>Import markdown files as raw strings</li>
32+
<li>CodeView automatically detects and parses code blocks marked with special comments</li>
33+
<li>Markdown content is rendered between code blocks</li>
34+
<li>Code blocks are fully interactive and editable</li>
35+
<li>Perfect for documentation with live examples</li>
36+
</ul>
37+
38+
<h3>Markdown syntax</h3>
39+
<pre style={{
40+
background: '#f6f8fa',
41+
padding: '16px',
42+
borderRadius: '6px',
43+
overflow: 'auto'
44+
}}>
45+
{`# Your Markdown Content
46+
47+
Regular markdown text here...
48+
49+
<!--start-code-->
50+
\`\`\`jsx
51+
import React from 'react';
52+
53+
function MyComponent() {
54+
return <div>Interactive code!</div>;
55+
}
56+
57+
render(<MyComponent />);
58+
\`\`\`
59+
<!--end-code-->
60+
61+
More markdown content...`}
62+
</pre>
63+
</Section>
64+
</div>
65+
);
66+
};

0 commit comments

Comments
 (0)