UNPKG

@sanity/block-content-to-markdown

Version:

[Deprecated] Transforming Sanity block content to markdown

2 lines (1 loc) 19.3 kB
!function(t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):("undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this).SanityBlockContentToMarkdown=t()}(function(){return function r(i,o,s){function a(e,t){if(!o[e]){if(!i[e]){var n="function"==typeof require&&require;if(!t&&n)return n(e,!0);if(l)return l(e,!0);throw(t=new Error("Cannot find module '"+e+"'")).code="MODULE_NOT_FOUND",t}n=o[e]={exports:{}},i[e][0].call(n.exports,function(t){return a(i[e][1][t]||t)},n,n.exports,r,i,o,s)}return o[e].exports}for(var l="function"==typeof require&&require,t=0;t<s.length;t++)a(s[t]);return a}({1:[function(t,e,n){e.exports=t("./lib/toMarkdown")},{"./lib/toMarkdown":3}],2:[function(t,e,n){var r=t("@sanity/block-content-to-hyperscript/internals").getImageUrl;function s(t){var e=1<arguments.length&&void 0!==arguments[1]?arguments[1]:"";return Array.isArray(t.children)?t.children.join(e):t.children}function i(t,e,n){var r,i,o,n=s(n);return e?(e=n.search(/\S/),-1==(r=n.search(/\S(?=\s*$)/))||-1==e?n:(i=n.substring(0,e),o=n.substring(r+1),""+i+t+n.substring(e,r+1)+t+o)):""+t+n+t}e.exports={types:{block:function(t){var e=t.node.style||"normal";return/^h\d$/.test(e)?new Array(parseInt(e[1],10)+1).join("#")+" "+s(t):"blockquote"===e?"> "+s(t):s(t)},image:function(t){var e=t.title;return"!["+(t.alt||"")+"]("+r(t)+(e?" "+JSON.stringify(e):"")+")"}},marks:{"strike-through":i.bind(null,"~~",!0),em:i.bind(null,"_",!0),code:i.bind(null,"`",!1),strong:i.bind(null,"**",!0),underline:s,link:function(t){var e=(n=t.mark).href,n=(n=n.title)?" "+JSON.stringify(n):"";return"["+s(t)+"]("+e+n+")"}},list:function(t){var e=new Array(t.level||1).join(" ");return e+s(t,"\n"+e)},listItem:function(t){return("bullet"===t.node.listItem?"*":t.index+1+".")+" "+s(t)},container:function(t){return s(t,"\n\n")},hardBreak:function(){return" \n"},markFallback:s}},{"@sanity/block-content-to-hyperscript/internals":4}],3:[function(t,e,n){function r(){function t(t){var e;return"block"===t._type&&Array.isArray(t.children)?((e=Object.assign({},t)).children=r(t.children),e):"span"===t._type&&0==t.text.length&&Array.isArray(t.marks)?0==(e=t.marks.filter(function(t){return!h.includes(t)})).length?null:Object.assign({},t,{marks:e}):t}var e=0<arguments.length&&void 0!==arguments[0]?arguments[0]:[];return Array.isArray(e)?e.map(t).filter(function(t){return!!t}):t(e)}function i(t,e,n){return n=n||e.node&&e.node.children,t(Object.assign({},e,{children:n}))}function o(t){var e=1<arguments.length&&void 0!==arguments[1]?arguments[1]:{},t=r(t||[]),n=u(d,e.serializers||{}),e=Object.assign({},e,{blocks:t,serializers:n,listNestMode:"last-child"});return c(i,e,f,p).trim()}var s=t("@sanity/block-content-to-hyperscript/internals"),a=s.getImageUrl,l=s.getSerializers,c=s.blocksToNodes,u=s.mergeSerializers,s=t("./serializers"),h=["strike-through","em","strong","underline"],t=l(i),f=t.defaultSerializers,p=t.serializeSpan,d=u(f,s);o.defaultSerializers=f,o.getImageUrl=a,e.exports=o},{"./serializers":2,"@sanity/block-content-to-hyperscript/internals":4}],4:[function(t,e,n){e.exports=t("./lib/internals")},{"./lib/internals":9}],5:[function(t,e,n){var i=t("object-assign"),p=t("./buildMarksTree"),o=t("./nestLists"),s=t("./generateKeys"),a=t("./mergeSerializers"),l=["projectId","dataset","imageOptions"],d={imageOptions:{}};function g(t){return"block"===t._type&&t.listItem}e.exports=function(c,t,e,u){var r=i({},d,t),t=Array.isArray(r.blocks)?r.blocks:[r.blocks],t=s(t),t=o(t,r.listNestMode),h=a(e,r.serializers||{}),f=l.reduce(function(t,e){var n=r[e];return void 0!==n&&(t[e]=n),t},{}),e=Boolean(r.renderContainerOnSingleChild),t=t.map(function r(t,e,n,i){return"list"===(o=t)._type&&o.listItem?(o=t.listItem,s=t.level,a=t._key,l=t.children.map(r),c(h.list,{key:a,level:s,type:o,options:f},l)):g(t)?(s=((t,e)=>{for(var n=0,r=0;r<e.length;r++){if(e[r]===t)return n;g(e[r])&&n++}return n})(a=t,n),o=a._key,l=p(a).map(r),c(h.listItem,{node:a,serializers:h,index:s,key:o,options:f},l)):"string"==typeof(n=t)||n.marks||"span"===n._type?u(t,h,e,{serializeNode:r}):(o=e,n=i,i=p(e=t).map(function(t,e,n){return r(t,e,n,!0)}),o={key:e._key||"block-".concat(o),node:e,isInline:n,serializers:h,options:f},c(h.block,o,i));var o,s,a,l});return e||1<t.length?(e=r.className?{className:r.className}:{},c(h.container,e,t)):t[0]||("function"==typeof h.empty?c(h.empty):h.empty)}},{"./buildMarksTree":6,"./generateKeys":7,"./mergeSerializers":10,"./nestLists":11,"object-assign":15}],6:[function(t,e,n){var s=["strong","em","code","underline","strike-through"];function r(t,i,o){var e;return t.marks&&0!==t.marks.length?(e=t.marks.reduce(function(t,e){t[e]=t[e]?t[e]+1:1;for(var n=i+1;n<o.length;n++){var r=o[n];if(!r.marks||!Array.isArray(r.marks)||-1===r.marks.indexOf(e))break;t[e]++}return t},{}),e=function(t,e,n){var r=t[e]||0,t=t[n]||0;return r!==t?t-r:(t=s.indexOf(e),r=s.indexOf(n),t!==r?t-r:e<n?-1:n<e?1:0)}.bind(null,e),t.marks.slice().sort(e)):t.marks||[]}e.exports=function(t){var l,c,e=t.children,u=t.markDefs;return e&&e.length?(l=e.map(r),c=[t={_type:"span",children:[]}],e.forEach(function(n,t){var e=l[t];if(e){var r=1;if(1<c.length)for(;r<c.length;r++){var i=c[r].markKey,i=e.indexOf(i);if(-1===i)break;e.splice(i,1)}var o=(t=>{for(var e=t.length-1;0<=e;e--){var n=t[e];if("span"===n._type&&n.children)return n}})(c=c.slice(0,r));if(e.forEach(function(e){var t={_type:"span",_key:n._key,children:[],mark:u.find(function(t){return t._key===e})||e,markKey:e};o.children.push(t),c.push(t),o=t}),"span"!==(t=n)._type||"string"!=typeof t.text||!Array.isArray(t.marks)&&void 0!==t.marks)o.children=o.children.concat(n);else{for(var s=n.text.split("\n"),a=s.length;1<a--;)s.splice(a,0,"\n");o.children=o.children.concat(s)}}else c[c.length-1].children.push(n)}),t.children):[]}},{}],7:[function(t,e,n){var r=t("object-assign");function i(t){return(t=>{var e=0,n=t.length;if(0!==n)for(var r=0;r<n;r++)e=(e<<5)-e+t.charCodeAt(r),e&=e;return e})(JSON.stringify(t)).toString(36).replace(/[^A-Za-z0-9]/g,"")}e.exports=function(t){return t.map(function(t){return t._key?t:r({_key:i(t)},t)})}},{"object-assign":15}],8:[function(t,e,n){var r=t("@sanity/generate-help-url"),a=t("@sanity/image-url"),l=t("object-assign"),c=encodeURIComponent,u="You must either:\n - Pass `projectId` and `dataset` to the block renderer\n - Materialize images to include the `url` field.\n\nFor more information, see ".concat(r("block-content-image-materializing"));e.exports=function(t){var e,n,r=t.node,t=t.options,i=t.projectId,o=t.dataset,s=r.asset;if(!s)throw new Error("Image does not have required `asset` property");if(s.url)return s.url+(n=(e=t).imageOptions,(e=Object.keys(n)).length?(e=e.map(function(t){return"".concat(c(t),"=").concat(c(n[t]))}),"?".concat(e.join("&"))):"");if(!i||!o)throw new Error(u);if(s._ref)return a(l({projectId:i,dataset:o},t.imageOptions||{})).image(r).toString();throw new Error("Invalid image reference in block, no `_ref` found on `asset`")}},{"@sanity/generate-help-url":13,"@sanity/image-url":14,"object-assign":15}],9:[function(t,e,n){var i=t("./serializers"),o=t("./blocksToNodes"),r=t("./getImageUrl"),t=t("./mergeSerializers");e.exports={blocksToNodes:function(t,e,n,r){return n?o(t,e,n,r):(n=i(t),o(t,e,n.defaultSerializers,n.serializeSpan))},getSerializers:i,getImageUrl:r,mergeSerializers:t}},{"./blocksToNodes":5,"./getImageUrl":8,"./mergeSerializers":10,"./serializers":12}],10:[function(t,e,n){function o(t){return(o="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}var s=t("object-assign");e.exports=function(r,i){return Object.keys(r).reduce(function(t,e){var n=o(r[e]);return t[e]="function"===n?(void 0!==i[e]?i:r)[e]:"object"===n?s({},r[e],i[e]):(void 0===i[e]?r:i)[e],t},{})}},{"object-assign":15}],11:[function(t,e,n){var l=t("object-assign");function c(t){return{_type:"list",_key:"".concat(t._key,"-parent"),level:t.level,listItem:t.listItem,children:[t]}}function u(t){return t.children&&t.children[t.children.length-1]}function h(t,e){var n="string"==typeof e.listItem;return"list"===t._type&&t.level===e.level&&n&&t.listItem===e.listItem?t:!!(n=u(t))&&h(n,e)}e.exports=function(t){for(var e,n=1<arguments.length&&void 0!==arguments[1]?arguments[1]:"html",r=[],i=0;i<t.length;i++){var o,s,a=t[i];Boolean(a.listItem)?e?(o=e,(s=a).level===o.level&&s.listItem===o.listItem?e.children.push(a):a.level>e.level?(s=c(a),"html"===n?(o=u(e),o=l({},o,{children:o.children.concat(s)}),e.children[e.children.length-1]=o):e.children.push(s),e=s):a.level<e.level?(o=h(r[r.length-1],a))?(e=o).children.push(a):(e=c(a),r.push(e)):a.listItem===e.listItem?(console.warn("Unknown state encountered for block",a),r.push(a)):(s=h(r[r.length-1],{level:a.level}))&&s.listItem===a.listItem?(e=s).children.push(a):(e=c(a),r.push(e))):(e=c(a),r.push(e)):(r.push(a),e=null)}return r}},{"object-assign":15}],12:[function(t,e,n){var s=t("object-assign"),r=t("./getImageUrl");e.exports=function(o,t){var n=t||{useDashedStyles:!1};function e(t,e){return o(t,null,e.children)}return{defaultSerializers:{types:{block:function(t){var e=t.node.style||"normal";return/^h\d/.test(e)?o(e,null,t.children):o("blockquote"===e?"blockquote":"p",null,t.children)},image:function(t){var e;return t.node.asset?(e=o("img",{src:r(t)}),t.isInline?e:o("figure",null,e)):null}},marks:{strong:e.bind(null,"strong"),em:e.bind(null,"em"),code:e.bind(null,"code"),underline:function(t){var e=n.useDashedStyles?{"text-decoration":"underline"}:{textDecoration:"underline"};return o("span",{style:e},t.children)},"strike-through":function(t){return o("del",null,t.children)},link:function(t){return o("a",{href:t.mark.href},t.children)}},list:function(t){var e="bullet"===t.type?"ul":"ol";return o(e,null,t.children)},listItem:function(t){t=t.node.style&&"normal"!==t.node.style?o(t.serializers.types.block,t,t.children):t.children;return o("li",null,t)},block:function(t){var e=t.node,n=e._type,r=t.serializers.types[n];if(r)return o(r,{node:e,options:t.options,isInline:t.isInline},t.children);throw new Error('Unknown block type "'.concat(n,'", please specify a serializer for it in the `serializers.types` prop'))},span:function(t){var e=t.node,n=e.mark,e=e.children,n="string"==typeof n?n:n._type,r=t.serializers.marks[n];return r?o(r,t.node,e):(console.warn('Unknown mark type "'.concat(n,'", please specify a serializer for it in the `serializers.marks` prop')),o(t.serializers.markFallback,null,e))},hardBreak:function(){return o("br")},container:"div",markFallback:"span",text:void 0,empty:""},serializeSpan:function(n,t,e,r){var i;return"\n"===n&&t.hardBreak?o(t.hardBreak,{key:"hb-".concat(e)}):"string"==typeof n?t.text?o(t.text,{key:"text-".concat(e)},n):n:(n.children&&(i={children:n.children.map(function(t,e){return r.serializeNode(t,e,n.children,!0)})}),i=s({},n,i),o(t.span,{key:n._key||"span-".concat(e),node:i,serializers:t}))}}}},{"./getImageUrl":8,"object-assign":15}],13:[function(t,e,n){e.exports=function(t){return"https://docs.sanity.io/help/"+t}},{}],14:[function(t,e,n){var r,i;r=this,i=function(){function g(){return(g=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var n,r=arguments[e];for(n in r)Object.prototype.hasOwnProperty.call(r,n)&&(t[n]=r[n])}return t}).apply(this,arguments)}function n(t,e){(null==e||e>t.length)&&(e=t.length);for(var n=0,r=new Array(e);n<e;n++)r[n]=t[n];return r}function i(t){var e=0;if("undefined"!=typeof Symbol&&null!=t[Symbol.iterator])return(e=t[Symbol.iterator]()).next.bind(e);if(Array.isArray(t)||(t=(t=>{var e;if(t)return"string"==typeof t?n(t,void 0):"Map"===(e="Object"===(e=Object.prototype.toString.call(t).slice(8,-1))&&t.constructor?t.constructor.name:e)||"Set"===e?Array.from(t):"Arguments"===e||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(e)?n(t,void 0):void 0})(t)))return function(){return e>=t.length?{done:!0}:{done:!1,value:t[e++]}};throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var m="image-Tb9Ew8CXIwaY6R1kjMvI0uRR-2000x3000-jpg",y=function(t){return!!t&&"string"==typeof t._ref},b=function(t){return!!t&&"string"==typeof t._id},v=function(t){return!(!t||!t.asset)&&"string"==typeof t.asset.url};function w(t){return("image-"+t.split("/").slice(-1)[0]).replace(/\.([a-z]+)$/,"-$1")}var k=[["width","w"],["height","h"],["format","fm"],["download","dl"],["blur","blur"],["sharpen","sharp"],["invert","invert"],["orientation","or"],["minHeight","min-h"],["maxHeight","max-h"],["minWidth","min-w"],["maxWidth","max-w"],["quality","q"],["fit","fit"],["crop","crop"],["saturation","sat"],["auto","auto"],["dpr","dpr"],["pad","pad"]];function t(t){var e,n,r,i,o,s,a,l,c,u,h,f,p,t=g({},t||{}),d=t.source,d=(delete t.source,(t=>{if(!t)return null;var e;if("string"==typeof t&&/^https?:\/\//.test(""+t))e={asset:{_ref:w(t)}};else if("string"==typeof t)e={asset:{_ref:t}};else if(y(t))e={asset:t};else if(b(t))e={asset:{_ref:t._id||""}};else if(v(t))e={asset:{_ref:w(t.asset.url)}};else{if("object"!=typeof t.asset)return null;e=t}return t.crop&&(e.crop=t.crop),t.hotspot&&(e.hotspot=t.hotspot),(t=e).crop&&t.hotspot||((t=g({},t)).crop||(t.crop={left:0,top:0,bottom:0,right:0}),t.hotspot)||(t.hotspot={x:.5,y:.5,height:1,width:1}),t})(d));return d?(e=(t=>{var e=(r=t.split("-"))[1],n=r[2],r=r[3];if(e&&n&&r){var n=n.split("x"),i=+n[0],n=+n[1];if(isFinite(i)&&isFinite(n))return{id:e,width:i,height:n,format:r}}throw new Error("Malformed asset _ref '"+t+"'. Expected an id like \""+m+'".')})(d.asset._ref||d.asset._id||""),a={left:a=Math.round(d.crop.left*e.width),top:p=Math.round(d.crop.top*e.height),width:Math.round(e.width-d.crop.right*e.width-a),height:Math.round(e.height-d.crop.bottom*e.height-p)},p=d.hotspot.height*e.height/2,o=d.hotspot.width*e.width/2,s={left:(s=d.hotspot.x*e.width)-o,top:(d=d.hotspot.y*e.height)-p,right:s+o,bottom:d+p},t.rect||t.focalPoint||t.ignoreImageParams||t.crop||(t=g(g({},t),(o={crop:a,hotspot:s},p=(d=t).width,d=d.height,p&&d?(a=o.hotspot,{width:p,height:d,rect:(s=o.crop).width/s.height>(l=p/d)?(f=s.height,h=s.top,(c=(a.right-a.left)/2+a.left-(u=f*l)/2)<s.left?c=s.left:c+u>s.left+s.width&&(c=s.left+s.width-u),{left:Math.round(c),top:Math.round(h),width:Math.round(u),height:Math.round(f)}):(c=s.width,h=s.left,(f=(a.bottom-a.top)/2+a.top-(u=c/l)/2)<s.top?f=s.top:f+u>s.top+s.height&&(f=s.top+s.height-u),{left:Math.max(0,Math.floor(h)),top:Math.max(0,Math.floor(f)),width:Math.round(c),height:Math.round(u)})}):{width:p,height:d,rect:o.crop}))),n=g(g({},t),{},{asset:e}),a=n.baseUrl||"https://cdn.sanity.io",l=n.asset.id+"-"+n.asset.width+"x"+n.asset.height+"."+n.asset.format,a=a+"/images/"+n.projectId+"/"+n.dataset+"/"+l,r=[],n.rect&&(i=(l=n.rect).left,s=l.top,h=l.width,l=l.height,0!==i||0!==s||l!==n.asset.height||h!==n.asset.width)&&r.push("rect="+i+","+s+","+h+","+l),n.bg&&r.push("bg="+n.bg),n.focalPoint&&(r.push("fp-x="+n.focalPoint.x),r.push("fp-y="+n.focalPoint.y)),(i=[n.flipHorizontal&&"h",n.flipVertical&&"v"].filter(Boolean).join(""))&&r.push("flip="+i),k.forEach(function(t){var e=t[0],t=t[1];void 0!==n[e]?r.push(t+"="+encodeURIComponent(n[e])):void 0!==n[t]&&r.push(t+"="+encodeURIComponent(n[t]))}),0===r.length?a:a+"?"+r.join("&")):null}var e=["clip","crop","fill","fillmax","max","scale","min"],r=["top","bottom","left","right","center","focalpoint","entropy"],o=["format"];(s=l.prototype).withOptions=function(t){var e,n=t.baseUrl||this.options.baseUrl,r={baseUrl:n};for(e in t)t.hasOwnProperty(e)&&(r[(t=>{for(var e=i(k);!(n=e()).done;){var n=n.value,r=n[0];if(t===r||t===n[1])return r}return t})(e)]=t[e]);return new l(this,g({baseUrl:n},r))},s.image=function(t){return this.withOptions({source:t})},s.dataset=function(t){return this.withOptions({dataset:t})},s.projectId=function(t){return this.withOptions({projectId:t})},s.bg=function(t){return this.withOptions({bg:t})},s.dpr=function(t){return this.withOptions({dpr:t})},s.width=function(t){return this.withOptions({width:t})},s.height=function(t){return this.withOptions({height:t})},s.focalPoint=function(t,e){return this.withOptions({focalPoint:{x:t,y:e}})},s.maxWidth=function(t){return this.withOptions({maxWidth:t})},s.minWidth=function(t){return this.withOptions({minWidth:t})},s.maxHeight=function(t){return this.withOptions({maxHeight:t})},s.minHeight=function(t){return this.withOptions({minHeight:t})},s.size=function(t,e){return this.withOptions({width:t,height:e})},s.blur=function(t){return this.withOptions({blur:t})},s.sharpen=function(t){return this.withOptions({sharpen:t})},s.rect=function(t,e,n,r){return this.withOptions({rect:{left:t,top:e,width:n,height:r}})},s.format=function(t){return this.withOptions({format:t})},s.invert=function(t){return this.withOptions({invert:t})},s.orientation=function(t){return this.withOptions({orientation:t})},s.quality=function(t){return this.withOptions({quality:t})},s.forceDownload=function(t){return this.withOptions({download:t})},s.flipHorizontal=function(){return this.withOptions({flipHorizontal:!0})},s.flipVertical=function(){return this.withOptions({flipVertical:!0})},s.ignoreImageParams=function(){return this.withOptions({ignoreImageParams:!0})},s.fit=function(t){if(-1===e.indexOf(t))throw new Error('Invalid fit mode "'+t+'"');return this.withOptions({fit:t})},s.crop=function(t){if(-1===r.indexOf(t))throw new Error('Invalid crop mode "'+t+'"');return this.withOptions({crop:t})},s.saturation=function(t){return this.withOptions({saturation:t})},s.auto=function(t){if(-1===o.indexOf(t))throw new Error('Invalid auto mode "'+t+'"');return this.withOptions({auto:t})},s.pad=function(t){return this.withOptions({pad:t})},s.url=function(){return t(this.options)},s.toString=function(){return this.url()};var s,a=l;function l(t,e){this.options=g(t?g({},t.options||{}):{},e||{})}return function(t){var e,n,r=t;return r&&"object"==typeof r.clientConfig?(e=(r=r.clientConfig).apiHost,n=r.projectId,r=r.dataset,new a(null,{baseUrl:(e||"https://api.sanity.io").replace(/^https:\/\/api\./,"https://cdn."),projectId:n,dataset:r})):new a(null,t)}},"object"==typeof n&&void 0!==e?e.exports=i():(r=r||self).SanityImageUrlBuilder=i()},{}],15:[function(t,e,n){var l=Object.getOwnPropertySymbols,c=Object.prototype.hasOwnProperty,u=Object.prototype.propertyIsEnumerable;e.exports=(()=>{try{if(Object.assign){var t=new String("abc");if(t[5]="de","5"!==Object.getOwnPropertyNames(t)[0]){for(var e,n={},r=0;r<10;r++)n["_"+String.fromCharCode(r)]=r;if("0123456789"===Object.getOwnPropertyNames(n).map(function(t){return n[t]}).join(""))return e={},"abcdefghijklmnopqrst".split("").forEach(function(t){e[t]=t}),"abcdefghijklmnopqrst"===Object.keys(Object.assign({},e)).join("")}}}catch(t){}})()?Object.assign:function(t,e){for(var n,r=(t=>{if(null==t)throw new TypeError("Object.assign cannot be called with null or undefined");return Object(t)})(t),i=1;i<arguments.length;i++){for(var o in n=Object(arguments[i]))c.call(n,o)&&(r[o]=n[o]);if(l)for(var s=l(n),a=0;a<s.length;a++)u.call(n,s[a])&&(r[s[a]]=n[s[a]])}return r}},{}]},{},[1])(1)});