我最近将 react-code-view 进行了重写, 基于 unplugin 让 Markdown 可以直接导入渲染成一个 React 组件. 以下是一个简单示例
npm install @react-code-view/react @react-code-view/unplugin
// 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() // Enable markdown import
]
});
# Counter Example
A simple counter to demonstrate live code editing and preview.
<!--start-code-->
\`\`\`jsx
const App = () => {
const [count, setCount] = useState(0);
return (
<button onClick={() => setCount(c => c + 1)}>
Clicked {count} times
</button>
);
};
render(<App />);
\`\`\`
<!--end-code-->
- Click "Show Code" to view and edit the source
- Preview updates instantly while you type
- Click the copy button to reuse the code
import Demo from './demo.md';
function App() {
return <Demo />;
}
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.