storyblok-js-client
Advanced tools
Comparing version 5.7.0 to 5.8.0
@@ -1,1 +0,1 @@ | ||
(function(f,d){typeof exports=="object"&&typeof module<"u"?module.exports=d():typeof define=="function"&&define.amd?define(d):(f=typeof globalThis<"u"?globalThis:f||self,f.StoryblokJSClient=d())})(this,function(){"use strict";var S=Object.defineProperty;var C=(f,d,p)=>d in f?S(f,d,{enumerable:!0,configurable:!0,writable:!0,value:p}):f[d]=p;var h=(f,d,p)=>(C(f,typeof d!="symbol"?d+"":d,p),p);function f(a){return!(a!==a||a===1/0||a===-1/0)}function d(a,t,e){if(!f(t))throw new TypeError("Expected `limit` to be a finite number");if(!f(e))throw new TypeError("Expected `interval` to be a finite number");const s=[];let r=[],n=0;const i=function(){n++;const o=setTimeout(function(){n--,s.length>0&&i(),r=r.filter(function(u){return u!==o})},e);r.indexOf(o)<0&&r.push(o);const c=s.shift();c.resolve(a.apply(c.self,c.args))},l=function(...o){const c=this;return new Promise(function(u,y){s.push({resolve:u,reject:y,args:o,self:c}),n<t&&i()})};return l.abort=function(){r.forEach(clearTimeout),r=[],s.forEach(function(o){o.reject(function(){Error.call(this,"Throttled function aborted"),this.name="AbortError"})}),s.length=0},l}const p=function(a,t){const e={};for(const s in a){const r=a[s];t.indexOf(s)>-1&&r!==null&&(e[s]=r)}return e},P=a=>a==="email",R={nodes:{horizontal_rule:()=>({singleTag:"hr"}),blockquote:()=>({tag:"blockquote"}),bullet_list:()=>({tag:"ul"}),code_block:a=>({tag:["pre",{tag:"code",attrs:a.attrs}]}),hard_break:()=>({singleTag:"br"}),heading:a=>({tag:`h${a.attrs.level}`}),image:a=>({singleTag:[{tag:"img",attrs:p(a.attrs,["src","alt","title"])}]}),list_item:()=>({tag:"li"}),ordered_list:()=>({tag:"ol"}),paragraph:()=>({tag:"p"}),emoji:a=>({tag:[{tag:"span",attrs:{["data-type"]:"emoji",["data-name"]:a.attrs.name}}]})},marks:{bold:()=>({tag:"b"}),strike:()=>({tag:"strike"}),underline:()=>({tag:"u"}),strong:()=>({tag:"strong"}),code:()=>({tag:"code"}),italic:()=>({tag:"i"}),link:a=>{const t={...a.attrs},{linktype:e="url"}=a.attrs;if(P(e)&&(t.href=`mailto:${t.href}`),t.anchor&&(t.href=`${t.href}#${t.anchor}`,delete t.anchor),t.custom){for(const s in t.custom)t[s]=t.custom[s];delete t.custom}return{tag:[{tag:"a",attrs:t}]}},styled:a=>({tag:[{tag:"span",attrs:a.attrs}]}),subscript:()=>({tag:"sub"}),superscript:()=>({tag:"sup"}),highlight:a=>({tag:[{tag:"span",attrs:{style:`background-color:${a.attrs.color};`}}]}),textStyle:a=>({tag:[{tag:"span",attrs:{style:`background-color:${a.attrs.color}`}}]})}},$=function(a){const t={"&":"&","<":"<",">":">",'"':""","'":"'"},e=/[&<>"']/g,s=RegExp(e.source);return a&&s.test(a)?a.replace(e,r=>t[r]):a};class v{constructor(t){h(this,"marks");h(this,"nodes");t||(t=R),this.marks=t.marks||[],this.nodes=t.nodes||[]}addNode(t,e){this.nodes[t]=e}addMark(t,e){this.marks[t]=e}render(t,e={optimizeImages:!1}){if(t&&t.content&&Array.isArray(t.content)){let s="";return t.content.forEach(r=>{s+=this.renderNode(r)}),e.optimizeImages?s.replace(/a.storyblok.com\/f\/(\d+)\/([^.]+)\.(gif|jpg|jpeg|png|tif|tiff|bmp)/g,"a.storyblok.com/f/$1/$2.$3/m/"):s}return console.warn("The render method must receive an object with a content field, which is an array"),""}renderNode(t){const e=[];t.marks&&t.marks.forEach(r=>{const n=this.getMatchingMark(r);n&&e.push(this.renderOpeningTag(n.tag))});const s=this.getMatchingNode(t);return s&&s.tag&&e.push(this.renderOpeningTag(s.tag)),t.content?t.content.forEach(r=>{e.push(this.renderNode(r))}):t.text?e.push($(t.text)):s&&s.singleTag?e.push(this.renderTag(s.singleTag," /")):s&&s.html&&e.push(s.html),s&&s.tag&&e.push(this.renderClosingTag(s.tag)),t.marks&&t.marks.slice(0).reverse().forEach(r=>{const n=this.getMatchingMark(r);n&&e.push(this.renderClosingTag(n.tag))}),e.join("")}renderTag(t,e){return t.constructor===String?`<${t}${e}>`:t.map(r=>{if(r.constructor===String)return`<${r}${e}>`;{let n=`<${r.tag}`;if(r.attrs)for(const i in r.attrs){const l=r.attrs[i];l!==null&&(n+=` ${i}="${l}"`)}return`${n}${e}>`}}).join("")}renderOpeningTag(t){return this.renderTag(t,"")}renderClosingTag(t){return t.constructor===String?`</${t}>`:t.slice(0).reverse().map(s=>s.constructor===String?`</${s}>`:`</${s.tag}>`).join("")}getMatchingNode(t){const e=this.nodes[t.type];if(typeof e=="function")return e(t)}getMatchingMark(t){const e=this.marks[t.type];if(typeof e=="function")return e(t)}}class k{constructor(){h(this,"isCDNUrl",(t="")=>t.indexOf("/cdn/")>-1);h(this,"getOptionsPage",(t,e=25,s=1)=>({...t,per_page:e,page:s}));h(this,"delay",t=>new Promise(e=>setTimeout(e,t)));h(this,"arrayFrom",(t=0,e)=>[...Array(t)].map(e));h(this,"range",(t=0,e=t)=>{const s=Math.abs(e-t)||0,r=t<e?1:-1;return this.arrayFrom(s,(n,i)=>i*r+t)});h(this,"asyncMap",async(t,e)=>Promise.all(t.map(e)));h(this,"flatMap",(t=[],e)=>t.map(e).reduce((s,r)=>[...s,...r],[]))}stringify(t,e,s){const r=[];for(const n in t){if(!Object.prototype.hasOwnProperty.call(t,n))continue;const i=t[n],l=s?"":encodeURIComponent(n);let o;typeof i=="object"?o=this.stringify(i,e?e+encodeURIComponent("["+l+"]"):l,Array.isArray(i)):o=(e?e+encodeURIComponent("["+l+"]"):l)+"="+encodeURIComponent(i),r.push(o)}return r.join("&")}getRegionURL(t){const e="api.storyblok.com",s="api-us.storyblok.com",r="app.storyblokchina.cn";switch(t){case"us":return s;case"cn":return r;default:return e}}}const x=Object.freeze(Object.defineProperty({__proto__:null,SbHelpers:k},Symbol.toStringTag,{value:"Module"}));class T{constructor(t){h(this,"baseURL");h(this,"timeout");h(this,"headers");h(this,"responseInterceptor");h(this,"fetch");h(this,"ejectInterceptor");h(this,"url");h(this,"parameters");this.baseURL=t.baseURL,this.headers=t.headers||new Headers,this.timeout=t!=null&&t.timeout?t.timeout*1e3:0,this.responseInterceptor=t.responseInterceptor,this.fetch=(...e)=>t.fetch?t.fetch(...e):fetch(...e),this.ejectInterceptor=!1,this.url="",this.parameters={}}get(t,e){return this.url=t,this.parameters=e,this._methodHandler("get")}post(t,e){return this.url=t,this.parameters=e,this._methodHandler("post")}put(t,e){return this.url=t,this.parameters=e,this._methodHandler("put")}delete(t,e){return this.url=t,this.parameters=e,this._methodHandler("delete")}async _responseHandler(t){const e=[],s={data:{},headers:{},status:0,statusText:""};t.status!==204&&await t.json().then(r=>{s.data=r});for(const r of t.headers.entries())e[r[0]]=r[1];return s.headers={...e},s.status=t.status,s.statusText=t.statusText,s}async _methodHandler(t){let e=`${this.baseURL}${this.url}`,s=null;if(t==="get"){const o=new k;e=`${this.baseURL}${this.url}?${o.stringify(this.parameters)}`}else s=JSON.stringify(this.parameters);const r=new URL(e),n=new AbortController,{signal:i}=n;let l;this.timeout&&(l=setTimeout(()=>n.abort(),this.timeout));try{const o=await this.fetch(`${r}`,{method:t,headers:this.headers,body:s,signal:i});this.timeout&&clearTimeout(l);const c=await this._responseHandler(o);return this.responseInterceptor&&!this.ejectInterceptor?this._statusHandler(this.responseInterceptor(c)):this._statusHandler(c)}catch(o){return{message:o}}}eject(){this.ejectInterceptor=!0}_statusHandler(t){const e=/20[0-6]/g;return new Promise((s,r)=>{if(e.test(`${t.status}`))return s(t);const n={message:new Error(t.statusText),status:t.status,response:Array.isArray(t.data)?t.data[0]:t.data.error||t.data.slug};r(n)})}}let m={};const g={};class _{constructor(t,e){h(this,"client");h(this,"maxRetries");h(this,"throttle");h(this,"accessToken");h(this,"cache");h(this,"helpers");h(this,"resolveCounter");h(this,"relations");h(this,"links");h(this,"richTextResolver");h(this,"resolveNestedRelations");if(!e){const n=new k().getRegionURL,i=t.https===!1?"http":"https";t.oauthToken?e=`${i}://${n(t.region)}/v1`:e=`${i}://${n(t.region)}/v2`}const s=new Headers;s.set("Content-Type","application/json"),s.set("Accept","application/json"),s.forEach((n,i)=>{t.headers&&t.headers[i]&&s.set(i,t.headers[i])});let r=5;t.oauthToken&&(s.set("Authorization",t.oauthToken),r=3),t.rateLimit&&(r=t.rateLimit),t.richTextSchema?this.richTextResolver=new v(t.richTextSchema):this.richTextResolver=new v,t.componentResolver&&this.setComponentResolver(t.componentResolver),this.maxRetries=t.maxRetries,this.throttle=d(this.throttledRequest,r,1e3),this.accessToken=t.accessToken||"",this.relations={},this.links={},this.cache=t.cache||{clear:"manual"},this.helpers=new k,this.resolveCounter=0,this.resolveNestedRelations=!1,this.client=new T({baseURL:e,timeout:t.timeout||0,headers:s,responseInterceptor:t.responseInterceptor,fetch:t.fetch})}setComponentResolver(t){this.richTextResolver.addNode("blok",e=>{let s="";return e.attrs.body.forEach(r=>{s+=t(r.component,r)}),{html:s}})}parseParams(t){return t.version||(t.version="published"),t.token||(t.token=this.getToken()),t.cv||(t.cv=g[t.token]),Array.isArray(t.resolve_relations)&&(t.resolve_relations=t.resolve_relations.join(",")),t}factoryParamOptions(t,e){return this.helpers.isCDNUrl(t)?this.parseParams(e):e}makeRequest(t,e,s,r){const n=this.factoryParamOptions(t,this.helpers.getOptionsPage(e,s,r));return this.cacheResponse(t,n)}get(t,e){e||(e={});const s=`/${t}`,r=this.factoryParamOptions(s,e);return this.cacheResponse(s,r)}async getAll(t,e,s){const r=(e==null?void 0:e.per_page)||25,n=`/${t}`,i=n.split("/"),l=s||i[i.length-1],o=1,c=await this.makeRequest(n,e,r,o),u=c.total?Math.ceil(c.total/r):1,y=await this.helpers.asyncMap(this.helpers.range(o,u),b=>this.makeRequest(n,e,r,b+1));return this.helpers.flatMap([c,...y],b=>Object.values(b.data[l]))}post(t,e){const s=`/${t}`;return Promise.resolve(this.throttle("post",s,e))}put(t,e){const s=`/${t}`;return Promise.resolve(this.throttle("put",s,e))}delete(t,e){const s=`/${t}`;return Promise.resolve(this.throttle("delete",s,e))}getStories(t){return this.get("cdn/stories",t)}getStory(t,e){return this.get(`cdn/stories/${t}`,e)}getToken(){return this.accessToken}ejectInterceptor(){this.client.eject()}_cleanCopy(t){return JSON.parse(JSON.stringify(t))}_insertLinks(t,e,s){const r=t[e];r&&r.fieldtype=="multilink"&&r.linktype=="story"&&typeof r.id=="string"&&this.links[s][r.id]?r.story=this._cleanCopy(this.links[s][r.id]):r&&r.linktype==="story"&&typeof r.uuid=="string"&&this.links[s][r.uuid]&&(r.story=this._cleanCopy(this.links[s][r.uuid]))}_insertRelations(t,e,s,r){if(s.indexOf(`${t.component}.${e}`)>-1){if(typeof t[e]=="string")this.relations[r][t[e]]&&(t[e]=this._cleanCopy(this.relations[r][t[e]]));else if(t[e]&&t[e].constructor===Array){const n=[];t[e].forEach(i=>{this.relations[r][i]&&n.push(this._cleanCopy(this.relations[r][i]))}),t[e]=n}}}iterateTree(t,e,s){const r=n=>{if(n!=null){if(n.constructor===Array)for(let i=0;i<n.length;i++)r(n[i]);else if(n.constructor===Object){if(n._stopResolving)return;for(const i in n)(n.component&&n._uid||n.type==="link")&&(this._insertRelations(n,i,e,s),this._insertLinks(n,i,s)),r(n[i])}}};r(t.content)}async resolveLinks(t,e,s){let r=[];if(t.link_uuids){const n=t.link_uuids.length,i=[],l=50;for(let o=0;o<n;o+=l){const c=Math.min(n,o+l);i.push(t.link_uuids.slice(o,c))}for(let o=0;o<i.length;o++)(await this.getStories({per_page:l,language:e.language,version:e.version,by_uuids:i[o].join(",")})).data.stories.forEach(u=>{r.push(u)})}else r=t.links;r.forEach(n=>{this.links[s][n.uuid]={...n,_stopResolving:!0}})}async resolveRelations(t,e,s){let r=[];if(t.rel_uuids){const n=t.rel_uuids.length,i=[],l=50;for(let o=0;o<n;o+=l){const c=Math.min(n,o+l);i.push(t.rel_uuids.slice(o,c))}for(let o=0;o<i.length;o++)(await this.getStories({per_page:l,language:e.language,version:e.version,by_uuids:i[o].join(",")})).data.stories.forEach(u=>{r.push(u)})}else r=t.rels;r&&r.length>0&&r.forEach(n=>{this.relations[s][n.uuid]={...n,_stopResolving:!0}})}async resolveStories(t,e,s){var n,i;let r=[];if(this.links[s]={},this.relations[s]={},typeof e.resolve_relations<"u"&&e.resolve_relations.length>0&&(typeof e.resolve_relations=="string"&&(r=e.resolve_relations.split(",")),await this.resolveRelations(t,e,s)),e.resolve_links&&["1","story","url"].indexOf(e.resolve_links)>-1&&((n=t.links)!=null&&n.length||(i=t.link_uuids)!=null&&i.length)&&await this.resolveLinks(t,e,s),this.resolveNestedRelations)for(const l in this.relations[s])this.iterateTree(this.relations[s][l],r,s);t.story?this.iterateTree(t.story,r,s):t.stories.forEach(l=>{this.iterateTree(l,r,s)}),delete this.links[s],delete this.relations[s]}async cacheResponse(t,e,s){const r=this.helpers.stringify({url:t,params:e}),n=this.cacheProvider();if(this.cache.clear==="auto"&&e.version==="draft"&&await this.flushCache(),e.version==="published"&&t!="/cdn/spaces/me"){const i=await n.get(r);if(i)return Promise.resolve(i)}return new Promise((i,l)=>{try{(async()=>{var o;try{const c=await this.throttle("get",t,e);let u={data:c.data,headers:c.headers};if((o=c.headers)!=null&&o["per-page"]&&(u=Object.assign({},u,{perPage:c.headers["per-page"]?parseInt(c.headers["per-page"]):0,total:c.headers["per-page"]?parseInt(c.headers.total):0})),c.status!=200)return l(c);if(u.data.story||u.data.stories){const y=this.resolveCounter=++this.resolveCounter%1e3;await this.resolveStories(u.data,e,`${y}`)}return e.version==="published"&&t!="/cdn/spaces/me"&&await n.set(r,u),u.data.cv&&e.token&&(e.version=="draft"&&g[e.token]!=u.data.cv&&await this.flushCache(),g[e.token]=u.data.cv),i(u)}catch(c){return l(c)}})()}catch{}})}throttledRequest(t,e,s){return this.client[t](e,s)}cacheVersions(){return g}cacheVersion(){return g[this.accessToken]}setCacheVersion(t){this.accessToken&&(g[this.accessToken]=t)}cacheProvider(){switch(this.cache.type){case"memory":return{get(t){return Promise.resolve(m[t])},getAll(){return Promise.resolve(m)},set(t,e){return m[t]=e,Promise.resolve(void 0)},flush(){return m={},Promise.resolve(void 0)}};case"custom":if(this.cache.custom)return this.cache.custom;default:return{get(){return Promise.resolve(void 0)},getAll(){return Promise.resolve(void 0)},set(){return Promise.resolve(void 0)},flush(){return Promise.resolve(void 0)}}}}async flushCache(){return await this.cacheProvider().flush(),this}}const w=(a,t)=>{for(const e in t)a[e]=t[e]};return w(_,{RichtextResolver:v,SbFetch:T,RichTextSchema:R}),w(_,x),_}); | ||
(function(f,d){typeof exports=="object"&&typeof module<"u"?module.exports=d():typeof define=="function"&&define.amd?define(d):(f=typeof globalThis<"u"?globalThis:f||self,f.StoryblokJSClient=d())})(this,function(){"use strict";var S=Object.defineProperty;var C=(f,d,p)=>d in f?S(f,d,{enumerable:!0,configurable:!0,writable:!0,value:p}):f[d]=p;var h=(f,d,p)=>(C(f,typeof d!="symbol"?d+"":d,p),p);function f(a){return!(a!==a||a===1/0||a===-1/0)}function d(a,t,e){if(!f(t))throw new TypeError("Expected `limit` to be a finite number");if(!f(e))throw new TypeError("Expected `interval` to be a finite number");const s=[];let r=[],n=0;const i=function(){n++;const o=setTimeout(function(){n--,s.length>0&&i(),r=r.filter(function(u){return u!==o})},e);r.indexOf(o)<0&&r.push(o);const c=s.shift();c.resolve(a.apply(c.self,c.args))},l=function(...o){const c=this;return new Promise(function(u,y){s.push({resolve:u,reject:y,args:o,self:c}),n<t&&i()})};return l.abort=function(){r.forEach(clearTimeout),r=[],s.forEach(function(o){o.reject(function(){Error.call(this,"Throttled function aborted"),this.name="AbortError"})}),s.length=0},l}const p=function(a,t){const e={};for(const s in a){const r=a[s];t.indexOf(s)>-1&&r!==null&&(e[s]=r)}return e},P=a=>a==="email",R={nodes:{horizontal_rule:()=>({singleTag:"hr"}),blockquote:()=>({tag:"blockquote"}),bullet_list:()=>({tag:"ul"}),code_block:a=>({tag:["pre",{tag:"code",attrs:a.attrs}]}),hard_break:()=>({singleTag:"br"}),heading:a=>({tag:`h${a.attrs.level}`}),image:a=>({singleTag:[{tag:"img",attrs:p(a.attrs,["src","alt","title"])}]}),list_item:()=>({tag:"li"}),ordered_list:()=>({tag:"ol"}),paragraph:()=>({tag:"p"}),emoji:a=>({tag:[{tag:"span",attrs:{["data-type"]:"emoji",["data-name"]:a.attrs.name}}]})},marks:{bold:()=>({tag:"b"}),strike:()=>({tag:"strike"}),underline:()=>({tag:"u"}),strong:()=>({tag:"strong"}),code:()=>({tag:"code"}),italic:()=>({tag:"i"}),link:a=>{const t={...a.attrs},{linktype:e="url"}=a.attrs;if(P(e)&&(t.href=`mailto:${t.href}`),t.anchor&&(t.href=`${t.href}#${t.anchor}`,delete t.anchor),t.custom){for(const s in t.custom)t[s]=t.custom[s];delete t.custom}return{tag:[{tag:"a",attrs:t}]}},styled:a=>({tag:[{tag:"span",attrs:a.attrs}]}),subscript:()=>({tag:"sub"}),superscript:()=>({tag:"sup"}),anchor:a=>({tag:[{tag:"span",attrs:a.attrs}]}),highlight:a=>({tag:[{tag:"span",attrs:{style:`background-color:${a.attrs.color};`}}]}),textStyle:a=>({tag:[{tag:"span",attrs:{style:`background-color:${a.attrs.color}`}}]})}},$=function(a){const t={"&":"&","<":"<",">":">",'"':""","'":"'"},e=/[&<>"']/g,s=RegExp(e.source);return a&&s.test(a)?a.replace(e,r=>t[r]):a};class v{constructor(t){h(this,"marks");h(this,"nodes");t||(t=R),this.marks=t.marks||[],this.nodes=t.nodes||[]}addNode(t,e){this.nodes[t]=e}addMark(t,e){this.marks[t]=e}render(t,e={optimizeImages:!1}){if(t&&t.content&&Array.isArray(t.content)){let s="";return t.content.forEach(r=>{s+=this.renderNode(r)}),e.optimizeImages?s.replace(/a.storyblok.com\/f\/(\d+)\/([^.]+)\.(gif|jpg|jpeg|png|tif|tiff|bmp)/g,"a.storyblok.com/f/$1/$2.$3/m/"):s}return console.warn("The render method must receive an object with a content field, which is an array"),""}renderNode(t){const e=[];t.marks&&t.marks.forEach(r=>{const n=this.getMatchingMark(r);n&&e.push(this.renderOpeningTag(n.tag))});const s=this.getMatchingNode(t);return s&&s.tag&&e.push(this.renderOpeningTag(s.tag)),t.content?t.content.forEach(r=>{e.push(this.renderNode(r))}):t.text?e.push($(t.text)):s&&s.singleTag?e.push(this.renderTag(s.singleTag," /")):s&&s.html&&e.push(s.html),s&&s.tag&&e.push(this.renderClosingTag(s.tag)),t.marks&&t.marks.slice(0).reverse().forEach(r=>{const n=this.getMatchingMark(r);n&&e.push(this.renderClosingTag(n.tag))}),e.join("")}renderTag(t,e){return t.constructor===String?`<${t}${e}>`:t.map(r=>{if(r.constructor===String)return`<${r}${e}>`;{let n=`<${r.tag}`;if(r.attrs)for(const i in r.attrs){const l=r.attrs[i];l!==null&&(n+=` ${i}="${l}"`)}return`${n}${e}>`}}).join("")}renderOpeningTag(t){return this.renderTag(t,"")}renderClosingTag(t){return t.constructor===String?`</${t}>`:t.slice(0).reverse().map(s=>s.constructor===String?`</${s}>`:`</${s.tag}>`).join("")}getMatchingNode(t){const e=this.nodes[t.type];if(typeof e=="function")return e(t)}getMatchingMark(t){const e=this.marks[t.type];if(typeof e=="function")return e(t)}}class k{constructor(){h(this,"isCDNUrl",(t="")=>t.indexOf("/cdn/")>-1);h(this,"getOptionsPage",(t,e=25,s=1)=>({...t,per_page:e,page:s}));h(this,"delay",t=>new Promise(e=>setTimeout(e,t)));h(this,"arrayFrom",(t=0,e)=>[...Array(t)].map(e));h(this,"range",(t=0,e=t)=>{const s=Math.abs(e-t)||0,r=t<e?1:-1;return this.arrayFrom(s,(n,i)=>i*r+t)});h(this,"asyncMap",async(t,e)=>Promise.all(t.map(e)));h(this,"flatMap",(t=[],e)=>t.map(e).reduce((s,r)=>[...s,...r],[]))}stringify(t,e,s){const r=[];for(const n in t){if(!Object.prototype.hasOwnProperty.call(t,n))continue;const i=t[n],l=s?"":encodeURIComponent(n);let o;typeof i=="object"?o=this.stringify(i,e?e+encodeURIComponent("["+l+"]"):l,Array.isArray(i)):o=(e?e+encodeURIComponent("["+l+"]"):l)+"="+encodeURIComponent(i),r.push(o)}return r.join("&")}getRegionURL(t){const e="api.storyblok.com",s="api-us.storyblok.com",r="app.storyblokchina.cn";switch(t){case"us":return s;case"cn":return r;default:return e}}}const x=Object.freeze(Object.defineProperty({__proto__:null,SbHelpers:k},Symbol.toStringTag,{value:"Module"}));class T{constructor(t){h(this,"baseURL");h(this,"timeout");h(this,"headers");h(this,"responseInterceptor");h(this,"fetch");h(this,"ejectInterceptor");h(this,"url");h(this,"parameters");this.baseURL=t.baseURL,this.headers=t.headers||new Headers,this.timeout=t!=null&&t.timeout?t.timeout*1e3:0,this.responseInterceptor=t.responseInterceptor,this.fetch=(...e)=>t.fetch?t.fetch(...e):fetch(...e),this.ejectInterceptor=!1,this.url="",this.parameters={}}get(t,e){return this.url=t,this.parameters=e,this._methodHandler("get")}post(t,e){return this.url=t,this.parameters=e,this._methodHandler("post")}put(t,e){return this.url=t,this.parameters=e,this._methodHandler("put")}delete(t,e){return this.url=t,this.parameters=e,this._methodHandler("delete")}async _responseHandler(t){const e=[],s={data:{},headers:{},status:0,statusText:""};t.status!==204&&await t.json().then(r=>{s.data=r});for(const r of t.headers.entries())e[r[0]]=r[1];return s.headers={...e},s.status=t.status,s.statusText=t.statusText,s}async _methodHandler(t){let e=`${this.baseURL}${this.url}`,s=null;if(t==="get"){const o=new k;e=`${this.baseURL}${this.url}?${o.stringify(this.parameters)}`}else s=JSON.stringify(this.parameters);const r=new URL(e),n=new AbortController,{signal:i}=n;let l;this.timeout&&(l=setTimeout(()=>n.abort(),this.timeout));try{const o=await this.fetch(`${r}`,{method:t,headers:this.headers,body:s,signal:i});this.timeout&&clearTimeout(l);const c=await this._responseHandler(o);return this.responseInterceptor&&!this.ejectInterceptor?this._statusHandler(this.responseInterceptor(c)):this._statusHandler(c)}catch(o){return{message:o}}}eject(){this.ejectInterceptor=!0}_statusHandler(t){const e=/20[0-6]/g;return new Promise((s,r)=>{if(e.test(`${t.status}`))return s(t);const n={message:new Error(t.statusText),status:t.status,response:Array.isArray(t.data)?t.data[0]:t.data.error||t.data.slug};r(n)})}}let m={};const g={};class _{constructor(t,e){h(this,"client");h(this,"maxRetries");h(this,"throttle");h(this,"accessToken");h(this,"cache");h(this,"helpers");h(this,"resolveCounter");h(this,"relations");h(this,"links");h(this,"richTextResolver");h(this,"resolveNestedRelations");if(!e){const n=new k().getRegionURL,i=t.https===!1?"http":"https";t.oauthToken?e=`${i}://${n(t.region)}/v1`:e=`${i}://${n(t.region)}/v2`}const s=new Headers;s.set("Content-Type","application/json"),s.set("Accept","application/json"),s.forEach((n,i)=>{t.headers&&t.headers[i]&&s.set(i,t.headers[i])});let r=5;t.oauthToken&&(s.set("Authorization",t.oauthToken),r=3),t.rateLimit&&(r=t.rateLimit),t.richTextSchema?this.richTextResolver=new v(t.richTextSchema):this.richTextResolver=new v,t.componentResolver&&this.setComponentResolver(t.componentResolver),this.maxRetries=t.maxRetries,this.throttle=d(this.throttledRequest,r,1e3),this.accessToken=t.accessToken||"",this.relations={},this.links={},this.cache=t.cache||{clear:"manual"},this.helpers=new k,this.resolveCounter=0,this.resolveNestedRelations=!1,this.client=new T({baseURL:e,timeout:t.timeout||0,headers:s,responseInterceptor:t.responseInterceptor,fetch:t.fetch})}setComponentResolver(t){this.richTextResolver.addNode("blok",e=>{let s="";return e.attrs.body.forEach(r=>{s+=t(r.component,r)}),{html:s}})}parseParams(t){return t.version||(t.version="published"),t.token||(t.token=this.getToken()),t.cv||(t.cv=g[t.token]),Array.isArray(t.resolve_relations)&&(t.resolve_relations=t.resolve_relations.join(",")),t}factoryParamOptions(t,e){return this.helpers.isCDNUrl(t)?this.parseParams(e):e}makeRequest(t,e,s,r){const n=this.factoryParamOptions(t,this.helpers.getOptionsPage(e,s,r));return this.cacheResponse(t,n)}get(t,e){e||(e={});const s=`/${t}`,r=this.factoryParamOptions(s,e);return this.cacheResponse(s,r)}async getAll(t,e,s){const r=(e==null?void 0:e.per_page)||25,n=`/${t}`,i=n.split("/"),l=s||i[i.length-1],o=1,c=await this.makeRequest(n,e,r,o),u=c.total?Math.ceil(c.total/r):1,y=await this.helpers.asyncMap(this.helpers.range(o,u),b=>this.makeRequest(n,e,r,b+1));return this.helpers.flatMap([c,...y],b=>Object.values(b.data[l]))}post(t,e){const s=`/${t}`;return Promise.resolve(this.throttle("post",s,e))}put(t,e){const s=`/${t}`;return Promise.resolve(this.throttle("put",s,e))}delete(t,e){const s=`/${t}`;return Promise.resolve(this.throttle("delete",s,e))}getStories(t){return this.get("cdn/stories",t)}getStory(t,e){return this.get(`cdn/stories/${t}`,e)}getToken(){return this.accessToken}ejectInterceptor(){this.client.eject()}_cleanCopy(t){return JSON.parse(JSON.stringify(t))}_insertLinks(t,e,s){const r=t[e];r&&r.fieldtype=="multilink"&&r.linktype=="story"&&typeof r.id=="string"&&this.links[s][r.id]?r.story=this._cleanCopy(this.links[s][r.id]):r&&r.linktype==="story"&&typeof r.uuid=="string"&&this.links[s][r.uuid]&&(r.story=this._cleanCopy(this.links[s][r.uuid]))}_insertRelations(t,e,s,r){if(s.indexOf(`${t.component}.${e}`)>-1){if(typeof t[e]=="string")this.relations[r][t[e]]&&(t[e]=this._cleanCopy(this.relations[r][t[e]]));else if(t[e]&&t[e].constructor===Array){const n=[];t[e].forEach(i=>{this.relations[r][i]&&n.push(this._cleanCopy(this.relations[r][i]))}),t[e]=n}}}iterateTree(t,e,s){const r=n=>{if(n!=null){if(n.constructor===Array)for(let i=0;i<n.length;i++)r(n[i]);else if(n.constructor===Object){if(n._stopResolving)return;for(const i in n)(n.component&&n._uid||n.type==="link")&&(this._insertRelations(n,i,e,s),this._insertLinks(n,i,s)),r(n[i])}}};r(t.content)}async resolveLinks(t,e,s){let r=[];if(t.link_uuids){const n=t.link_uuids.length,i=[],l=50;for(let o=0;o<n;o+=l){const c=Math.min(n,o+l);i.push(t.link_uuids.slice(o,c))}for(let o=0;o<i.length;o++)(await this.getStories({per_page:l,language:e.language,version:e.version,by_uuids:i[o].join(",")})).data.stories.forEach(u=>{r.push(u)})}else r=t.links;r.forEach(n=>{this.links[s][n.uuid]={...n,_stopResolving:!0}})}async resolveRelations(t,e,s){let r=[];if(t.rel_uuids){const n=t.rel_uuids.length,i=[],l=50;for(let o=0;o<n;o+=l){const c=Math.min(n,o+l);i.push(t.rel_uuids.slice(o,c))}for(let o=0;o<i.length;o++)(await this.getStories({per_page:l,language:e.language,version:e.version,by_uuids:i[o].join(",")})).data.stories.forEach(u=>{r.push(u)})}else r=t.rels;r&&r.length>0&&r.forEach(n=>{this.relations[s][n.uuid]={...n,_stopResolving:!0}})}async resolveStories(t,e,s){var n,i;let r=[];if(this.links[s]={},this.relations[s]={},typeof e.resolve_relations<"u"&&e.resolve_relations.length>0&&(typeof e.resolve_relations=="string"&&(r=e.resolve_relations.split(",")),await this.resolveRelations(t,e,s)),e.resolve_links&&["1","story","url"].indexOf(e.resolve_links)>-1&&((n=t.links)!=null&&n.length||(i=t.link_uuids)!=null&&i.length)&&await this.resolveLinks(t,e,s),this.resolveNestedRelations)for(const l in this.relations[s])this.iterateTree(this.relations[s][l],r,s);t.story?this.iterateTree(t.story,r,s):t.stories.forEach(l=>{this.iterateTree(l,r,s)}),delete this.links[s],delete this.relations[s]}async cacheResponse(t,e,s){const r=this.helpers.stringify({url:t,params:e}),n=this.cacheProvider();if(this.cache.clear==="auto"&&e.version==="draft"&&await this.flushCache(),e.version==="published"&&t!="/cdn/spaces/me"){const i=await n.get(r);if(i)return Promise.resolve(i)}return new Promise((i,l)=>{try{(async()=>{var o;try{const c=await this.throttle("get",t,e);let u={data:c.data,headers:c.headers};if((o=c.headers)!=null&&o["per-page"]&&(u=Object.assign({},u,{perPage:c.headers["per-page"]?parseInt(c.headers["per-page"]):0,total:c.headers["per-page"]?parseInt(c.headers.total):0})),c.status!=200)return l(c);if(u.data.story||u.data.stories){const y=this.resolveCounter=++this.resolveCounter%1e3;await this.resolveStories(u.data,e,`${y}`)}return e.version==="published"&&t!="/cdn/spaces/me"&&await n.set(r,u),u.data.cv&&e.token&&(e.version=="draft"&&g[e.token]!=u.data.cv&&await this.flushCache(),g[e.token]=u.data.cv),i(u)}catch(c){return l(c)}})()}catch{}})}throttledRequest(t,e,s){return this.client[t](e,s)}cacheVersions(){return g}cacheVersion(){return g[this.accessToken]}setCacheVersion(t){this.accessToken&&(g[this.accessToken]=t)}cacheProvider(){switch(this.cache.type){case"memory":return{get(t){return Promise.resolve(m[t])},getAll(){return Promise.resolve(m)},set(t,e){return m[t]=e,Promise.resolve(void 0)},flush(){return m={},Promise.resolve(void 0)}};case"custom":if(this.cache.custom)return this.cache.custom;default:return{get(){return Promise.resolve(void 0)},getAll(){return Promise.resolve(void 0)},set(){return Promise.resolve(void 0)},flush(){return Promise.resolve(void 0)}}}}async flushCache(){return await this.cacheProvider().flush(),this}}const w=(a,t)=>{for(const e in t)a[e]=t[e]};return w(_,{RichtextResolver:v,SbFetch:T,RichTextSchema:R}),w(_,x),_}); |
@@ -1,1 +0,1 @@ | ||
(function(a,o){typeof exports=="object"&&typeof module<"u"?module.exports=o():typeof define=="function"&&define.amd?define(o):(a=typeof globalThis<"u"?globalThis:a||self,a.RichTextResolver=o())})(this,function(){"use strict";var p=Object.defineProperty;var m=(a,o,c)=>o in a?p(a,o,{enumerable:!0,configurable:!0,writable:!0,value:c}):a[o]=c;var l=(a,o,c)=>(m(a,typeof o!="symbol"?o+"":o,c),c);const a=function(n,t){const e={};for(const r in n){const s=n[r];t.indexOf(r)>-1&&s!==null&&(e[r]=s)}return e},o=n=>n==="email",h={nodes:{horizontal_rule:()=>({singleTag:"hr"}),blockquote:()=>({tag:"blockquote"}),bullet_list:()=>({tag:"ul"}),code_block:n=>({tag:["pre",{tag:"code",attrs:n.attrs}]}),hard_break:()=>({singleTag:"br"}),heading:n=>({tag:`h${n.attrs.level}`}),image:n=>({singleTag:[{tag:"img",attrs:a(n.attrs,["src","alt","title"])}]}),list_item:()=>({tag:"li"}),ordered_list:()=>({tag:"ol"}),paragraph:()=>({tag:"p"}),emoji:n=>({tag:[{tag:"span",attrs:{["data-type"]:"emoji",["data-name"]:n.attrs.name}}]})},marks:{bold:()=>({tag:"b"}),strike:()=>({tag:"strike"}),underline:()=>({tag:"u"}),strong:()=>({tag:"strong"}),code:()=>({tag:"code"}),italic:()=>({tag:"i"}),link:n=>{const t={...n.attrs},{linktype:e="url"}=n.attrs;if(o(e)&&(t.href=`mailto:${t.href}`),t.anchor&&(t.href=`${t.href}#${t.anchor}`,delete t.anchor),t.custom){for(const r in t.custom)t[r]=t.custom[r];delete t.custom}return{tag:[{tag:"a",attrs:t}]}},styled:n=>({tag:[{tag:"span",attrs:n.attrs}]}),subscript:()=>({tag:"sub"}),superscript:()=>({tag:"sup"}),highlight:n=>({tag:[{tag:"span",attrs:{style:`background-color:${n.attrs.color};`}}]}),textStyle:n=>({tag:[{tag:"span",attrs:{style:`background-color:${n.attrs.color}`}}]})}},d=function(n){const t={"&":"&","<":"<",">":">",'"':""","'":"'"},e=/[&<>"']/g,r=RegExp(e.source);return n&&r.test(n)?n.replace(e,s=>t[s]):n};class f{constructor(t){l(this,"marks");l(this,"nodes");t||(t=h),this.marks=t.marks||[],this.nodes=t.nodes||[]}addNode(t,e){this.nodes[t]=e}addMark(t,e){this.marks[t]=e}render(t,e={optimizeImages:!1}){if(t&&t.content&&Array.isArray(t.content)){let r="";return t.content.forEach(s=>{r+=this.renderNode(s)}),e.optimizeImages?r.replace(/a.storyblok.com\/f\/(\d+)\/([^.]+)\.(gif|jpg|jpeg|png|tif|tiff|bmp)/g,"a.storyblok.com/f/$1/$2.$3/m/"):r}return console.warn("The render method must receive an object with a content field, which is an array"),""}renderNode(t){const e=[];t.marks&&t.marks.forEach(s=>{const i=this.getMatchingMark(s);i&&e.push(this.renderOpeningTag(i.tag))});const r=this.getMatchingNode(t);return r&&r.tag&&e.push(this.renderOpeningTag(r.tag)),t.content?t.content.forEach(s=>{e.push(this.renderNode(s))}):t.text?e.push(d(t.text)):r&&r.singleTag?e.push(this.renderTag(r.singleTag," /")):r&&r.html&&e.push(r.html),r&&r.tag&&e.push(this.renderClosingTag(r.tag)),t.marks&&t.marks.slice(0).reverse().forEach(s=>{const i=this.getMatchingMark(s);i&&e.push(this.renderClosingTag(i.tag))}),e.join("")}renderTag(t,e){return t.constructor===String?`<${t}${e}>`:t.map(s=>{if(s.constructor===String)return`<${s}${e}>`;{let i=`<${s.tag}`;if(s.attrs)for(const u in s.attrs){const g=s.attrs[u];g!==null&&(i+=` ${u}="${g}"`)}return`${i}${e}>`}}).join("")}renderOpeningTag(t){return this.renderTag(t,"")}renderClosingTag(t){return t.constructor===String?`</${t}>`:t.slice(0).reverse().map(r=>r.constructor===String?`</${r}>`:`</${r.tag}>`).join("")}getMatchingNode(t){const e=this.nodes[t.type];if(typeof e=="function")return e(t)}getMatchingMark(t){const e=this.marks[t.type];if(typeof e=="function")return e(t)}}return f}); | ||
(function(a,o){typeof exports=="object"&&typeof module<"u"?module.exports=o():typeof define=="function"&&define.amd?define(o):(a=typeof globalThis<"u"?globalThis:a||self,a.RichTextResolver=o())})(this,function(){"use strict";var p=Object.defineProperty;var m=(a,o,c)=>o in a?p(a,o,{enumerable:!0,configurable:!0,writable:!0,value:c}):a[o]=c;var l=(a,o,c)=>(m(a,typeof o!="symbol"?o+"":o,c),c);const a=function(n,t){const e={};for(const r in n){const s=n[r];t.indexOf(r)>-1&&s!==null&&(e[r]=s)}return e},o=n=>n==="email",h={nodes:{horizontal_rule:()=>({singleTag:"hr"}),blockquote:()=>({tag:"blockquote"}),bullet_list:()=>({tag:"ul"}),code_block:n=>({tag:["pre",{tag:"code",attrs:n.attrs}]}),hard_break:()=>({singleTag:"br"}),heading:n=>({tag:`h${n.attrs.level}`}),image:n=>({singleTag:[{tag:"img",attrs:a(n.attrs,["src","alt","title"])}]}),list_item:()=>({tag:"li"}),ordered_list:()=>({tag:"ol"}),paragraph:()=>({tag:"p"}),emoji:n=>({tag:[{tag:"span",attrs:{["data-type"]:"emoji",["data-name"]:n.attrs.name}}]})},marks:{bold:()=>({tag:"b"}),strike:()=>({tag:"strike"}),underline:()=>({tag:"u"}),strong:()=>({tag:"strong"}),code:()=>({tag:"code"}),italic:()=>({tag:"i"}),link:n=>{const t={...n.attrs},{linktype:e="url"}=n.attrs;if(o(e)&&(t.href=`mailto:${t.href}`),t.anchor&&(t.href=`${t.href}#${t.anchor}`,delete t.anchor),t.custom){for(const r in t.custom)t[r]=t.custom[r];delete t.custom}return{tag:[{tag:"a",attrs:t}]}},styled:n=>({tag:[{tag:"span",attrs:n.attrs}]}),subscript:()=>({tag:"sub"}),superscript:()=>({tag:"sup"}),anchor:n=>({tag:[{tag:"span",attrs:n.attrs}]}),highlight:n=>({tag:[{tag:"span",attrs:{style:`background-color:${n.attrs.color};`}}]}),textStyle:n=>({tag:[{tag:"span",attrs:{style:`background-color:${n.attrs.color}`}}]})}},d=function(n){const t={"&":"&","<":"<",">":">",'"':""","'":"'"},e=/[&<>"']/g,r=RegExp(e.source);return n&&r.test(n)?n.replace(e,s=>t[s]):n};class f{constructor(t){l(this,"marks");l(this,"nodes");t||(t=h),this.marks=t.marks||[],this.nodes=t.nodes||[]}addNode(t,e){this.nodes[t]=e}addMark(t,e){this.marks[t]=e}render(t,e={optimizeImages:!1}){if(t&&t.content&&Array.isArray(t.content)){let r="";return t.content.forEach(s=>{r+=this.renderNode(s)}),e.optimizeImages?r.replace(/a.storyblok.com\/f\/(\d+)\/([^.]+)\.(gif|jpg|jpeg|png|tif|tiff|bmp)/g,"a.storyblok.com/f/$1/$2.$3/m/"):r}return console.warn("The render method must receive an object with a content field, which is an array"),""}renderNode(t){const e=[];t.marks&&t.marks.forEach(s=>{const i=this.getMatchingMark(s);i&&e.push(this.renderOpeningTag(i.tag))});const r=this.getMatchingNode(t);return r&&r.tag&&e.push(this.renderOpeningTag(r.tag)),t.content?t.content.forEach(s=>{e.push(this.renderNode(s))}):t.text?e.push(d(t.text)):r&&r.singleTag?e.push(this.renderTag(r.singleTag," /")):r&&r.html&&e.push(r.html),r&&r.tag&&e.push(this.renderClosingTag(r.tag)),t.marks&&t.marks.slice(0).reverse().forEach(s=>{const i=this.getMatchingMark(s);i&&e.push(this.renderClosingTag(i.tag))}),e.join("")}renderTag(t,e){return t.constructor===String?`<${t}${e}>`:t.map(s=>{if(s.constructor===String)return`<${s}${e}>`;{let i=`<${s.tag}`;if(s.attrs)for(const u in s.attrs){const g=s.attrs[u];g!==null&&(i+=` ${u}="${g}"`)}return`${i}${e}>`}}).join("")}renderOpeningTag(t){return this.renderTag(t,"")}renderClosingTag(t){return t.constructor===String?`</${t}>`:t.slice(0).reverse().map(r=>r.constructor===String?`</${r}>`:`</${r.tag}>`).join("")}getMatchingNode(t){const e=this.nodes[t.type];if(typeof e=="function")return e(t)}getMatchingMark(t){const e=this.marks[t.type];if(typeof e=="function")return e(t)}}return f}); |
@@ -1,1 +0,1 @@ | ||
(function(n,s){typeof exports=="object"&&typeof module<"u"?module.exports=s():typeof define=="function"&&define.amd?define(s):(n=typeof globalThis<"u"?globalThis:n||self,n.RichTextSchema=s())})(this,function(){"use strict";const n=function(t,e){const o={};for(const r in t){const a=t[r];e.indexOf(r)>-1&&a!==null&&(o[r]=a)}return o},s=t=>t==="email";return{nodes:{horizontal_rule:()=>({singleTag:"hr"}),blockquote:()=>({tag:"blockquote"}),bullet_list:()=>({tag:"ul"}),code_block:t=>({tag:["pre",{tag:"code",attrs:t.attrs}]}),hard_break:()=>({singleTag:"br"}),heading:t=>({tag:`h${t.attrs.level}`}),image:t=>({singleTag:[{tag:"img",attrs:n(t.attrs,["src","alt","title"])}]}),list_item:()=>({tag:"li"}),ordered_list:()=>({tag:"ol"}),paragraph:()=>({tag:"p"}),emoji:t=>({tag:[{tag:"span",attrs:{["data-type"]:"emoji",["data-name"]:t.attrs.name}}]})},marks:{bold:()=>({tag:"b"}),strike:()=>({tag:"strike"}),underline:()=>({tag:"u"}),strong:()=>({tag:"strong"}),code:()=>({tag:"code"}),italic:()=>({tag:"i"}),link:t=>{const e={...t.attrs},{linktype:o="url"}=t.attrs;if(s(o)&&(e.href=`mailto:${e.href}`),e.anchor&&(e.href=`${e.href}#${e.anchor}`,delete e.anchor),e.custom){for(const r in e.custom)e[r]=e.custom[r];delete e.custom}return{tag:[{tag:"a",attrs:e}]}},styled:t=>({tag:[{tag:"span",attrs:t.attrs}]}),subscript:()=>({tag:"sub"}),superscript:()=>({tag:"sup"}),highlight:t=>({tag:[{tag:"span",attrs:{style:`background-color:${t.attrs.color};`}}]}),textStyle:t=>({tag:[{tag:"span",attrs:{style:`background-color:${t.attrs.color}`}}]})}}}); | ||
(function(n,s){typeof exports=="object"&&typeof module<"u"?module.exports=s():typeof define=="function"&&define.amd?define(s):(n=typeof globalThis<"u"?globalThis:n||self,n.RichTextSchema=s())})(this,function(){"use strict";const n=function(t,r){const o={};for(const e in t){const a=t[e];r.indexOf(e)>-1&&a!==null&&(o[e]=a)}return o},s=t=>t==="email";return{nodes:{horizontal_rule:()=>({singleTag:"hr"}),blockquote:()=>({tag:"blockquote"}),bullet_list:()=>({tag:"ul"}),code_block:t=>({tag:["pre",{tag:"code",attrs:t.attrs}]}),hard_break:()=>({singleTag:"br"}),heading:t=>({tag:`h${t.attrs.level}`}),image:t=>({singleTag:[{tag:"img",attrs:n(t.attrs,["src","alt","title"])}]}),list_item:()=>({tag:"li"}),ordered_list:()=>({tag:"ol"}),paragraph:()=>({tag:"p"}),emoji:t=>({tag:[{tag:"span",attrs:{["data-type"]:"emoji",["data-name"]:t.attrs.name}}]})},marks:{bold:()=>({tag:"b"}),strike:()=>({tag:"strike"}),underline:()=>({tag:"u"}),strong:()=>({tag:"strong"}),code:()=>({tag:"code"}),italic:()=>({tag:"i"}),link:t=>{const r={...t.attrs},{linktype:o="url"}=t.attrs;if(s(o)&&(r.href=`mailto:${r.href}`),r.anchor&&(r.href=`${r.href}#${r.anchor}`,delete r.anchor),r.custom){for(const e in r.custom)r[e]=r.custom[e];delete r.custom}return{tag:[{tag:"a",attrs:r}]}},styled:t=>({tag:[{tag:"span",attrs:t.attrs}]}),subscript:()=>({tag:"sub"}),superscript:()=>({tag:"sup"}),anchor:t=>({tag:[{tag:"span",attrs:t.attrs}]}),highlight:t=>({tag:[{tag:"span",attrs:{style:`background-color:${t.attrs.color};`}}]}),textStyle:t=>({tag:[{tag:"span",attrs:{style:`background-color:${t.attrs.color}`}}]})}}}); |
@@ -184,2 +184,3 @@ import { ResponseFn } from './sbFetch'; | ||
export interface ISbNode extends Element { | ||
content: object[]; | ||
attrs: { | ||
@@ -186,0 +187,0 @@ anchor?: string; |
@@ -27,2 +27,3 @@ import { NodeSchema, MarkSchema } from './interfaces'; | ||
superscript: MarkSchema; | ||
anchor: MarkSchema; | ||
highlight: MarkSchema; | ||
@@ -29,0 +30,0 @@ textStyle: MarkSchema; |
{ | ||
"name": "storyblok-js-client", | ||
"version": "5.7.0", | ||
"version": "5.8.0", | ||
"description": "Universal JavaScript SDK for Storyblok's API", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -202,2 +202,3 @@ import { ResponseFn } from './sbFetch' | ||
export interface ISbNode extends Element { | ||
content: object[] | ||
attrs: { | ||
@@ -204,0 +205,0 @@ anchor?: string |
@@ -58,2 +58,3 @@ import { ISbNode, NodeSchema, MarkSchema, ISbComponentType } from './interfaces' | ||
} | ||
const image: NodeSchema = (node: ISbNode) => { | ||
@@ -161,2 +162,3 @@ return { | ||
} | ||
const styled: MarkSchema = (node: ISbNode) => { | ||
@@ -185,2 +187,13 @@ return { | ||
const anchor: MarkSchema = (node: ISbNode) => { | ||
return { | ||
tag: [ | ||
{ | ||
tag: 'span', | ||
attrs: node.attrs, | ||
}, | ||
], | ||
} | ||
} | ||
const highlight: MarkSchema = (node: ISbNode) => { | ||
@@ -239,2 +252,3 @@ const attrs = { | ||
superscript, | ||
anchor, | ||
highlight, | ||
@@ -241,0 +255,0 @@ textStyle, |
@@ -355,2 +355,56 @@ export const IMAGE_DATA = { | ||
export const PARAGRAPH_WITH_ANCHOR_IN_THE_MIDDLE = { | ||
"type": "doc", | ||
"content": [ | ||
{ | ||
"type": "paragraph", | ||
"content": [ | ||
{ | ||
"text": "a long text with a super nice ", | ||
"type": "text" | ||
}, | ||
{ | ||
"text": "anchor here", | ||
"type": "text", | ||
"marks": [ | ||
{ | ||
"type": "anchor", | ||
"attrs": { | ||
"id": "test2" | ||
} | ||
} | ||
] | ||
}, | ||
{ | ||
"text": ", and at the end of the text is a normal tag", | ||
"type": "text" | ||
} | ||
] | ||
} | ||
] | ||
} | ||
export const PARAGRAPH_WITH_ANCHOR = { | ||
"type": "doc", | ||
"content": [ | ||
{ | ||
"type": "paragraph", | ||
"content": [ | ||
{ | ||
"text": "Paragraph with anchor in the midle", | ||
"type": "text", | ||
"marks": [ | ||
{ | ||
"type": "anchor", | ||
"attrs": { | ||
"id": "test" | ||
} | ||
} | ||
] | ||
} | ||
] | ||
} | ||
] | ||
} | ||
export const TEXT_COLOR_DATA = { | ||
@@ -357,0 +411,0 @@ type: 'doc', |
@@ -14,2 +14,4 @@ /* eslint-disable no-undef */ | ||
LINK_WITH_ANCHOR_FOR_CUSTOM_SCHEMA, | ||
PARAGRAPH_WITH_ANCHOR_IN_THE_MIDDLE, | ||
PARAGRAPH_WITH_ANCHOR, | ||
TEXT_COLOR_DATA, | ||
@@ -329,2 +331,42 @@ HIGLIGHT_COLOR_DATA, | ||
test('should render a h1 title with a anchor in the middle of the text', () => { | ||
const sentenceWithAnchor = { | ||
type: 'doc', | ||
content: [ | ||
{ | ||
type: 'heading', | ||
attrs: { | ||
level: '1' | ||
}, | ||
content: [ | ||
{ | ||
text: 'Title with ', | ||
type: 'text' | ||
}, | ||
{ | ||
text: 'Anchor', | ||
type: 'text', | ||
marks: [ | ||
{ | ||
type: 'anchor', | ||
attrs: { | ||
id: 'test1' | ||
} | ||
} | ||
] | ||
}, | ||
{ | ||
text: ' in the midle', | ||
type: 'text' | ||
} | ||
] | ||
} | ||
] | ||
} | ||
const result = resolver.render(sentenceWithAnchor) | ||
const expected = '<h1>Title with <span id="test1">Anchor</span> in the midle</h1>' | ||
expect(result).toBe(expected) | ||
}) | ||
test('should render a text with text color', () => { | ||
@@ -337,2 +379,9 @@ const result = resolver.render(TEXT_COLOR_DATA) | ||
test('should render a anchor in the text', () => { | ||
const result = resolver.render(PARAGRAPH_WITH_ANCHOR) | ||
const expected = '<p><span id="test">Paragraph with anchor in the midle</span></p>' | ||
expect(result).toBe(expected) | ||
}) | ||
test('should render a text with highlight color', () => { | ||
@@ -346,2 +395,9 @@ const result = resolver.render(HIGLIGHT_COLOR_DATA) | ||
test('should render a anchor in the middle of a text', () => { | ||
const result = resolver.render(PARAGRAPH_WITH_ANCHOR_IN_THE_MIDDLE) | ||
const expected = '<p>a long text with a super nice <span id="test2">anchor here</span>, and at the end of the text is a normal tag</p>' | ||
expect(result).toBe(expected) | ||
}) | ||
test('should render a text with bold', () => { | ||
@@ -352,2 +408,2 @@ const result = resolver.render(BOLD_TEXT) | ||
expect(result).toBe(expected) | ||
}) | ||
}) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
156345
4505