UNPKG

remark-codesandbox

Version:

🎩 Create CodeSandbox directly from code blocks

3 lines (2 loc) • 6.84 kB
"use strict";function e(e){return e&&"object"==typeof e&&"default"in e?e.default:e}var t=e(require("unist-util-visit")),r=e(require("unist-util-is")),n=e(require("mdast-util-to-string")),i=e(require("unist-builder")),o=e(require("codesandbox/lib/api/define")),a=e(require("isomorphic-fetch"));function s(){return(s=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e}).apply(this,arguments)}var u="undefined"!=typeof Symbol?Symbol.iterator||(Symbol.iterator=Symbol("Symbol.iterator")):"@@iterator";function c(e,t,r){if(!e.s){if(r instanceof f){if(!r.s)return void(r.o=c.bind(null,e,t));1&t&&(t=r.s),r=r.v}if(r&&r.then)return void r.then(c.bind(null,e,t),c.bind(null,e,2));e.s=t,e.v=r;var n=e.o;n&&n(e)}}var f=function(){function e(){}return e.prototype.then=function(t,r){var n=new e,i=this.s;if(i){var o=1&i?t:r;if(o){try{c(n,1,o(this.v))}catch(e){c(n,2,e)}return n}return this}return this.o=function(e){try{var i=e.v;1&e.s?c(n,1,t?t(i):i):r?c(n,1,r(i)):c(n,2,i)}catch(e){c(n,2,e)}},n},e}();function l(e){return e instanceof f&&1&e.s}var d,h=o.getParameters,v=function(e,t){function r(e){return e.trim().split(";").map(function(e){return e.trim()}).filter(Boolean).map(function(e){return e.split(":").map(function(e){return e.trim()})})}var n=r(e),i=r(t),o=Object.fromEntries(n.concat(i));return Object.entries(o).map(function(e){return e.join(":")+";"}).join(" ")};d=window.URLSearchParams;var m={react:{extends:"new"},"react-component":{extends:"new",entry:"src/App.js"}},p=["overrideEntry","entry","style"];module.exports=function(e){void 0===e&&(e={});var o=new Map,y=e.mode||"meta",b=s({},m,{},e.customTemplates||{}),w=e.autoDeploy||!1,g="iframe"===y?{fontsize:"14px",hidenavigation:1,theme:"dark"}:void 0;return void 0!==e.query?g=e.query:void 0!==e.iframeQuery&&(console.warn("options.iframeQuery is now deprecated and will be removed in the upcoming version, please use options.query instead."),g=e.iframeQuery),function(e,m){try{var x,P=[];return t(e,function(e,t,i){!x&&r(e,["heading",{depth:1}])?x=n(e):r(e,"code")&&P.push([e,t,i])}),Promise.resolve(function(e,t,r){if("function"==typeof e[u]){var n,i,o,a=e[u]();if(function e(r){try{for(;!(n=a.next()).done;)if((r=t(n.value))&&r.then){if(!l(r))return void r.then(e,o||(o=c.bind(null,i=new f,2)));r=r.v}i?c(i,1,r):i=r}catch(e){c(i||(i=new f),2,e)}}(),a.return){var s=function(e){try{n.done||a.return()}catch(e){}return e};if(i&&i.then)return i.then(s,function(e){throw s(e)});s()}return i}if(!("length"in e))throw new TypeError("Object is not iterable");for(var d=[],h=0;h<e.length;h++)d.push(e[h]);return function(e,t,r){var n,i,o=-1;return function r(a){try{for(;++o<e.length;)if((a=t(o))&&a.then){if(!l(a))return void a.then(r,i||(i=c.bind(null,n=new f,2)));a=a.v}n?c(n,1,a):n=a}catch(e){c(n||(n=new f),2,e)}}(),n}(d,function(e){return t(d[e])})}(P,function(e){var t,r,n=e[0],u=e[2],c=(t=n.meta||"",r={},t.split(" ").forEach(function(e){var t=e.indexOf("=");if(t>0){var n=e.slice(0,t),i=e.slice(t+1);r[n]=i}}),r).codesandbox;if(c){var f=c.split("?"),l=f[0],m=f[1];return Promise.resolve(function(e,t,r,n){try{return e.has(t)||e.set(t,function e(t,r,n){try{var i=function(e){var r;if(o)return e;var n=function(){if(t.startsWith("file:"))throw new Error('"file:" template is not supported in browser environment!');var e=function(e){var t={};(r.directories||[]).forEach(function(e){t[e.shortid]=e}),(r.modules||[]).forEach(function(e){t[e.shortid]=e});var n={};(r.modules||[]).forEach(function(e){var r=function e(t,r){var n=t[r];return n?[e(t,n.directory_shortid),n.title].filter(Boolean).join("/"):null}(t,e.shortid);n[r]={content:e.code}}),r.files=n},n=function(e,n){try{var i=Promise.resolve(a("https://codesandbox.io/api/v1/sandboxes/"+t)).then(function(e){return Promise.resolve(e.json()).then(function(t){r=(e=t).data})})}catch(e){return n(e)}return i&&i.then?i.then(void 0,n):i}(0,function(e){throw console.error("Failed to get the sandbox template: "+t),e});return n&&n.then?n.then(e):e()}();return n&&n.then?n.then(function(e){return r}):r},o=!1,u=function(){if(r[t])return Promise.resolve(e(r[t].extends,r)).then(function(e){var n=function(e,t){return s({},e,{},t,{files:s({},e.files||{},{},t.files||{})})}(e,r[t]);return o=!0,n})}();return Promise.resolve(u&&u.then?u.then(i):i(u))}catch(e){return Promise.reject(e)}}(t,r)),Promise.resolve(e.get(t))}catch(e){return Promise.reject(e)}}(o,l,b)).then(function(e){var t;function r(){switch(y){case"button":var t=i("paragraph",[i("link",{url:U},[i("image",{url:"https://codesandbox.io/static/img/play-codesandbox.svg",alt:"Edit on CodeSandbox"})])]),r=u.children.indexOf(n);return void u.children.splice(r+1,0,t);case"iframe":var o=i("html",{value:'<iframe\n src="'+(w?U.replace("/s/","/embed/"):U+"&embed=1")+'"\n style="'+v("width:100%; height:500px; border:0; border-radius:4px; overflow:hidden;",P)+'"\n title="'+(e.title||"")+'"\n allow="geolocation; microphone; camera; midi; vr; accelerometer; gyroscope; payment; ambient-light-sensor; encrypted-media; usb"\n sandbox="allow-modals allow-forms allow-popups allow-scripts allow-same-origin"\n></iframe>'}),a=u.children.indexOf(n);return void u.children.splice(a,1,o);case"meta":default:return n.data=n.data||{},n.data.hProperties=n.data.hProperties||{},n.data.codesandboxUrl=U,void(n.data.hProperties.dataCodesandboxUrl=U)}}e.title=x||e.title;var o,c=function(e){var t=new URLSearchParams;new URLSearchParams(e).forEach(function(e,r){t.set(r,e)});for(var r=arguments.length,n=new Array(r>1?r-1:0),i=1;i<r;i++)n[i-1]=arguments[i];return n.forEach(function(e){new URLSearchParams(e).forEach(function(e,r){t.set(r,e)})}),t}(g,e.query,m),f=c.has("entry")?(o=c.get("entry")).startsWith("./")?o.slice(2):o.startsWith("/")?o.slice(1):o:e.entry;c.has("module")||c.set("module",f.startsWith("/")?f:"/"+f);var b=c.get("overrideEntry"),P=c.get("style")||"";if(p.forEach(function(e){c.delete(e)}),!e.files[f])throw new Error('Entry "'+f+'" is not present in template "'+l+'".');var j=e.files[f].content;if(b){if("false"!==b){var E=b.split("-"),S=E[0],q=E[1],O=j.split("\n");j=[].concat(O.slice(0,Number(S)-1),[n.value],""===q?[]:O.slice(Number(q))).join("\n")}}else j=n.value;var U,L=h({files:s({},e.files,(t={},t[f]={content:j},t))}),R=function(){if(w)return Promise.resolve(a("https://codesandbox.io/api/v1/sandboxes/define",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({parameters:L,json:1})}).then(function(e){return e.json()})).then(function(e){U="https://codesandbox.io/s/"+e.sandbox_id+"?"+c.toString()});U="https://codesandbox.io/api/v1/sandboxes/define?"+new d({parameters:L,query:c}).toString()}();return R&&R.then?R.then(r):r()})}}))}catch(e){return Promise.reject(e)}}}; //# sourceMappingURL=remark-codesandbox.js.map