UNPKG

rehype-docm

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