UNPKG

jsx-slack

Version:

Build JSON object for Slack Block Kit surfaces from JSX

5 lines (4 loc) 27.8 kB
var Z=-1,j=0,E=1,C=2,F=3,P=4,H=5,z=6,tt=7,et=8,nt=typeof self=="object"?self:globalThis,_t=(t,n)=>{const e=(r,i)=>(t.set(i,r),r),o=r=>{if(t.has(r))return t.get(r);const[i,l]=n[r];switch(i){case j:case Z:return e(l,r);case E:{const a=e([],r);for(const c of l)a.push(o(c));return a}case C:{const a=e({},r);for(const[c,u]of l)a[o(c)]=o(u);return a}case F:return e(new Date(l),r);case P:{const{source:a,flags:c}=l;return e(new RegExp(a,c),r)}case H:{const a=e(new Map,r);for(const[c,u]of l)a.set(o(c),o(u));return a}case z:{const a=e(new Set,r);for(const c of l)a.add(o(c));return a}case tt:{const{name:a,message:c}=l;return e(new nt[a](c),r)}case et:return e(BigInt(l),r);case"BigInt":return e(Object(BigInt(l)),r)}return e(new nt[i](l),r)};return o},rt=t=>_t(new Map,t)(0),N="",{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[E,N];case"Object":return[C,N];case"Date":return[F,N];case"RegExp":return[P,N];case"Map":return[H,N];case"Set":return[z,N]}return e.includes("Array")?[E,e]:e.includes("Error")?[tt,e]:[C,e]},B=([t,n])=>t===j&&(n==="function"||n==="symbol"),Kt=(t,n,e,o)=>{const r=(l,a)=>{const c=o.push(l)-1;return e.set(a,c),c},i=l=>{if(e.has(l))return e.get(l);let[a,c]=k(l);switch(a){case j:{let s=l;switch(c){case"bigint":a=et,s=l.toString();break;case"function":case"symbol":if(t)throw new TypeError("unable to serialize "+c);s=null;break;case"undefined":return r([Z],l)}return r([a,s],l)}case E:{if(c)return r([c,[...l]],l);const s=[],h=r([a,s],l);for(const d of l)s.push(i(d));return h}case C:{if(c)switch(c){case"BigInt":return r([c,l.toString()],l);case"Boolean":case"Number":case"String":return r([c,l.valueOf()],l)}if(n&&"toJSON"in l)return i(l.toJSON());const s=[],h=r([a,s],l);for(const d of Gt(l))(t||!B(k(l[d])))&&s.push([i(d),i(l[d])]);return h}case F:return r([a,l.toISOString()],l);case P:{const{source:s,flags:h}=l;return r([a,{source:s,flags:h}],l)}case H:{const s=[],h=r([a,s],l);for(const[d,m]of l)(t||!(B(k(d))||B(k(m))))&&s.push([i(d),i(m)]);return h}case z:{const s=[],h=r([a,s],l);for(const d of l)(t||!B(k(d)))&&s.push(i(d));return h}}const{message:u}=l;return r([a,{name:c,message:u}],l)};return i},ot=(t,{json:n,lossy:e}={})=>{const o=[];return Kt(!(n||e),!!n,new Map,o)(t),o},it=typeof structuredClone=="function"?(t,n)=>n&&("json"in n||"lossy"in n)?rt(ot(t,n)):structuredClone(t):(t,n)=>rt(ot(t,n)),D=function(t,n,e,o,r){const i=v(n);if(e!=null&&(typeof e!="number"||e<0||e===Number.POSITIVE_INFINITY))throw new Error("Expected positive finite `index`");if(o!=null&&(!o.type||!o.children))throw new Error("Expected valid `parent`");if(e==null!=(o==null))throw new Error("Expected both `index` and `parent`");return lt(t)?i.call(r,t,e,o):!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(o);function o(...r){let i=-1;for(;++i<n.length;)if(n[i].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,o,r){return!!(lt(e)&&t.call(this,e,typeof o=="number"?o: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 at=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 ct(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 oe;if(typeof t=="function")return M(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 M(o);function o(...r){let i=-1;for(;++i<n.length;)if(n[i].apply(this,r))return!0;return!1}}function ne(t){const n=t;return M(e);function e(o){const r=o;let i;for(i in t)if(r[i]!==n[i])return!1;return!0}}function re(t){return M(n);function n(e){return e&&e.type===t}}function M(t){return n;function n(e,o,r){return!!(ie(e)&&t.call(this,e,typeof o=="number"?o:void 0,r||void 0))}}function oe(){return!0}function ie(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"],ae=["button","input","select","textarea"],ce=["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),o={ignore:!1,stripAtStart:!1,remove:!1};let r=0,i=e.length;return n.before&&dt(e.charAt(0))&&r++,r!==i&&dt(e.charAt(i-1))&&(n.after?i--:o.stripAtStart=!0),r===i?o.remove=!0:t.value=e.slice(r,i),o}function fe(t,n){let e=n.before;const o=n.after,r=t.children;let i=r.length,l=-1;for(;++l<i;){const a=ut(r[l],{...n,after:pt(r,l,o),before:e});a.remove?(r.splice(l,1),l--,i--):a.ignore||(e=a.stripAtStart),ft(r[l])&&(e=!1)}return{ignore:!1,stripAtStart:!!(e||o),remove:!1}}function pt(t,n,e){for(;++n<t.length;){const o=t[n];let r=he(o);if(r===void 0&&"children"in o&&!de(o)&&(r=pt(o.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(!ct(t))return!1}else if(!L(t))return!1}function ft(t){return at(t)||D(t,ae)}function ht(t){return D(t,le)}function de(t){return!!(t.type==="element"&&t.properties.hidden)||L(t)||D(t,ce)}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,V=!1,mt="skip";function we(t,n,e,o){let r;typeof n=="function"&&typeof e!="function"?(o=e,e=n):r=n;const i=A(r),l=o?-1:1;a(t,void 0,[])();function a(c,u,s){const h=c&&typeof c=="object"?c:{};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 ("+(c.type+(m?"<"+m+">":""))+")"})}return d;function d(){let m=gt,b,y,x;if((!n||i(c,u,s[s.length-1]||void 0))&&(m=xe(e(c,s)),m[0]===V))return m;if("children"in c&&c.children){const w=c;if(w.children&&m[0]!==mt)for(y=(o?w.children.length:-1)+l,x=s.concat(w);y>-1&&y<w.children.length;){const Yt=w.children[y];if(b=a(Yt,y,x)(),b[0]===V)return b;y=typeof b[1]=="number"?b[1]:y+l}}return m}}}function xe(t){return Array.isArray(t)?t:typeof t=="number"?[ve,t]:t==null?gt:[t]}function U(t,n,e,o){let r,i,l;typeof n=="function"&&typeof e!="function"?(i=void 0,l=n,r=e):(i=n,l=e,r=o),we(t,i,a,r);function a(c,u){const s=u[u.length-1],h=s?s.children.indexOf(c):void 0;return l(c,h,s)}}var Ne=yt("end"),Se=yt("start");function yt(t){return n;function n(e){const o=e&&e.position&&e.position[t]||{};if(typeof o.line=="number"&&o.line>0&&typeof o.column=="number"&&o.column>0)return{line:o.line,column:o.column,offset:typeof o.offset=="number"&&o.offset>-1?o.offset:void 0}}}function ke(t){const n=Se(t),e=Ne(t);if(n&&e)return{start:n,end:e}}function Ae(t,n){const e=n.properties||{},o=t.all(n),r={type:"link",url:t.resolve(String(e.href||"")||null),title:e.title?String(e.title):null,children:o};return t.patch(n,r),r}function Ie(t,n){t.baseFound||(t.frozenBaseUrl=String(n.properties&&n.properties.href||"")||void 0,t.baseFound=!0)}function Te(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 o=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(o(t.children[n],n,t))return t.children[n]},vt=/\n/g,wt=/[\t ]+/g,W=v("br"),xt=v(Re),Ee=v("p"),Nt=v("tr"),Ce=v(["datalist","head","noembed","noframes","noscript","rp","script","style","template","title",Oe,qe]),St=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 I(t,n={}){const e="children"in t?t.children:[],o=St(t),r=It(t,{whitespace:n.whitespace||"normal",breakBefore:!1,breakAfter:!1}),i=[];(t.type==="text"||t.type==="comment")&&i.push(...At(t,{whitespace:r,breakBefore:!0,breakAfter:!0}));let l=-1;for(;++l<e.length;)i.push(...kt(e[l],t,{whitespace:r,breakBefore:l?void 0:o,breakAfter:l<e.length-1?W(e[l+1]):o}));const a=[];let c;for(l=-1;++l<i.length;){const u=i[l];typeof u=="number"?c!==void 0&&u>c&&(c=u):u&&(c!==void 0&&c>-1&&a.push(` `.repeat(c)||" "),c=-1,a.push(u))}return a.join("")}function kt(t,n,e){return t.type==="element"?Be(t,n,e):t.type==="text"?e.whitespace==="normal"?At(t,e):Me(t):[]}function Be(t,n,e){const o=It(t,e),r=t.children||[];let i=-1,l=[];if(Ce(t))return l;let a,c;for(W(t)||Nt(t)&&bt(n,t,Nt)?c=` `:Ee(t)?(a=2,c=2):St(t)&&(a=1,c=1);++i<r.length;)l=l.concat(kt(r[i],t,{whitespace:o,breakBefore:i?void 0:a,breakAfter:i<r.length-1?W(r[i+1]):c}));return xt(t)&&bt(n,t,xt)&&l.push(" "),a&&l.unshift(a),c&&l.push(c),l}function At(t,n){const e=String(t.value),o=[],r=[];let i=0;for(;i<=e.length;){vt.lastIndex=i;const c=vt.exec(e),u=c&&"index"in c?c.index:e.length;o.push(Ue(e.slice(i,u).replace(/[\u061C\u200E\u200F\u202A-\u202E\u2066-\u2069]/g,""),i===0?n.breakBefore:!0,u===e.length?n.breakAfter:!0)),i=u+1}let l=-1,a;for(;++l<o.length;)o[l].charCodeAt(o[l].length-1)===8203||l<o.length-1&&o[l+1].charCodeAt(0)===8203?(r.push(o[l]),a=void 0):o[l]?(typeof a=="number"&&r.push(a),r.push(o[l]),a=0):(l===0||l===o.length-1)&&r.push(0);return r}function Me(t){return[String(t.value)]}function Ue(t,n,e){const o=[];let r=0,i;for(;r<t.length;){wt.lastIndex=r;const l=wt.exec(t);i=l?l.index:t.length,!r&&!i&&l&&!n&&o.push(""),r!==i&&o.push(t.slice(r,i)),r=l?i+l[0].length:i}return r!==i&&!e&&o.push(""),o.join(" ")}function It(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 o=n.codePointAt(e-1);if(o!==void 0&&(o===10||o===13))e--;else break}return n.slice(0,e)}var Y="language-";function O(t,n){const e=n.children;let o=-1,r,i;if(n.tagName==="pre")for(;++o<e.length;){const a=e[o];if(a.type==="element"&&a.tagName==="code"&&a.properties&&a.properties.className&&Array.isArray(a.properties.className)){r=a.properties.className;break}}if(r){for(o=-1;++o<r.length;)if(String(r[o]).slice(0,Y.length)===Y){i=String(r[o]).slice(Y.length);break}}const l={type:"code",lang:i||null,meta:null,value:Fe(I(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 _(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=[],o=[];let r=-1;for(;++r<n.children.length;){const a=n.children[r];a.type==="element"&&a.tagName==="div"?e.push(...a.children):e.push(a)}let i={definitions:[],titles:[]};for(r=-1;++r<e.length;){const a=e[r];if(a.type==="element"&&a.tagName==="dt"){const c=e[r-1];c&&c.type==="element"&&c.tagName==="dd"&&(o.push(i),i={definitions:[],titles:[]}),i.titles.push(a)}else i.definitions.push(a)}o.push(i),r=-1;const l=[];for(;++r<o.length;){const a=[...Tt(t,o[r].titles),...Tt(t,o[r].definitions)];a.length>0&&l.push({type:"listItem",spread:a.length>1,checked:null,children:a})}if(l.length>0){const a={type:"list",ordered:!1,start:null,spread:$(l),children:l};return t.patch(n,a),a}}function Tt(t,n){const e=t.all({type:"root",children:n}),o=t.toSpecificContent(e,ze);return o.length===0?[]:o.length===1?o[0].children:[{type:"list",ordered:!1,start:null,spread:$(o),children:o}]}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 S(t,n){const e=Number(n.tagName.charAt(1))||1,o=t.all(n),r={type:"heading",depth:e,children:o};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||{},o=String(e.src||""),r=String(e.title||"");if(o&&r){const i={type:"link",title:null,url:t.resolve(o),children:[{type:"text",value:r}]};return t.patch(n,i),i}}function jt(t,n){const e=n.properties||{},o={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,o),o}function T(t,n){const e={type:"inlineCode",value:I(n)};return t.patch(n,e),e}function Et(t,n){const e=[],o=[],r=n||t.properties||{},i=Ct(t),l=Math.min(Number.parseInt(String(r.size),10),0)||(r.multiple?4:1);let a=-1;for(;++a<i.length;){const s=i[a];s&&s.properties&&s.properties.selected&&e.push(s)}const c=e.length>0?e:i,u=c.length>l?l:c.length;for(a=-1;++a<u;){const s=c[a],h=s.properties||{},d=I(s),m=d||String(h.label||""),b=String(h.value||"")||d;o.push([b,m===b?void 0:m])}return o}function Ct(t){const n=[];let e=-1;for(;++e<t.children.length;){const o=t.children[e];"children"in o&&Array.isArray(o.children)&&n.push(...Ct(o)),o.type==="element"&&o.tagName==="option"&&(!o.properties||!o.properties.disabled)&&n.push(o)}return n}var Le="[x]",Ve="[ ]";function We(t,n){const e=n.properties||{},o=String(e.value||e.placeholder||"");if(e.disabled||e.type==="hidden"||e.type==="file")return;if(e.type==="checkbox"||e.type==="radio"){const c={type:"text",value:e.checked?t.options.checked||Le:t.options.unchecked||Ve};return t.patch(n,c),c}if(e.type==="image"){const c=e.alt||o;if(c){const u={type:"image",url:t.resolve(String(e.src||"")||null),title:String(e.title||"")||null,alt:String(c)};return t.patch(n,u),u}return}let r=[];if(o)r=[[o,void 0]];else if(e.type!=="button"&&e.type!=="file"&&e.type!=="password"&&e.type!=="reset"&&e.type!=="submit"&&e.list){const c=String(e.list),u=t.elementById.get(c);u&&u.tagName==="datalist"&&(r=Et(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 c=[];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}]};c.push(h),u!==r.length-1&&c.push({type:"text",value:", "})}return c}const i=[];let l=-1;for(;++l<r.length;)i.push(r[l][1]?r[l][1]+" ("+r[l][0]+")":r[l][0]);const a={type:"text",value:i.join(", ")};return t.patch(n,a),a}var Ye={}.hasOwnProperty;function _e(t,n){const e=t.type==="element"&&Ye.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)||at(t)||Ge(t)||Qe(t)&&_e(t,"itemProp"))}function G(t,n){const{rest:e,checkbox:o}=Ut(n),r=o?!!o.properties.checked:null,i=Mt(e),l=t.toFlow(t.all(e)),a={type:"listItem",spread:i,checked:r,children:l};return t.patch(n,a),a}function Mt(t){let n=-1,e=!1;for(;++n<t.children.length;){const o=t.children[n];if(o.type==="element"){if(Bt(o))continue;if(o.tagName==="p"||e||Mt(o))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:o}=Ut(n);if(e){const r={...t,children:[o,...t.children.slice(1)]};return{checkbox:e,rest:r}}}return{checkbox:void 0,rest:t}}function K(t,n){const e=n.tagName==="ol",o=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 i={type:"list",ordered:e,start:r,spread:$(o),children:o};return t.patch(n,i),i}function Xe(){return{type:"listItem",spread:!1,checked:null,children:[]}}var Ze={};function tn(t,n){const e=n||Ze,o=typeof e.includeImageAlt=="boolean"?e.includeImageAlt:!0,r=typeof e.includeHtml=="boolean"?e.includeHtml:!0;return Ot(t,o,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 o=[];let r=-1;for(;++r<t.length;)o[r]=Ot(t[r],n,e);return o.join("")}function en(t){return!!(t&&typeof t=="object")}var nn=A(["break","delete","emphasis","footnote","footnoteReference","image","imageReference","inlineCode","link","linkReference","strong","text"]);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(o){return o.type==="text"?ct(o.value):!1})?[]:[{type:"paragraph",children:e}]}}function rn(t){return Ft(t.children,n,e);function n(o){const r=Q(t);return r.children=o,[r]}function e(o){if("children"in o&&"children"in t){const r=Q(t),i=Q(o);return r.children=o.children,i.children.push(r),i}return{...o}}}function Ft(t,n,e){const o=on(t),r=[];let i=[],l=-1;for(;++l<o.length;){const a=o[l];Pt(a)?i.push(a):(i.length>0&&(r.push(...n(i)),i=[]),r.push(e(a)))}return i.length>0&&(r.push(...n(i)),i=[]),r}function on(t){const n=[];let e=-1;for(;++e<t.length;){const o=t[e];(o.type==="delete"||o.type==="link")&&q(o.children)?n.push(...rn(o)):n.push(o)}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 it({...t,children:[]})}function Ht(t,n){const e=n.properties||{},o=n.tagName==="video"?String(e.poster||""):"";let r=String(e.src||""),i=-1,l=!1,a=t.all(n);if(U({type:"root",children:a},function(s){if(s.type==="link")return l=!0,V}),l||q(a))return a;for(;!r&&++i<n.children.length;){const s=n.children[i];s.type==="element"&&s.tagName==="source"&&s.properties&&(r=String(s.properties.src||""))}if(o){const s={type:"image",title:null,url:t.resolve(o),alt:tn(a)};t.patch(n,s),a=[s]}const c=a,u={type:"link",title:e.title?String(e.title):null,url:t.resolve(r),children:c};return t.patch(n,u),u}function zt(t,n){const e=t.all(n);if(e.length>0){const o={type:"paragraph",children:e};return t.patch(n,o),o}}var ln=['"'];function an(t,n){const e=t.options.quotes||ln;t.qNesting++;const o=t.all(n);t.qNesting--;const r=e[t.qNesting%e.length],i=o[0],l=o[o.length-1],a=r.charAt(0),c=r.length>1?r.charAt(1):r;return i&&i.type==="text"?i.value=a+i.value:o.unshift({type:"text",value:a}),l&&l.type==="text"?l.value+=c:o.push({type:"text",value:c}),o}function cn(t,n){let e=t.all(n);(t.options.document||q(e))&&(e=qt(e));const o={type:"root",children:e};return t.patch(n,o),o}function sn(t,n){const e=Et(n);let o=-1;const r=[];for(;++o<e.length;){const i=e[o];r.push(i[1]?i[1]+" ("+i[0]+")":i[0])}if(r.length>0){const i={type:"text",value:r.join(", ")};return t.patch(n,i),i}}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 o=n.properties.rowSpan,r=n.properties.colSpan;if(o||r){const i=e.data||(e.data={});o&&(i.hastUtilToMdastTemporaryRowSpan=o),r&&(i.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:I(n)};return t.patch(n,u),u}t.inTable=!0;const{align:e,headless:o}=hn(n),r=t.toSpecificContent(t.all(n),Lt);o&&r.unshift(Lt());let i=-1;for(;++i<r.length;){const u=r[i],s=t.toSpecificContent(u.children,dn);u.children=s}let l=1;for(i=-1;++i<r.length;){const u=r[i].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=i-1;for(;++y<i+b;){let x=s-1;for(;++x<s+m&&r[y];){const w=[];(y!==i||x!==s)&&w.push({type:"tableCell",children:[]}),r[y].children.splice(x,0,...w)}}}"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(i=-1;++i<r.length;){const u=r[i].children;let s=u.length-1;for(;++s<l;)u.push({type:"tableCell",children:[]})}let a=e.length-1;for(;++a<l;)e.push(null);t.inTable=!1;const c={type:"table",align:e,children:r};return t.patch(n,c),c}function hn(t){const n={align:[null],headless:!0};let e=0,o=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[o]){const i=String(r.properties.align||"")||null;(i==="center"||i==="left"||i==="right"||i===null)&&(n.align[o]=i)}n.headless&&e<2&&r.tagName==="th"&&(n.headless=!1),o++}else r.tagName==="thead"?n.headless=!1:r.tagName==="tr"&&(e++,o=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:I(n)};return t.patch(n,e),e}function yn(t,n){const e={type:"text",value:"\u200B"};return t.patch(n,e),e}var Vt={comment:Pe,doctype:f,root:cn,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:Ie,blockquote:Te,br:je,code:T,dir:K,dl:He,dt:G,dd:G,del:_,em:R,h1:S,h2:S,h3:S,h4:S,h5:S,h6:S,hr:De,i:R,iframe:Je,img:jt,image:jt,input:We,kbd:T,li:G,listing:O,mark:R,ol:K,p:zt,plaintext:O,pre:O,q:an,s:_,samp:T,select:sn,strike:_,strong:Dt,summary:zt,table:fn,td:Jt,textarea:mn,th:Jt,tr:un,tt:T,u:R,ul:K,var:T,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 Wt={}.hasOwnProperty;function bn(t){return{all:vn,baseFound:!1,elementById:new Map,frozenBaseUrl:void 0,handlers:{...X,...t.handlers},inTable:!1,nodeHandlers:{...Vt,...t.nodeHandlers},one:wn,options:t,patch:xn,qNesting:0,resolve:Nn,toFlow:Sn,toSpecificContent:kn}}function vn(t){const n=t.children||[],e=[];let o=-1;for(;++o<n.length;){const l=n[o],a=this.one(l,t);Array.isArray(a)?e.push(...a):a&&e.push(a)}let r=0,i=e.length;for(;r<i&&e[r].type==="break";)r++;for(;i>r&&e[i-1].type==="break";)i--;return r===0&&i===e.length?e:e.slice(r,i)}function wn(t,n){if(t.type==="element"){if(t.properties&&t.properties.dataMdast==="ignore")return;if(Wt.call(this.handlers,t.tagName))return this.handlers[t.tagName](this,t,n)||void 0}else if(Wt.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 xn(t,n){t.position&&(n.position=ke(t))}function Nn(t){const n=this.frozenBaseUrl;return t==null?"":n?String(new URL(t,n)):t}function Sn(t){return qt(t)}function kn(t,n){const e=n(),o=[];let r=[],i=-1;for(;++i<t.length;){const a=t[i];if(l(a))r.length>0&&(a.children.unshift(...r),r=[]),o.push(a);else{const c=a;r.push(c)}}if(r.length>0){let a=o[o.length-1];a||(a=n(),o.push(a)),a.children.push(...r),r=[]}return o;function l(a){return a.type===e.type}}var An={};function In(t,n){const e=it(t),o=n||An,r=ue({newlines:o.newlines===!0}),i=bn(o);let l;r(e),U(e,function(c){if(c&&c.type==="element"&&c.properties){const u=String(c.properties.id||"")||void 0;u&&!i.elementById.has(u)&&i.elementById.set(u,c)}});const a=i.one(e,void 0);return a?Array.isArray(a)?l={type:"root",children:a}:l=a:l={type:"root",children:[]},U(l,function(c,u,s){if(c.type==="text"&&u!==void 0&&s){const h=s.children[u-1];if(h&&h.type===c.type)return h.value+=c.value,s.children.splice(u,1),h.position&&c.position&&(h.position.end=c.position.end),u-1;if(c.value=c.value.replace(/[\t ]*(\r?\n|\r)[\t ]*/,"$1"),s&&(s.type==="heading"||s.type==="paragraph"||s.type==="root")&&(u||(c.value=c.value.replace(/^[\t ]+/,"")),u===s.children.length-1&&(c.value=c.value.replace(/[\t ]+$/,""))),!c.value)return s.children.splice(u,1),u}}),l}var Tn=X.li,jn=X.textarea,En=Vt.root;export{In as hastUtilToMdast,Tn as hastUtilToMdastListItem,En as hastUtilToMdastRoot,jn as hastUtilToMdastTextarea};