UNPKG

rehype-docm

Version:
2 lines (1 loc) 1.19 kB
import{relative,dirname}from"path";import is from"unist-util-is";import flatten from"lodash.flatten";import nodeToString from"hast-util-to-string";import{jsx,imports}from"docm-utils";import{format}from"docm-utils/lib/format";import{getSandboxImportInfo}from"docm-utils/lib/codesandbox";const isPlayground=o=>"Playground"===o,addPropsOnPlayground=async(o,t,r,i,a,e)=>{const s=jsx.componentName(o.value),n=RegExp(`^\\<${s}`);if(isPlayground(s)){const m=(await format(nodeToString(o))).slice(1,1/0),p=`{props: this ? this.props : props,${r.join(",")}}`,l=jsx.sanitizeCode(jsx.removeTags(m));if(o.value=o.value.replace(n,`<${s} __position={${t}} __code={'${l}'} __scope={${p}}`),e){const t=await getSandboxImportInfo(l,i,a);o.value=o.value.replace(n,`<${s} __codesandbox={\`${t}\`}`)}}},addComponentsProps=(o,t,r,i)=>async(a,e)=>{await addPropsOnPlayground(a,e,o,t,r,i)};var index=(o,t)=>()=>(r,i)=>{const a=r.children.filter(o=>is("import",o)),e=flatten(a.map(imports.getFullImports)),s=flatten(a.map(imports.getImportsVariables)),n=relative(o,dirname(i.history[0])),m=r.children.filter(o=>is("jsx",o)).map(addComponentsProps(s,e,n,t));return Promise.all(m).then(()=>r)};export default index;