diff --git a/.dumi/rehypeAntd.ts b/.dumi/rehypeAntd.ts index 7c3bdc6e12..56c5b1acf3 100644 --- a/.dumi/rehypeAntd.ts +++ b/.dumi/rehypeAntd.ts @@ -68,17 +68,29 @@ function rehypeAntd(): UnifiedTransformer { node.tagName = 'VideoPlayer'; } else if (node.tagName === 'SourceCode') { const { lang } = node.properties; + if (typeof lang === 'string' && lang.startsWith('sandpack')) { + const code = (node.children[0] as any).value as string; + const configRegx = /^const sandpackConfig = ([\S\s]*?});/; + const [configString] = code.match(configRegx) || []; + // eslint-disable-next-line no-eval + const config = configString && eval(`(${configString.replace(configRegx, '$1')})`); + Object.keys(config || {}).forEach((key) => { + if (typeof config[key] === 'object') { + config[key] = JSON.stringify(config[key]); + } + }); + parent!.children.splice(i!, 1, { type: 'element', tagName: 'Sandpack', properties: { - dark: lang === 'sandpackdark', + ...config, }, children: [ { type: 'text', - value: (node.children[0] as any).value, + value: code.replace(configRegx, '').trim(), }, ], }); diff --git a/.dumi/theme/builtins/Sandpack/index.tsx b/.dumi/theme/builtins/Sandpack/index.tsx index 791cdce75a..cc38bbc751 100644 --- a/.dumi/theme/builtins/Sandpack/index.tsx +++ b/.dumi/theme/builtins/Sandpack/index.tsx @@ -1,34 +1,11 @@ -import type { ReactNode } from 'react'; +import type { FC, ReactNode } from 'react'; import React, { Suspense } from 'react'; -import { useSearchParams, useServerInsertedHTML } from 'dumi'; +import { useSearchParams } from 'dumi'; import { createStyles } from 'antd-style'; -import { getSandpackCssText } from '@codesandbox/sandpack-react'; import { Skeleton } from 'antd'; const OriginSandpack = React.lazy(() => import('./Sandpack')); -const setup = { - dependencies: { - react: '^18.0.0', - 'react-dom': '^18.0.0', - antd: '^5.0.0', - }, - devDependencies: { - '@types/react': '^18.0.0', - '@types/react-dom': '^18.0.0', - typescript: '^5', - }, - entry: 'index.tsx', -}; - -const options = { - activeFile: 'app.tsx' as never, - visibleFiles: ['index.tsx', 'app.tsx', 'package.json', 'index.css'] as any, - showLineNumbers: true, - editorHeight: '500px', - autorun: false, -}; - const indexContent = `import React from 'react'; import { createRoot } from 'react-dom/client'; import App from './app'; @@ -64,16 +41,44 @@ const SandpackFallback = () => { ); }; -const Sandpack = ({ children, dark }: { children: ReactNode; dark: boolean }) => { +type SandpackProps = { + children?: ReactNode; + dark?: boolean; + autorun?: boolean; + dependencies?: string; +}; + +const Sandpack: FC = ({ + children, + dark, + dependencies: extraDeps, + autorun = false, +}) => { const [searchParams] = useSearchParams(); + const dependencies = extraDeps && JSON.parse(extraDeps); + + const setup = { + dependencies: { + react: '^18.0.0', + 'react-dom': '^18.0.0', + antd: '^5.0.0', + ...dependencies, + }, + devDependencies: { + '@types/react': '^18.0.0', + '@types/react-dom': '^18.0.0', + typescript: '^5', + }, + entry: 'index.tsx', + }; - useServerInsertedHTML(() => ( -