Skip to main content
Latest
File
const t=t=>Array.isArray(t)&&t.length>0,e=e=>t(e)&&2===e.length&&e.every((t=>"string"==typeof t)),o=(o,n,r="Missing Provided Input")=>{if(void 0===o)return[n];if("string"==typeof o)return[[o,o]];if(e(o))return[o];if((o=>t(o)&&o.every((t=>e(t))))(o))return o;throw Error(r)};function n(t){return t.replace(/[-[\]{}()*+?.,\\^$|#\s]/g,"\\$&")}const r=t=>!!t&&"object"==typeof t&&null!==t&&Object.keys(t).every((t=>"symbol"!=typeof t));function s(t,e){return Object.prototype.hasOwnProperty.call(t,e)}const a=Symbol("Blank Value");class i{constructor(t){this.context=t}getContextVal(t,e,o){const s=t.content,i=this.context,{allowRootBracket:l,allowUnquotedProps:c,sanitizer:p}=o,{per:f,opBracket:u,clBracket:d,quote:h,apostrophe:m,backtick:g}=e,y=[h,m,g].flat().filter((t=>s?.includes(t))).map((t=>n(t)));function b(t,e,o=0){let r=a,i=0;const p=Object.keys(e);t:{if(0===o)for(const n of p)if(t.startsWith(n,o)){r=e[n],i=n.length;break t}if(0!==o)for(const n of f)if(t.startsWith(n,o))for(const s of p)if(t.startsWith(n+s,o)){r=e[s],i=n.length+s.length;break t}if(0!==o||l){const a=u.map(((t,e)=>[t,d[e]])).filter((e=>t.includes(e[0])&&t.includes(e[1])));let l=null;for(const[e,n]of a)if(t.startsWith(e,o)){l=[e,n];break}if(null===l)break t;const[f,h]=l.map((t=>n(t)));for(const t of[...y,...c?[""]:[]])for(const a of p){const l=n(a),c=RegExp(`^[\\s\\S]{${o}}(${f}\\s*${t}(${l})${t}\\s*${h})`);c.lastIndex=o;const p=c.exec(s);if(p&&p[2]){r=e[a],i=p[1].length;break t}}}}return[r,i]}let x=a,k=0;for(;s?.length>k;){const t=x===a?i:x;if(!r(t))break;const[e,o]=b(s,t,k);if(0===o)break;k+=o,x=e}if(x=p(x===a?{isEmpty:!0,id:s,at:k}:{isEmpty:!1,value:x,id:s}),"string"!=typeof x)throw Error("Sanitizer did not provide string");return x}}const l={"'":"'",'"':'"',"\\":"\\","`":"`","[":"[","]":"]"},c=t=>s(l,t)?l[t]:t,p={".":["."],"\\":["'"],"'":["'"],'"':["""],"[":["["],"]":["]"]},f=t=>`\\u${Number.prototype.toString.call(t,16).toUpperCase()}`,u=t=>`&#x${Number.prototype.toString.call(t,16).toUpperCase()};`,d=(t,e)=>[...t].map((t=>t.split("").map((t=>({js:f,html:u}[e](String.prototype.charCodeAt.call(t,0))))).join(""))).join(""),h={url:t=>[encodeURIComponent(t)],html:t=>[[...t].map((t=>d(t,"html"))).join(""),[...t].map((t=>(t=>s(p,t)?p[t]:t)(t))).join("")],js:t=>[[...t].map((t=>c(t))).join(""),[...t].map((t=>d(t,"js"))).join("")],json:t=>[[...t].map((t=>c(t))).join(""),[...t].map((t=>d(t,"js"))).join("")]},m=(t,e)=>{const o=new Set;for(const n in e)Object.prototype.hasOwnProperty.call(h,n)&&h[n](t).forEach((t=>o.add(t)));return Array.from(o)},g=t=>({startStopPairs:o(t.startStopPairs,["${","}"],"Invalid startStopPairs"),escapeSep:void 0!==t.escapeSep&&t.escapeSep,allowUnquotedProps:!!t.allowUnquotedProps,allowRootBracket:!!t.allowRootBracket,sanitizer:t.sanitizer||(t=>!0===t.isEmpty?"undefined":String(t.value)),maxNameLength:t.maxNameLength||256}),y=m,b=t=>{const[e,o,n,r,s,a]=[".","[","]",'"',"'","`"].map((e=>t?m(e,t):[e]));return{per:e,opBracket:o,clBracket:n,quote:r,apostrophe:s,backtick:a}},x=(t,e,o)=>{const s=r(o)?o:{},a=g(s),{startStopPairs:l,escapeSep:c,maxNameLength:p}=a,f=l.filter((e=>t.includes(e[0])&&t.includes(e[1]))).map((t=>[n(t[0]),n(t[1])])).map((([t,e])=>`(?:${t}\\s*([\\s\\S]{0,${p}}?)\\s*${e})`)),u=new Map,d=RegExp(f.join("|"),"g"),h=()=>{d.lastIndex=0},m=()=>d.exec(t);for(const t of function(t,e,o){return{[Symbol.iterator]:()=>({next(){const n=t(...e);return n?{done:!1,value:n}:(o&&o(),{done:!0,value:void 0})}})}}(m,[],h))if(null!==t){const{index:e}=t,[o,n]=t.filter((t=>"string"==typeof t));if(!o&&!d.lastIndex)break;if(u.has(o)){const t=u.get(o);t&&t.indexes.push(e)}else u.set(o,{content:n,indexes:[e]})}const y=new i(e);let x=0;const k=[...t],j=b(c);for(const[t,e]of u){Object.prototype.hasOwnProperty.call(e,"value")||(e.value=y.getContextVal(e,j,a));for(const o of e.indexes){const n=String(e.value||"");k.splice(o+x,t.length,...n),x+=n.length-t.length}}return k.join("")};export default x;export{y as createEscapes,g as getDefaultOptions,b as getSeparators,x as subslate};//# sourceMappingURL=subslate.esm.js.map