jsx-slack
Version:
Build JSON object for Slack Block Kit surfaces from JSX
5 lines (4 loc) • 27.6 kB
JavaScript
var Z=-1,j=0,C=1,M=2,F=3,P=4,H=5,z=6,tt=7,et=8,nt=typeof self=="object"?self:globalThis,Yt=(t,n)=>{const e=(r,o)=>(t.set(o,r),r),i=r=>{if(t.has(r))return t.get(r);const[o,l]=n[r];switch(o){case j:case Z:return e(l,r);case C:{const c=e([],r);for(const a of l)c.push(i(a));return c}case M:{const c=e({},r);for(const[a,u]of l)c[i(a)]=i(u);return c}case F:return e(new Date(l),r);case P:{const{source:c,flags:a}=l;return e(new RegExp(c,a),r)}case H:{const c=e(new Map,r);for(const[a,u]of l)c.set(i(a),i(u));return c}case z:{const c=e(new Set,r);for(const a of l)c.add(i(a));return c}case tt:{const{name:c,message:a}=l;return e(new nt[c](a),r)}case et:return e(BigInt(l),r);case"BigInt":return e(Object(BigInt(l)),r)}return e(new nt[o](l),r)};return i},rt=t=>Yt(new Map,t)(0),S="",{toString:_t}={},{keys:Gt}=Object,k=t=>{const n=typeof t;if(n!=="object"||!t)return[j,n];const e=_t.call(t).slice(8,-1);switch(e){case"Array":return[C,S];case"Object":return[M,S];case"Date":return[F,S];case"RegExp":return[P,S];case"Map":return[H,S];case"Set":return[z,S]}return e.includes("Array")?[C,e]:e.includes("Error")?[tt,e]:[M,e]},B=([t,n])=>t===j&&(n==="function"||n==="symbol"),Kt=(t,n,e,i)=>{const r=(l,c)=>{const a=i.push(l)-1;return e.set(c,a),a},o=l=>{if(e.has(l))return e.get(l);let[c,a]=k(l);switch(c){case j:{let s=l;switch(a){case"bigint":c=et,s=l.toString();break;case"function":case"symbol":if(t)throw new TypeError("unable to serialize "+a);s=null;break;case"undefined":return r([Z],l)}return r([c,s],l)}case C:{if(a)return r([a,[...l]],l);const s=[],h=r([c,s],l);for(const d of l)s.push(o(d));return h}case M:{if(a)switch(a){case"BigInt":return r([a,l.toString()],l);case"Boolean":case"Number":case"String":return r([a,l.valueOf()],l)}if(n&&"toJSON"in l)return o(l.toJSON());const s=[],h=r([c,s],l);for(const d of Gt(l))(t||!B(k(l[d])))&&s.push([o(d),o(l[d])]);return h}case F:return r([c,l.toISOString()],l);case P:{const{source:s,flags:h}=l;return r([c,{source:s,flags:h}],l)}case H:{const s=[],h=r([c,s],l);for(const[d,m]of l)(t||!(B(k(d))||B(k(m))))&&s.push([o(d),o(m)]);return h}case z:{const s=[],h=r([c,s],l);for(const d of l)(t||!B(k(d)))&&s.push(o(d));return h}}const{message:u}=l;return r([c,{name:a,message:u}],l)};return o},it=(t,{json:n,lossy:e}={})=>{const i=[];return Kt(!(n||e),!!n,new Map,i)(t),i},ot=typeof structuredClone=="function"?(t,n)=>n&&("json"in n||"lossy"in n)?rt(it(t,n)):structuredClone(t):(t,n)=>rt(it(t,n)),D=function(t,n,e,i,r){const o=v(n);return lt(t)?o.call(r,t,e,i):!1},v=function(t){if(t==null)return Zt;if(typeof t=="string")return Xt(t);if(typeof t=="object")return Qt(t);if(typeof t=="function")return J(t);throw new Error("Expected function, string, or array as `test`")};function Qt(t){const n=[];let e=-1;for(;++e<t.length;)n[e]=v(t[e]);return J(i);function i(...r){let o=-1;for(;++o<n.length;)if(n[o].apply(this,r))return!0;return!1}}function Xt(t){return J(n);function n(e){return e.tagName===t}}function J(t){return n;function n(e,i,r){return!!(lt(e)&&t.call(this,e,typeof i=="number"?i:void 0,r||void 0))}}function Zt(t){return!!(t&&typeof t=="object"&&"type"in t&&t.type==="element"&&"tagName"in t&&typeof t.tagName=="string")}function lt(t){return t!==null&&typeof t=="object"&&"type"in t&&"tagName"in t}var ct=v(function(t){return t.tagName==="audio"||t.tagName==="canvas"||t.tagName==="embed"||t.tagName==="iframe"||t.tagName==="img"||t.tagName==="math"||t.tagName==="object"||t.tagName==="picture"||t.tagName==="svg"||t.tagName==="video"}),te=/[ \t\n\f\r]/g;function at(t){return typeof t=="object"?t.type==="text"?st(t.value):!1:st(t)}function st(t){return t.replace(te,"")===""}var A=function(t){if(t==null)return ie;if(typeof t=="function")return E(t);if(typeof t=="object")return Array.isArray(t)?ee(t):ne(t);if(typeof t=="string")return re(t);throw new Error("Expected function, string, or object as test")};function ee(t){const n=[];let e=-1;for(;++e<t.length;)n[e]=A(t[e]);return E(i);function i(...r){let o=-1;for(;++o<n.length;)if(n[o].apply(this,r))return!0;return!1}}function ne(t){const n=t;return E(e);function e(i){const r=i;let o;for(o in t)if(r[o]!==n[o])return!1;return!0}}function re(t){return E(n);function n(e){return e&&e.type===t}}function E(t){return n;function n(e,i,r){return!!(oe(e)&&t.call(this,e,typeof i=="number"?i:void 0,r||void 0))}}function ie(){return!0}function oe(t){return t!==null&&typeof t=="object"&&"type"in t}var le=["address","article","aside","blockquote","body","br","caption","center","col","colgroup","dd","dialog","dir","div","dl","dt","figcaption","figure","footer","form","h1","h2","h3","h4","h5","h6","head","header","hgroup","hr","html","legend","li","li","listing","main","menu","nav","ol","optgroup","option","p","plaintext","pre","section","summary","table","tbody","td","td","tfoot","th","th","thead","tr","ul","wbr","xmp"],ce=["button","input","select","textarea"],ae=["area","base","basefont","dialog","datalist","head","link","meta","noembed","noframes","param","rp","script","source","style","template","track","title"],se={},L=A(["doctype","comment"]);function ue(t){const n=ye((t||se).newlines?ge:me);return function(e){ut(e,{collapse:n,whitespace:"normal"})}}function ut(t,n){if("children"in t){const e={...n};return(t.type==="root"||ht(t))&&(e.before=!0,e.after=!0),e.whitespace=be(t,n),fe(t,e)}if(t.type==="text"){if(n.whitespace==="normal")return pe(t,n);n.whitespace==="nowrap"&&(t.value=n.collapse(t.value))}return{ignore:L(t),stripAtStart:!1,remove:!1}}function pe(t,n){const e=n.collapse(t.value),i={ignore:!1,stripAtStart:!1,remove:!1};let r=0,o=e.length;return n.before&&dt(e.charAt(0))&&r++,r!==o&&dt(e.charAt(o-1))&&(n.after?o--:i.stripAtStart=!0),r===o?i.remove=!0:t.value=e.slice(r,o),i}function fe(t,n){let e=n.before;const i=n.after,r=t.children;let o=r.length,l=-1;for(;++l<o;){const c=ut(r[l],{...n,after:pt(r,l,i),before:e});c.remove?(r.splice(l,1),l--,o--):c.ignore||(e=c.stripAtStart),ft(r[l])&&(e=!1)}return{ignore:!1,stripAtStart:!!(e||i),remove:!1}}function pt(t,n,e){for(;++n<t.length;){const i=t[n];let r=he(i);if(r===void 0&&"children"in i&&!de(i)&&(r=pt(i.children,-1)),typeof r=="boolean")return r}return e}function he(t){if(t.type==="element"){if(ft(t))return!1;if(ht(t))return!0}else if(t.type==="text"){if(!at(t))return!1}else if(!L(t))return!1}function ft(t){return ct(t)||D(t,ce)}function ht(t){return D(t,le)}function de(t){return!!(t.type==="element"&&t.properties.hidden)||L(t)||D(t,ae)}function dt(t){return t===" "||t===`
`}function ge(t){const n=/\r?\n|\r/.exec(t);return n?n[0]:" "}function me(){return" "}function ye(t){return n;function n(e){return String(e).replace(/[\t\n\v\f\r ]+/g,t)}}function be(t,n){if("tagName"in t&&t.properties)switch(t.tagName){case"listing":case"plaintext":case"script":case"style":case"xmp":return"pre";case"nobr":return"nowrap";case"pre":return t.properties.wrap?"pre-wrap":"pre";case"td":case"th":return t.properties.noWrap?"nowrap":n.whitespace;case"textarea":return"pre-wrap"}return n.whitespace}var gt=[],ve=!0,W=!1,mt="skip";function xe(t,n,e,i){let r;typeof n=="function"&&typeof e!="function"?(i=e,e=n):r=n;const o=A(r),l=i?-1:1;c(t,void 0,[])();function c(a,u,s){const h=a&&typeof a=="object"?a:{};if(typeof h.type=="string"){const m=typeof h.tagName=="string"?h.tagName:typeof h.name=="string"?h.name:void 0;Object.defineProperty(d,"name",{value:"node ("+(a.type+(m?"<"+m+">":""))+")"})}return d;function d(){let m=gt,b,y,w;if((!n||o(a,u,s[s.length-1]||void 0))&&(m=we(e(a,s)),m[0]===W))return m;if("children"in a&&a.children){const x=a;if(x.children&&m[0]!==mt)for(y=(i?x.children.length:-1)+l,w=s.concat(x);y>-1&&y<x.children.length;){const Vt=x.children[y];if(b=c(Vt,y,w)(),b[0]===W)return b;y=typeof b[1]=="number"?b[1]:y+l}}return m}}}function we(t){return Array.isArray(t)?t:typeof t=="number"?[ve,t]:t==null?gt:[t]}function U(t,n,e,i){let r,o,l;typeof n=="function"&&typeof e!="function"?(o=void 0,l=n,r=e):(o=n,l=e,r=i),xe(t,o,c,r);function c(a,u){const s=u[u.length-1],h=s?s.children.indexOf(a):void 0;return l(a,h,s)}}var Se=yt("end"),Ne=yt("start");function yt(t){return n;function n(e){const i=e&&e.position&&e.position[t]||{};if(typeof i.line=="number"&&i.line>0&&typeof i.column=="number"&&i.column>0)return{line:i.line,column:i.column,offset:typeof i.offset=="number"&&i.offset>-1?i.offset:void 0}}}function ke(t){const n=Ne(t),e=Se(t);if(n&&e)return{start:n,end:e}}function Ae(t,n){const e=n.properties||{},i=t.all(n),r={type:"link",url:t.resolve(String(e.href||"")||null),title:e.title?String(e.title):null,children:i};return t.patch(n,r),r}function Te(t,n){t.baseFound||(t.frozenBaseUrl=String(n.properties&&n.properties.href||"")||void 0,t.baseFound=!0)}function Ie(t,n){const e={type:"blockquote",children:t.toFlow(t.all(n))};return t.patch(n,e),e}function je(t,n){const e={type:"break"};return t.patch(n,e),e}var bt=function(t,n,e){const i=A(e);if(!t||!t.type||!t.children)throw new Error("Expected parent node");if(typeof n=="number"){if(n<0||n===Number.POSITIVE_INFINITY)throw new Error("Expected positive finite number as index")}else if(n=t.children.indexOf(n),n<0)throw new Error("Expected child node or index");for(;++n<t.children.length;)if(i(t.children[n],n,t))return t.children[n]},vt=/\n/g,xt=/[\t ]+/g,$=v("br"),wt=v(Re),Ce=v("p"),St=v("tr"),Me=v(["datalist","head","noembed","noframes","noscript","rp","script","style","template","title",Oe,qe]),Nt=v(["address","article","aside","blockquote","body","caption","center","dd","dialog","dir","dl","dt","div","figure","figcaption","footer","form,","h1","h2","h3","h4","h5","h6","header","hgroup","hr","html","legend","listing","main","menu","nav","ol","p","plaintext","pre","section","ul","xmp"]);function T(t,n={}){const e="children"in t?t.children:[],i=Nt(t),r=Tt(t,{whitespace:n.whitespace||"normal",breakBefore:!1,breakAfter:!1}),o=[];(t.type==="text"||t.type==="comment")&&o.push(...At(t,{whitespace:r,breakBefore:!0,breakAfter:!0}));let l=-1;for(;++l<e.length;)o.push(...kt(e[l],t,{whitespace:r,breakBefore:l?void 0:i,breakAfter:l<e.length-1?$(e[l+1]):i}));const c=[];let a;for(l=-1;++l<o.length;){const u=o[l];typeof u=="number"?a!==void 0&&u>a&&(a=u):u&&(a!==void 0&&a>-1&&c.push(`
`.repeat(a)||" "),a=-1,c.push(u))}return c.join("")}function kt(t,n,e){return t.type==="element"?Be(t,n,e):t.type==="text"?e.whitespace==="normal"?At(t,e):Ee(t):[]}function Be(t,n,e){const i=Tt(t,e),r=t.children||[];let o=-1,l=[];if(Me(t))return l;let c,a;for($(t)||St(t)&&bt(n,t,St)?a=`
`:Ce(t)?(c=2,a=2):Nt(t)&&(c=1,a=1);++o<r.length;)l=l.concat(kt(r[o],t,{whitespace:i,breakBefore:o?void 0:c,breakAfter:o<r.length-1?$(r[o+1]):a}));return wt(t)&&bt(n,t,wt)&&l.push(" "),c&&l.unshift(c),a&&l.push(a),l}function At(t,n){const e=String(t.value),i=[],r=[];let o=0;for(;o<=e.length;){vt.lastIndex=o;const a=vt.exec(e),u=a&&"index"in a?a.index:e.length;i.push(Ue(e.slice(o,u).replace(/[\u061C\u200E\u200F\u202A-\u202E\u2066-\u2069]/g,""),o===0?n.breakBefore:!0,u===e.length?n.breakAfter:!0)),o=u+1}let l=-1,c;for(;++l<i.length;)i[l].charCodeAt(i[l].length-1)===8203||l<i.length-1&&i[l+1].charCodeAt(0)===8203?(r.push(i[l]),c=void 0):i[l]?(typeof c=="number"&&r.push(c),r.push(i[l]),c=0):(l===0||l===i.length-1)&&r.push(0);return r}function Ee(t){return[String(t.value)]}function Ue(t,n,e){const i=[];let r=0,o;for(;r<t.length;){xt.lastIndex=r;const l=xt.exec(t);o=l?l.index:t.length,!r&&!o&&l&&!n&&i.push(""),r!==o&&i.push(t.slice(r,o)),r=l?o+l[0].length:o}return r!==o&&!e&&i.push(""),i.join(" ")}function Tt(t,n){if(t.type==="element"){const e=t.properties||{};switch(t.tagName){case"listing":case"plaintext":case"xmp":return"pre";case"nobr":return"nowrap";case"pre":return e.wrap?"pre-wrap":"pre";case"td":case"th":return e.noWrap?"nowrap":n.whitespace;case"textarea":return"pre-wrap"}}return n.whitespace}function Oe(t){return!!(t.properties||{}).hidden}function Re(t){return t.tagName==="td"||t.tagName==="th"}function qe(t){return t.tagName==="dialog"&&!(t.properties||{}).open}function Fe(t){const n=String(t);let e=n.length;for(;e>0;){const i=n.codePointAt(e-1);if(i!==void 0&&(i===10||i===13))e--;else break}return n.slice(0,e)}var V="language-";function O(t,n){const e=n.children;let i=-1,r,o;if(n.tagName==="pre")for(;++i<e.length;){const c=e[i];if(c.type==="element"&&c.tagName==="code"&&c.properties&&c.properties.className&&Array.isArray(c.properties.className)){r=c.properties.className;break}}if(r){for(i=-1;++i<r.length;)if(String(r[i]).slice(0,V.length)===V){o=String(r[i]).slice(V.length);break}}const l={type:"code",lang:o||null,meta:null,value:Fe(T(n))};return t.patch(n,l),l}function Pe(t,n){const e={type:"html",value:"<!--"+n.value+"-->"};return t.patch(n,e),e}function Y(t,n){const e={type:"delete",children:t.all(n)};return t.patch(n,e),e}function _(t){let n=-1;if(t.length>1){for(;++n<t.length;)if(t[n].spread)return!0}return!1}function He(t,n){const e=[],i=[];let r=-1;for(;++r<n.children.length;){const c=n.children[r];c.type==="element"&&c.tagName==="div"?e.push(...c.children):e.push(c)}let o={definitions:[],titles:[]};for(r=-1;++r<e.length;){const c=e[r];if(c.type==="element"&&c.tagName==="dt"){const a=e[r-1];a&&a.type==="element"&&a.tagName==="dd"&&(i.push(o),o={definitions:[],titles:[]}),o.titles.push(c)}else o.definitions.push(c)}i.push(o),r=-1;const l=[];for(;++r<i.length;){const c=[...It(t,i[r].titles),...It(t,i[r].definitions)];c.length>0&&l.push({type:"listItem",spread:c.length>1,checked:null,children:c})}if(l.length>0){const c={type:"list",ordered:!1,start:null,spread:_(l),children:l};return t.patch(n,c),c}}function It(t,n){const e=t.all({type:"root",children:n}),i=t.toSpecificContent(e,ze);return i.length===0?[]:i.length===1?i[0].children:[{type:"list",ordered:!1,start:null,spread:_(i),children:i}]}function ze(){return{type:"listItem",spread:!1,checked:null,children:[]}}function R(t,n){const e={type:"emphasis",children:t.all(n)};return t.patch(n,e),e}function N(t,n){const e=Number(n.tagName.charAt(1))||1,i=t.all(n),r={type:"heading",depth:e,children:i};return t.patch(n,r),r}function De(t,n){const e={type:"thematicBreak"};return t.patch(n,e),e}function Je(t,n){const e=n.properties||{},i=String(e.src||""),r=String(e.title||"");if(i&&r){const o={type:"link",title:null,url:t.resolve(i),children:[{type:"text",value:r}]};return t.patch(n,o),o}}function jt(t,n){const e=n.properties||{},i={type:"image",url:t.resolve(String(e.src||"")||null),title:e.title?String(e.title):null,alt:e.alt?String(e.alt):""};return t.patch(n,i),i}function I(t,n){const e={type:"inlineCode",value:T(n)};return t.patch(n,e),e}function Ct(t,n){const e=[],i=[],r=n||t.properties||{},o=Mt(t),l=Math.min(Number.parseInt(String(r.size),10),0)||(r.multiple?4:1);let c=-1;for(;++c<o.length;){const s=o[c];s&&s.properties&&s.properties.selected&&e.push(s)}const a=e.length>0?e:o,u=a.length>l?l:a.length;for(c=-1;++c<u;){const s=a[c],h=s.properties||{},d=T(s),m=d||String(h.label||""),b=String(h.value||"")||d;i.push([b,m===b?void 0:m])}return i}function Mt(t){const n=[];let e=-1;for(;++e<t.children.length;){const i=t.children[e];"children"in i&&Array.isArray(i.children)&&n.push(...Mt(i)),i.type==="element"&&i.tagName==="option"&&(!i.properties||!i.properties.disabled)&&n.push(i)}return n}var Le="[x]",We="[ ]";function $e(t,n){const e=n.properties||{},i=String(e.value||e.placeholder||"");if(e.disabled||e.type==="hidden"||e.type==="file")return;if(e.type==="checkbox"||e.type==="radio"){const a={type:"text",value:e.checked?t.options.checked||Le:t.options.unchecked||We};return t.patch(n,a),a}if(e.type==="image"){const a=e.alt||i;if(a){const u={type:"image",url:t.resolve(String(e.src||"")||null),title:String(e.title||"")||null,alt:String(a)};return t.patch(n,u),u}return}let r=[];if(i)r=[[i,void 0]];else if(e.type!=="button"&&e.type!=="file"&&e.type!=="password"&&e.type!=="reset"&&e.type!=="submit"&&e.list){const a=String(e.list),u=t.elementById.get(a);u&&u.tagName==="datalist"&&(r=Ct(u,e))}if(r.length===0)return;if(e.type==="password"&&(r[0]=["\u2022".repeat(r[0][0].length),void 0]),e.type==="email"||e.type==="url"){const a=[];let u=-1;for(;++u<r.length;){const s=t.resolve(r[u][0]),h={type:"link",title:null,url:e.type==="email"?"mailto:"+s:s,children:[{type:"text",value:r[u][1]||s}]};a.push(h),u!==r.length-1&&a.push({type:"text",value:", "})}return a}const o=[];let l=-1;for(;++l<r.length;)o.push(r[l][1]?r[l][1]+" ("+r[l][0]+")":r[l][0]);const c={type:"text",value:o.join(", ")};return t.patch(n,c),c}var Ve={}.hasOwnProperty;function Ye(t,n){const e=t.type==="element"&&Ve.call(t.properties,n)&&t.properties[n];return e!=null&&e!==!1}var _e=new Set(["pingback","prefetch","stylesheet"]);function Ge(t){if(t.type!=="element"||t.tagName!=="link")return!1;if(t.properties.itemProp)return!0;const n=t.properties.rel;let e=-1;if(!Array.isArray(n)||n.length===0)return!1;for(;++e<n.length;)if(!_e.has(String(n[e])))return!1;return!0}var Ke=v(["a","abbr","area","b","bdi","bdo","br","button","cite","code","data","datalist","del","dfn","em","i","input","ins","kbd","keygen","label","map","mark","meter","noscript","output","progress","q","ruby","s","samp","script","select","small","span","strong","sub","sup","template","textarea","time","u","var","wbr"]),Qe=v("meta");function Bt(t){return!!(t.type==="text"||Ke(t)||ct(t)||Ge(t)||Qe(t)&&Ye(t,"itemProp"))}function G(t,n){const{rest:e,checkbox:i}=Ut(n),r=i?!!i.properties.checked:null,o=Et(e),l=t.toFlow(t.all(e)),c={type:"listItem",spread:o,checked:r,children:l};return t.patch(n,c),c}function Et(t){let n=-1,e=!1;for(;++n<t.children.length;){const i=t.children[n];if(i.type==="element"){if(Bt(i))continue;if(i.tagName==="p"||e||Et(i))return!0;e=!0}}return!1}function Ut(t){const n=t.children[0];if(n&&n.type==="element"&&n.tagName==="input"&&n.properties&&(n.properties.type==="checkbox"||n.properties.type==="radio")){const e={...t,children:t.children.slice(1)};return{checkbox:n,rest:e}}if(n&&n.type==="element"&&n.tagName==="p"){const{checkbox:e,rest:i}=Ut(n);if(e){const r={...t,children:[i,...t.children.slice(1)]};return{checkbox:e,rest:r}}}return{checkbox:void 0,rest:t}}function K(t,n){const e=n.tagName==="ol",i=t.toSpecificContent(t.all(n),Xe);let r=null;e&&(r=n.properties&&n.properties.start?Number.parseInt(String(n.properties.start),10):1);const o={type:"list",ordered:e,start:r,spread:_(i),children:i};return t.patch(n,o),o}function Xe(){return{type:"listItem",spread:!1,checked:null,children:[]}}var Ze={};function tn(t,n){const e=Ze,i=typeof e.includeImageAlt=="boolean"?e.includeImageAlt:!0,r=typeof e.includeHtml=="boolean"?e.includeHtml:!0;return Ot(t,i,r)}function Ot(t,n,e){if(en(t)){if("value"in t)return t.type==="html"&&!e?"":t.value;if(n&&"alt"in t&&t.alt)return t.alt;if("children"in t)return Rt(t.children,n,e)}return Array.isArray(t)?Rt(t,n,e):""}function Rt(t,n,e){const i=[];let r=-1;for(;++r<t.length;)i[r]=Ot(t[r],n,e);return i.join("")}function en(t){return!!(t&&typeof t=="object")}var nn=A(["break","delete","emphasis","footnote","footnoteReference","image","imageReference","inlineCode","inlineMath","link","linkReference","mdxJsxTextElement","mdxTextExpression","strong","text","textDirective"]);function q(t){let n=-1;for(;++n<t.length;){const e=t[n];if(!Pt(e)||"children"in e&&q(e.children))return!0}return!1}function qt(t){return Ft(t,n,function(e){return e});function n(e){return e.every(function(i){return i.type==="text"?at(i.value):!1})?[]:[{type:"paragraph",children:e}]}}function rn(t){return Ft(t.children,n,e);function n(i){const r=Q(t);return r.children=i,[r]}function e(i){if("children"in i&&"children"in t){const r=Q(t),o=Q(i);return r.children=i.children,o.children.push(r),o}return{...i}}}function Ft(t,n,e){const i=on(t),r=[];let o=[],l=-1;for(;++l<i.length;){const c=i[l];Pt(c)?o.push(c):(o.length>0&&(r.push(...n(o)),o=[]),r.push(e(c)))}return o.length>0&&(r.push(...n(o)),o=[]),r}function on(t){const n=[];let e=-1;for(;++e<t.length;){const i=t[e];(i.type==="delete"||i.type==="link")&&q(i.children)?n.push(...rn(i)):n.push(i)}return n}function Pt(t){const n=t.data&&t.data.hName;return n?Bt({type:"element",tagName:n,properties:{},children:[]}):nn(t)}function Q(t){return ot({...t,children:[]})}function Ht(t,n){const e=n.properties||{},i=n.tagName==="video"?String(e.poster||""):"";let r=String(e.src||""),o=-1,l=!1,c=t.all(n);if(U({type:"root",children:c},function(s){if(s.type==="link")return l=!0,W}),l||q(c))return c;for(;!r&&++o<n.children.length;){const s=n.children[o];s.type==="element"&&s.tagName==="source"&&s.properties&&(r=String(s.properties.src||""))}if(i){const s={type:"image",title:null,url:t.resolve(i),alt:tn(c)};t.patch(n,s),c=[s]}const a=c,u={type:"link",title:e.title?String(e.title):null,url:t.resolve(r),children:a};return t.patch(n,u),u}function zt(t,n){const e=t.all(n);if(e.length>0){const i={type:"paragraph",children:e};return t.patch(n,i),i}}var ln=['"'];function cn(t,n){const e=t.options.quotes||ln;t.qNesting++;const i=t.all(n);t.qNesting--;const r=e[t.qNesting%e.length],o=i[0],l=i[i.length-1],c=r.charAt(0),a=r.length>1?r.charAt(1):r;return o&&o.type==="text"?o.value=c+o.value:i.unshift({type:"text",value:c}),l&&l.type==="text"?l.value+=a:i.push({type:"text",value:a}),i}function an(t,n){let e=t.all(n);(t.options.document||q(e))&&(e=qt(e));const i={type:"root",children:e};return t.patch(n,i),i}function sn(t,n){const e=Ct(n);let i=-1;const r=[];for(;++i<e.length;){const o=e[i];r.push(o[1]?o[1]+" ("+o[0]+")":o[0])}if(r.length>0){const o={type:"text",value:r.join(", ")};return t.patch(n,o),o}}function Dt(t,n){const e={type:"strong",children:t.all(n)};return t.patch(n,e),e}function Jt(t,n){const e={type:"tableCell",children:t.all(n)};if(t.patch(n,e),n.properties){const i=n.properties.rowSpan,r=n.properties.colSpan;if(i||r){const o=e.data||(e.data={});i&&(o.hastUtilToMdastTemporaryRowSpan=i),r&&(o.hastUtilToMdastTemporaryColSpan=r)}}return e}function un(t,n){const e={type:"tableRow",children:t.toSpecificContent(t.all(n),pn)};return t.patch(n,e),e}function pn(){return{type:"tableCell",children:[]}}function fn(t,n){if(t.inTable){const u={type:"text",value:T(n)};return t.patch(n,u),u}t.inTable=!0;const{align:e,headless:i}=hn(n),r=t.toSpecificContent(t.all(n),Lt);i&&r.unshift(Lt());let o=-1;for(;++o<r.length;){const u=r[o],s=t.toSpecificContent(u.children,dn);u.children=s}let l=1;for(o=-1;++o<r.length;){const u=r[o].children;let s=-1;for(;++s<u.length;){const h=u[s];if(h.data){const d=h.data,m=Number.parseInt(String(d.hastUtilToMdastTemporaryColSpan),10)||1,b=Number.parseInt(String(d.hastUtilToMdastTemporaryRowSpan),10)||1;if(m>1||b>1){let y=o-1;for(;++y<o+b;){let w=s-1;for(;++w<s+m&&r[y];){const x=[];(y!==o||w!==s)&&x.push({type:"tableCell",children:[]}),r[y].children.splice(w,0,...x)}}}"hastUtilToMdastTemporaryColSpan"in h.data&&delete h.data.hastUtilToMdastTemporaryColSpan,"hastUtilToMdastTemporaryRowSpan"in h.data&&delete h.data.hastUtilToMdastTemporaryRowSpan,Object.keys(h.data).length===0&&delete h.data}}u.length>l&&(l=u.length)}for(o=-1;++o<r.length;){const u=r[o].children;let s=u.length-1;for(;++s<l;)u.push({type:"tableCell",children:[]})}let c=e.length-1;for(;++c<l;)e.push(null);t.inTable=!1;const a={type:"table",align:e,children:r};return t.patch(n,a),a}function hn(t){const n={align:[null],headless:!0};let e=0,i=0;return U(t,function(r){if(r.type==="element"){if(r.tagName==="table"&&t!==r)return mt;if((r.tagName==="th"||r.tagName==="td")&&r.properties){if(!n.align[i]){const o=String(r.properties.align||"")||null;(o==="center"||o==="left"||o==="right"||o===null)&&(n.align[i]=o)}n.headless&&e<2&&r.tagName==="th"&&(n.headless=!1),i++}else r.tagName==="thead"?n.headless=!1:r.tagName==="tr"&&(e++,i=0)}}),n}function dn(){return{type:"tableCell",children:[]}}function Lt(){return{type:"tableRow",children:[]}}function gn(t,n){const e={type:"text",value:n.value};return t.patch(n,e),e}function mn(t,n){const e={type:"text",value:T(n)};return t.patch(n,e),e}function yn(t,n){const e={type:"text",value:"\u200B"};return t.patch(n,e),e}var Wt={comment:Pe,doctype:f,root:an,text:gn},X={applet:f,area:f,basefont:f,bgsound:f,caption:f,col:f,colgroup:f,command:f,content:f,datalist:f,dialog:f,element:f,embed:f,frame:f,frameset:f,isindex:f,keygen:f,link:f,math:f,menu:f,menuitem:f,meta:f,nextid:f,noembed:f,noframes:f,optgroup:f,option:f,param:f,script:f,shadow:f,source:f,spacer:f,style:f,svg:f,template:f,title:f,track:f,abbr:p,acronym:p,bdi:p,bdo:p,big:p,blink:p,button:p,canvas:p,cite:p,data:p,details:p,dfn:p,font:p,ins:p,label:p,map:p,marquee:p,meter:p,nobr:p,noscript:p,object:p,output:p,progress:p,rb:p,rbc:p,rp:p,rt:p,rtc:p,ruby:p,slot:p,small:p,span:p,sup:p,sub:p,tbody:p,tfoot:p,thead:p,time:p,address:g,article:g,aside:g,body:g,center:g,div:g,fieldset:g,figcaption:g,figure:g,form:g,footer:g,header:g,hgroup:g,html:g,legend:g,main:g,multicol:g,nav:g,picture:g,section:g,a:Ae,audio:Ht,b:Dt,base:Te,blockquote:Ie,br:je,code:I,dir:K,dl:He,dt:G,dd:G,del:Y,em:R,h1:N,h2:N,h3:N,h4:N,h5:N,h6:N,hr:De,i:R,iframe:Je,img:jt,image:jt,input:$e,kbd:I,li:G,listing:O,mark:R,ol:K,p:zt,plaintext:O,pre:O,q:cn,s:Y,samp:I,select:sn,strike:Y,strong:Dt,summary:zt,table:fn,td:Jt,textarea:mn,th:Jt,tr:un,tt:I,u:R,ul:K,var:I,video:Ht,wbr:yn,xmp:O};function p(t,n){return t.all(n)}function g(t,n){return t.toFlow(t.all(n))}function f(){}var $t={}.hasOwnProperty;function bn(t){return{all:vn,baseFound:!1,elementById:new Map,frozenBaseUrl:void 0,handlers:{...X,...t.handlers},inTable:!1,nodeHandlers:{...Wt,...t.nodeHandlers},one:xn,options:t,patch:wn,qNesting:0,resolve:Sn,toFlow:Nn,toSpecificContent:kn}}function vn(t){const n=t.children||[],e=[];let i=-1;for(;++i<n.length;){const l=n[i],c=this.one(l,t);Array.isArray(c)?e.push(...c):c&&e.push(c)}let r=0,o=e.length;for(;r<o&&e[r].type==="break";)r++;for(;o>r&&e[o-1].type==="break";)o--;return r===0&&o===e.length?e:e.slice(r,o)}function xn(t,n){if(t.type==="element"){if(t.properties&&t.properties.dataMdast==="ignore")return;if($t.call(this.handlers,t.tagName))return this.handlers[t.tagName](this,t,n)||void 0}else if($t.call(this.nodeHandlers,t.type))return this.nodeHandlers[t.type](this,t,n)||void 0;if("value"in t&&typeof t.value=="string"){const e={type:"text",value:t.value};return this.patch(t,e),e}if("children"in t)return this.all(t)}function wn(t,n){t.position&&(n.position=ke(t))}function Sn(t){const n=this.frozenBaseUrl;return t==null?"":n?String(new URL(t,n)):t}function Nn(t){return qt(t)}function kn(t,n){const e=n(),i=[];let r=[],o=-1;for(;++o<t.length;){const c=t[o];if(l(c))r.length>0&&(c.children.unshift(...r),r=[]),i.push(c);else{const a=c;r.push(a)}}if(r.length>0){let c=i[i.length-1];c||(c=n(),i.push(c)),c.children.push(...r),r=[]}return i;function l(c){return c.type===e.type}}var An={};function Tn(t,n){const e=ot(t),i=n||An,r=ue({newlines:i.newlines===!0}),o=bn(i);let l;r(e),U(e,function(a){if(a&&a.type==="element"&&a.properties){const u=String(a.properties.id||"")||void 0;u&&!o.elementById.has(u)&&o.elementById.set(u,a)}});const c=o.one(e,void 0);return c?Array.isArray(c)?l={type:"root",children:c}:l=c:l={type:"root",children:[]},U(l,function(a,u,s){if(a.type==="text"&&u!==void 0&&s){const h=s.children[u-1];if(h&&h.type===a.type)return h.value+=a.value,s.children.splice(u,1),h.position&&a.position&&(h.position.end=a.position.end),u-1;if(a.value=a.value.replace(/[\t ]*(\r?\n|\r)[\t ]*/,"$1"),s&&(s.type==="heading"||s.type==="paragraph"||s.type==="root")&&(u||(a.value=a.value.replace(/^[\t ]+/,"")),u===s.children.length-1&&(a.value=a.value.replace(/[\t ]+$/,""))),!a.value)return s.children.splice(u,1),u}}),l}var In=X.li,jn=X.textarea,Cn=Wt.root;export{Tn as hastUtilToMdast,In as hastUtilToMdastListItem,Cn as hastUtilToMdastRoot,jn as hastUtilToMdastTextarea};