import fs from 'fs'; import type { IApi, IRoute } from 'dumi'; import { extractStyle } from '@ant-design/cssinjs'; import ReactTechStack from 'dumi/dist/techStacks/react'; import sylvanas from 'sylvanas'; /** * extends dumi internal tech stack, for customize previewer props */ class AntdReactTechStack extends ReactTechStack { // eslint-disable-next-line class-methods-use-this generatePreviewerProps(...[props, opts]: any) { if (opts.type === 'external') { // try to find md file with the same name as the demo tsx file const locale = opts.mdAbsPath.match(/index\.([a-z-]+)\.md$/i)?.[1]; const mdPath = opts.fileAbsPath!.replace(/\.\w+$/, '.md'); const md = fs.existsSync(mdPath) ? fs.readFileSync(mdPath, 'utf-8') : ''; const codePath = opts.fileAbsPath!.replace(/\.\w+$/, '.tsx'); const code = fs.existsSync(codePath) ? fs.readFileSync(codePath, 'utf-8') : ''; props.jsx = sylvanas.parseText(code); if (md) { // extract description & css style from md file const description = md.match( new RegExp(`(?:^|\\n)## ${locale}([^]+?)(\\n## [a-z]|\\n\`\`\`|\\n