@storyblok/astro 1.2.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -59,6 +59,8 @@ export default defineConfig({
59
59
  });
60
60
  ```
61
61
 
62
+ > ⚠️ This SDK uses the Fetch API under the hood. If your environment doesn't support it, you need to install a polyfill like [isomorphic-fetch](https://github.com/matthew-andrews/isomorphic-fetch). More info on [storyblok-js-client docs](https://github.com/storyblok/storyblok-js-client#fetch-use-polyfill-if-needed---version-5).
63
+
62
64
  ### Options
63
65
 
64
66
  When you initialize the integration, you can pass all [_@storyblok/js_ options](https://github.com/storyblok/storyblok-js#features-and-api). For spaces created in the United States, you have to set the `region` parameter accordingly `{ apiOptions: { region: 'us' } }`.
@@ -1,6 +1,12 @@
1
1
  ---
2
2
  import components from "virtual:storyblok-components";
3
3
  import camelcase from "camelcase";
4
+ import type { SbBlokData } from "./types";
5
+
6
+ interface Props {
7
+ blok: SbBlokData;
8
+ props: object;
9
+ }
4
10
 
5
11
  const { blok, ...props } = Astro.props;
6
12
 
@@ -1,17 +1,17 @@
1
- (function(i,c){typeof exports=="object"&&typeof module<"u"?c(exports,require("axios")):typeof define=="function"&&define.amd?define(["exports","axios"],c):(i=typeof globalThis<"u"?globalThis:i||self,c(i.storyblokAstro={}))})(this,function(i){"use strict";function c(r,e,t){const o="virtual:storyblok-init",n="\0"+o;return{name:"vite-plugin-storyblok-init",async resolveId(s){if(s===o)return n},async load(s){if(s===n)return`
1
+ (function(l,c){typeof exports=="object"&&typeof module<"u"?c(exports):typeof define=="function"&&define.amd?define(["exports"],c):(l=typeof globalThis<"u"?globalThis:l||self,c(l.storyblokAstro={}))})(this,function(l){"use strict";function c(t,e,r){const o="virtual:storyblok-init",s="\0"+o;return{name:"vite-plugin-storyblok-init",async resolveId(n){if(n===o)return s},async load(n){if(n===s)return`
2
2
  import { storyblokInit, apiPlugin } from "@storyblok/js";
3
3
  const { storyblokApi } = storyblokInit({
4
- accessToken: "${r}",
4
+ accessToken: "${t}",
5
5
  use: ${e?"[]":"[apiPlugin]"},
6
- apiOptions: ${JSON.stringify(t)},
6
+ apiOptions: ${JSON.stringify(r)},
7
7
  });
8
- export const storyblokApiInstance = storyblokApi;
9
- `}}}const T=/[\p{Lu}]/u,I=/[\p{Ll}]/u,d=/^[\p{Lu}](?![\p{Lu}])/gu,f=/([\p{Alpha}\p{N}_]|$)/u,u=/[_.\- ]+/,E=new RegExp("^"+u.source),h=new RegExp(u.source+f.source,"gu"),p=new RegExp("\\d+"+f.source,"gu"),A=(r,e,t)=>{let o=!1,n=!1,s=!1;for(let l=0;l<r.length;l++){const a=r[l];o&&T.test(a)?(r=r.slice(0,l)+"-"+r.slice(l),o=!1,s=n,n=!0,l++):n&&s&&I.test(a)?(r=r.slice(0,l-1)+"-"+r.slice(l-1),s=n,n=!1,o=!0):(o=e(a)===a&&t(a)!==a,s=n,n=t(a)===a&&e(a)!==a)}return r},S=(r,e)=>(d.lastIndex=0,r.replace(d,t=>e(t))),w=(r,e)=>(h.lastIndex=0,p.lastIndex=0,r.replace(h,(t,o)=>e(o)).replace(p,t=>e(t)));function g(r,e){if(!(typeof r=="string"||Array.isArray(r)))throw new TypeError("Expected the input to be `string | string[]`");if(e={pascalCase:!1,preserveConsecutiveUppercase:!1,...e},Array.isArray(r)?r=r.map(s=>s.trim()).filter(s=>s.length).join("-"):r=r.trim(),r.length===0)return"";const t=e.locale===!1?s=>s.toLowerCase():s=>s.toLocaleLowerCase(e.locale),o=e.locale===!1?s=>s.toUpperCase():s=>s.toLocaleUpperCase(e.locale);return r.length===1?u.test(r)?"":e.pascalCase?o(r):t(r):(r!==t(r)&&(r=A(r,t,o)),r=r.replace(E,""),r=e.preserveConsecutiveUppercase?S(r,t):t(r),e.pascalCase&&(r=o(r.charAt(0))+r.slice(1)),w(r,o))}function _(r){const e="virtual:storyblok-components",t="\0"+e;return{name:"vite-plugin-storyblok-components",async resolveId(o){if(o===e)return t},async load(o){if(o===t){const n=[];for await(const[s,l]of Object.entries(r)){const{id:a}=await this.resolve("/src/"+l+".astro");n.push(`import ${g(s)} from "${a}"`)}return`${n.join(";")};export default {${Object.keys(r).map(s=>g(s)).join(",")}}`}}}}var R=Object.defineProperty,y=Object.getOwnPropertySymbols,C=Object.prototype.hasOwnProperty,$=Object.prototype.propertyIsEnumerable,b=(r,e,t)=>e in r?R(r,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):r[e]=t,x=(r,e)=>{for(var t in e||(e={}))C.call(e,t)&&b(r,t,e[t]);if(y)for(var t of y(e))$.call(e,t)&&b(r,t,e[t]);return r};let k=!1;const m=[],O=r=>new Promise((e,t)=>{if(typeof window>"u"||(window.storyblokRegisterEvent=n=>{if(window.location===window.parent.location){console.warn("You are not in Draft Mode or in the Visual Editor.");return}k?n():m.push(n)},document.getElementById("storyblok-javascript-bridge")))return;const o=document.createElement("script");o.async=!0,o.src=r,o.id="storyblok-javascript-bridge",o.onerror=n=>t(n),o.onload=n=>{m.forEach(s=>s()),k=!0,e(n)},document.getElementsByTagName("head")[0].appendChild(o)}),P=function(r,e){if(!r)return null;let t={};for(let o in r){let n=r[o];e.indexOf(o)>-1&&n!==null&&(t[o]=n)}return t},N=r=>r==="email";var v={nodes:{horizontal_rule(){return{singleTag:"hr"}},blockquote(){return{tag:"blockquote"}},bullet_list(){return{tag:"ul"}},code_block(r){return{tag:["pre",{tag:"code",attrs:r.attrs}]}},hard_break(){return{singleTag:"br"}},heading(r){return{tag:`h${r.attrs.level}`}},image(r){return{singleTag:[{tag:"img",attrs:P(r.attrs,["src","alt","title"])}]}},list_item(){return{tag:"li"}},ordered_list(){return{tag:"ol"}},paragraph(){return{tag:"p"}}},marks:{bold(){return{tag:"b"}},strike(){return{tag:"strike"}},underline(){return{tag:"u"}},strong(){return{tag:"strong"}},code(){return{tag:"code"}},italic(){return{tag:"i"}},link(r){const e=x({},r.attrs),{linktype:t="url"}=r.attrs;return N(t)&&(e.href=`mailto:${e.href}`),e.anchor&&(e.href=`${e.href}#${e.anchor}`,delete e.anchor),{tag:[{tag:"a",attrs:e}]}},styled(r){return{tag:[{tag:"span",attrs:r.attrs}]}}}};const j=function(r){const e={"&":"&amp;","<":"&lt;",">":"&gt;",'"':"&quot;","'":"&#39;"},t=/[&<>"']/g,o=RegExp(t.source);return r&&o.test(r)?r.replace(t,n=>e[n]):r};class L{constructor(e){e||(e=v),this.marks=e.marks||[],this.nodes=e.nodes||[]}addNode(e,t){this.nodes[e]=t}addMark(e,t){this.marks[e]=t}render(e={}){if(e.content&&Array.isArray(e.content)){let t="";return e.content.forEach(o=>{t+=this.renderNode(o)}),t}return console.warn("The render method must receive an object with a content field, which is an array"),""}renderNode(e){let t=[];e.marks&&e.marks.forEach(n=>{const s=this.getMatchingMark(n);s&&t.push(this.renderOpeningTag(s.tag))});const o=this.getMatchingNode(e);return o&&o.tag&&t.push(this.renderOpeningTag(o.tag)),e.content?e.content.forEach(n=>{t.push(this.renderNode(n))}):e.text?t.push(j(e.text)):o&&o.singleTag?t.push(this.renderTag(o.singleTag," /")):o&&o.html&&t.push(o.html),o&&o.tag&&t.push(this.renderClosingTag(o.tag)),e.marks&&e.marks.slice(0).reverse().forEach(n=>{const s=this.getMatchingMark(n);s&&t.push(this.renderClosingTag(s.tag))}),t.join("")}renderTag(e,t){return e.constructor===String?`<${e}${t}>`:e.map(n=>{if(n.constructor===String)return`<${n}${t}>`;{let s=`<${n.tag}`;if(n.attrs)for(let l in n.attrs){let a=n.attrs[l];a!==null&&(s+=` ${l}="${a}"`)}return`${s}${t}>`}}).join("")}renderOpeningTag(e){return this.renderTag(e,"")}renderClosingTag(e){return e.constructor===String?`</${e}>`:e.slice(0).reverse().map(o=>o.constructor===String?`</${o}>`:`</${o.tag}>`).join("")}getMatchingNode(e){if(typeof this.nodes[e.type]=="function")return this.nodes[e.type](e)}getMatchingMark(e){if(typeof this.marks[e.type]=="function")return this.marks[e.type](e)}}var M=r=>{if(typeof r!="object"||typeof r._editable>"u")return{};const e=JSON.parse(r._editable.replace(/^<!--#storyblok#/,"").replace(/-->$/,""));return{"data-blok-c":JSON.stringify(e),"data-blok-uid":e.id+"-"+e.uid}};let B;const U="https://app.storyblok.com/f/storyblok-v2-latest.js",D=(r,e)=>{r.addNode("blok",t=>{let o="";return t.attrs.body.forEach(n=>{o+=e(n.component,n)}),{html:o}})},z=(r,e,t)=>{let o=t||B;if(!o){console.error("Please initialize the Storyblok SDK before calling the renderRichText function");return}return r===""?"":r?(e&&(o=new L(e.schema),e.resolver&&D(o,e.resolver)),o.render(r)):(console.warn(`${r} is not a valid Richtext object. This might be because the value of the richtext field is empty.
8
+ export const storyblokApiInstance = storyblokApi;
9
+ `}}}const A=/[\p{Lu}]/u,E=/[\p{Ll}]/u,d=/^[\p{Lu}](?![\p{Lu}])/gu,g=/([\p{Alpha}\p{N}_]|$)/u,u=/[_.\- ]+/,w=new RegExp("^"+u.source),f=new RegExp(u.source+g.source,"gu"),h=new RegExp("\\d+"+g.source,"gu"),S=(t,e,r)=>{let o=!1,s=!1,n=!1;for(let a=0;a<t.length;a++){const i=t[a];o&&A.test(i)?(t=t.slice(0,a)+"-"+t.slice(a),o=!1,n=s,s=!0,a++):s&&n&&E.test(i)?(t=t.slice(0,a-1)+"-"+t.slice(a-1),n=s,s=!1,o=!0):(o=e(i)===i&&r(i)!==i,n=s,s=r(i)===i&&e(i)!==i)}return t},R=(t,e)=>(d.lastIndex=0,t.replace(d,r=>e(r))),_=(t,e)=>(f.lastIndex=0,h.lastIndex=0,t.replace(f,(r,o)=>e(o)).replace(h,r=>e(r)));function p(t,e){if(!(typeof t=="string"||Array.isArray(t)))throw new TypeError("Expected the input to be `string | string[]`");if(e={pascalCase:!1,preserveConsecutiveUppercase:!1,...e},Array.isArray(t)?t=t.map(n=>n.trim()).filter(n=>n.length).join("-"):t=t.trim(),t.length===0)return"";const r=e.locale===!1?n=>n.toLowerCase():n=>n.toLocaleLowerCase(e.locale),o=e.locale===!1?n=>n.toUpperCase():n=>n.toLocaleUpperCase(e.locale);return t.length===1?u.test(t)?"":e.pascalCase?o(t):r(t):(t!==r(t)&&(t=S(t,r,o)),t=t.replace(w,""),t=e.preserveConsecutiveUppercase?R(t,r):r(t),e.pascalCase&&(t=o(t.charAt(0))+t.slice(1)),_(t,o))}function C(t){const e="virtual:storyblok-components",r="\0"+e;return{name:"vite-plugin-storyblok-components",async resolveId(o){if(o===e)return r},async load(o){if(o===r){const s=[];for await(const[n,a]of Object.entries(t)){const{id:i}=await this.resolve("/src/"+a+".astro");s.push(`import ${p(n)} from "${i}"`)}return`${s.join(";")};export default {${Object.keys(t).map(n=>p(n)).join(",")}}`}}}}var $=Object.defineProperty,b=Object.getOwnPropertySymbols,O=Object.prototype.hasOwnProperty,x=Object.prototype.propertyIsEnumerable,y=(t,e,r)=>e in t?$(t,e,{enumerable:!0,configurable:!0,writable:!0,value:r}):t[e]=r,P=(t,e)=>{for(var r in e||(e={}))O.call(e,r)&&y(t,r,e[r]);if(b)for(var r of b(e))x.call(e,r)&&y(t,r,e[r]);return t};let m=!1;const k=[],j=t=>new Promise((e,r)=>{if(typeof window>"u"||(window.storyblokRegisterEvent=s=>{if(window.location===window.parent.location){console.warn("You are not in Draft Mode or in the Visual Editor.");return}m?s():k.push(s)},document.getElementById("storyblok-javascript-bridge")))return;const o=document.createElement("script");o.async=!0,o.src=t,o.id="storyblok-javascript-bridge",o.onerror=s=>r(s),o.onload=s=>{k.forEach(n=>n()),m=!0,e(s)},document.getElementsByTagName("head")[0].appendChild(o)});var N=Object.defineProperty,L=(t,e,r)=>e in t?N(t,e,{enumerable:!0,configurable:!0,writable:!0,value:r}):t[e]=r,v=(t,e,r)=>(L(t,typeof e!="symbol"?e+"":e,r),r);const M=function(t,e){const r={};for(const o in t){const s=t[o];e.indexOf(o)>-1&&s!==null&&(r[o]=s)}return r},U=t=>t==="email",B=()=>({singleTag:"hr"}),D=()=>({tag:"blockquote"}),z=()=>({tag:"ul"}),F=t=>({tag:["pre",{tag:"code",attrs:t.attrs}]}),V=()=>({singleTag:"br"}),q=t=>({tag:`h${t.attrs.level}`}),J=t=>({singleTag:[{tag:"img",attrs:M(t.attrs,["src","alt","title"])}]}),G=()=>({tag:"li"}),K=()=>({tag:"ol"}),H=()=>({tag:"p"}),W=()=>({tag:"b"}),Y=()=>({tag:"strike"}),Q=()=>({tag:"u"}),X=()=>({tag:"strong"}),Z=()=>({tag:"code"}),ee=()=>({tag:"i"}),te=t=>{const e=P({},t.attrs),{linktype:r="url"}=t.attrs;return U(r)&&(e.href=`mailto:${e.href}`),e.anchor&&(e.href=`${e.href}#${e.anchor}`,delete e.anchor),{tag:[{tag:"a",attrs:e}]}},re=t=>({tag:[{tag:"span",attrs:t.attrs}]}),T={nodes:{horizontal_rule:B,blockquote:D,bullet_list:z,code_block:F,hard_break:V,heading:q,image:J,list_item:G,ordered_list:K,paragraph:H},marks:{bold:W,strike:Y,underline:Q,strong:X,code:Z,italic:ee,link:te,styled:re}},oe=function(t){const e={"&":"&amp;","<":"&lt;",">":"&gt;",'"':"&quot;","'":"&#39;"},r=/[&<>"']/g,o=RegExp(r.source);return t&&o.test(t)?t.replace(r,s=>e[s]):t};class I{constructor(e){v(this,"marks"),v(this,"nodes"),e||(e=T),this.marks=e.marks||[],this.nodes=e.nodes||[]}addNode(e,r){this.nodes[e]=r}addMark(e,r){this.marks[e]=r}render(e){if(e&&e.content&&Array.isArray(e.content)){let r="";return e.content.forEach(o=>{r+=this.renderNode(o)}),r}return console.warn("The render method must receive an object with a content field, which is an array"),""}renderNode(e){const r=[];e.marks&&e.marks.forEach(s=>{const n=this.getMatchingMark(s);n&&r.push(this.renderOpeningTag(n.tag))});const o=this.getMatchingNode(e);return o&&o.tag&&r.push(this.renderOpeningTag(o.tag)),e.content?e.content.forEach(s=>{r.push(this.renderNode(s))}):e.text?r.push(oe(e.text)):o&&o.singleTag?r.push(this.renderTag(o.singleTag," /")):o&&o.html&&r.push(o.html),o&&o.tag&&r.push(this.renderClosingTag(o.tag)),e.marks&&e.marks.slice(0).reverse().forEach(s=>{const n=this.getMatchingMark(s);n&&r.push(this.renderClosingTag(n.tag))}),r.join("")}renderTag(e,r){return e.constructor===String?`<${e}${r}>`:e.map(o=>{if(o.constructor===String)return`<${o}${r}>`;{let s=`<${o.tag}`;if(o.attrs)for(const n in o.attrs){const a=o.attrs[n];a!==null&&(s+=` ${n}="${a}"`)}return`${s}${r}>`}}).join("")}renderOpeningTag(e){return this.renderTag(e,"")}renderClosingTag(e){return e.constructor===String?`</${e}>`:e.slice(0).reverse().map(r=>r.constructor===String?`</${r}>`:`</${r.tag}>`).join("")}getMatchingNode(e){const r=this.nodes[e.type];if(typeof r=="function")return r(e)}getMatchingMark(e){const r=this.marks[e.type];if(typeof r=="function")return r(e)}}var se=t=>{if(typeof t!="object"||typeof t._editable>"u")return{};const e=JSON.parse(t._editable.replace(/^<!--#storyblok#/,"").replace(/-->$/,""));return{"data-blok-c":JSON.stringify(e),"data-blok-uid":e.id+"-"+e.uid}};let ne;const ae="https://app.storyblok.com/f/storyblok-v2-latest.js",le=(t,e)=>{t.addNode("blok",r=>{let o="";return r.attrs.body.forEach(s=>{o+=e(s.component,s)}),{html:o}})},ie=(t,e,r)=>{let o=r||ne;if(!o){console.error("Please initialize the Storyblok SDK before calling the renderRichText function");return}return t===""?"":t?(e&&(o=new I(e.schema),e.resolver&&le(o,e.resolver)),o.render(t)):(console.warn(`${t} is not a valid Richtext object. This might be because the value of the richtext field is empty.
10
10
 
11
- For more info about the richtext object check https://github.com/storyblok/storyblok-js#rendering-rich-text`),"")},H=()=>O(U);function q(){return globalThis.storyblokApiInstance||console.error("storyblokApiInstance has not been initialized correctly"),storyblokApiInstance}function F(r,e){const t=globalThis.storyblokApiInstance.richTextResolver;if(!t){console.error("Please initialize the Storyblok SDK before calling the renderRichText function");return}return z(r,e,t)}function V(r){return{name:"@storyblok/astro",hooks:{"astro:config:setup":({injectScript:e,updateConfig:t})=>{var n;t({vite:{plugins:[c(r.accessToken,r.useCustomApi,r.apiOptions),_(r.components)]}}),e("page-ssr",`
11
+ For more info about the richtext object check https://github.com/storyblok/storyblok-js#rendering-rich-text`),"")},ce=()=>j(ae);function ue(){return globalThis.storyblokApiInstance||console.error("storyblokApiInstance has not been initialized correctly"),globalThis.storyblokApiInstance}function de(t,e){const r=globalThis.storyblokApiInstance.richTextResolver;if(!r){console.error("Please initialize the Storyblok SDK before calling the renderRichText function");return}return ie(t,e,r)}function ge(t={accessToken:"",useCustomApi:!1,apiOptions:{},bridge:!0,components:{}}){return{name:"@storyblok/astro",hooks:{"astro:config:setup":({injectScript:e,updateConfig:r})=>{r({vite:{plugins:[c(t.accessToken,t.useCustomApi,t.apiOptions),C(t.components)]}}),e("page-ssr",`
12
12
  import { storyblokApiInstance } from "virtual:storyblok-init";
13
13
  globalThis.storyblokApiInstance = storyblokApiInstance;
14
- `),((n=r.bridge)!=null?n:!0)&&e("page",`
14
+ `),t.bridge&&e("page",`
15
15
  import { loadStoryblokBridge } from "@storyblok/astro";
16
16
  loadStoryblokBridge().then(() => {
17
17
  const { StoryblokBridge, location } = window;
@@ -23,4 +23,4 @@
23
23
  }
24
24
  });
25
25
  });
26
- `)}}}}i.RichTextSchema=v,i.default=V,i.loadStoryblokBridge=H,i.renderRichText=F,i.storyblokEditable=M,i.useStoryblokApi=q,Object.defineProperties(i,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
26
+ `)}}}}l.RichTextResolver=I,l.RichTextSchema=T,l.default=ge,l.loadStoryblokBridge=ce,l.renderRichText=de,l.storyblokEditable=se,l.useStoryblokApi=ue,Object.defineProperties(l,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
@@ -1,271 +1,234 @@
1
- import "axios";
2
- function k(r, e, t) {
3
- const o = "virtual:storyblok-init", n = "\0" + o;
1
+ function k(t, e, r) {
2
+ const o = "virtual:storyblok-init", s = "\0" + o;
4
3
  return {
5
4
  name: "vite-plugin-storyblok-init",
6
- async resolveId(s) {
7
- if (s === o)
8
- return n;
5
+ async resolveId(n) {
6
+ if (n === o)
7
+ return s;
9
8
  },
10
- async load(s) {
11
- if (s === n)
9
+ async load(n) {
10
+ if (n === s)
12
11
  return `
13
12
  import { storyblokInit, apiPlugin } from "@storyblok/js";
14
13
  const { storyblokApi } = storyblokInit({
15
- accessToken: "${r}",
14
+ accessToken: "${t}",
16
15
  use: ${e ? "[]" : "[apiPlugin]"},
17
- apiOptions: ${JSON.stringify(t)},
16
+ apiOptions: ${JSON.stringify(r)},
18
17
  });
19
- export const storyblokApiInstance = storyblokApi;
18
+ export const storyblokApiInstance = storyblokApi;
20
19
  `;
21
20
  }
22
21
  };
23
22
  }
24
- const m = /[\p{Lu}]/u, v = /[\p{Ll}]/u, c = /^[\p{Lu}](?![\p{Lu}])/gu, b = /([\p{Alpha}\p{N}_]|$)/u, i = /[_.\- ]+/, T = new RegExp("^" + i.source), u = new RegExp(i.source + b.source, "gu"), d = new RegExp("\\d+" + b.source, "gu"), I = (r, e, t) => {
25
- let o = !1, n = !1, s = !1;
26
- for (let l = 0; l < r.length; l++) {
27
- const a = r[l];
28
- o && m.test(a) ? (r = r.slice(0, l) + "-" + r.slice(l), o = !1, s = n, n = !0, l++) : n && s && v.test(a) ? (r = r.slice(0, l - 1) + "-" + r.slice(l - 1), s = n, n = !1, o = !0) : (o = e(a) === a && t(a) !== a, s = n, n = t(a) === a && e(a) !== a);
23
+ const v = /[\p{Lu}]/u, I = /[\p{Ll}]/u, c = /^[\p{Lu}](?![\p{Lu}])/gu, m = /([\p{Alpha}\p{N}_]|$)/u, i = /[_.\- ]+/, T = new RegExp("^" + i.source), u = new RegExp(i.source + m.source, "gu"), d = new RegExp("\\d+" + m.source, "gu"), E = (t, e, r) => {
24
+ let o = !1, s = !1, n = !1;
25
+ for (let a = 0; a < t.length; a++) {
26
+ const l = t[a];
27
+ o && v.test(l) ? (t = t.slice(0, a) + "-" + t.slice(a), o = !1, n = s, s = !0, a++) : s && n && I.test(l) ? (t = t.slice(0, a - 1) + "-" + t.slice(a - 1), n = s, s = !1, o = !0) : (o = e(l) === l && r(l) !== l, n = s, s = r(l) === l && e(l) !== l);
29
28
  }
30
- return r;
31
- }, E = (r, e) => (c.lastIndex = 0, r.replace(c, (t) => e(t))), A = (r, e) => (u.lastIndex = 0, d.lastIndex = 0, r.replace(u, (t, o) => e(o)).replace(d, (t) => e(t)));
32
- function f(r, e) {
33
- if (!(typeof r == "string" || Array.isArray(r)))
29
+ return t;
30
+ }, A = (t, e) => (c.lastIndex = 0, t.replace(c, (r) => e(r))), w = (t, e) => (u.lastIndex = 0, d.lastIndex = 0, t.replace(u, (r, o) => e(o)).replace(d, (r) => e(r)));
31
+ function g(t, e) {
32
+ if (!(typeof t == "string" || Array.isArray(t)))
34
33
  throw new TypeError("Expected the input to be `string | string[]`");
35
34
  if (e = {
36
35
  pascalCase: !1,
37
36
  preserveConsecutiveUppercase: !1,
38
37
  ...e
39
- }, Array.isArray(r) ? r = r.map((s) => s.trim()).filter((s) => s.length).join("-") : r = r.trim(), r.length === 0)
38
+ }, Array.isArray(t) ? t = t.map((n) => n.trim()).filter((n) => n.length).join("-") : t = t.trim(), t.length === 0)
40
39
  return "";
41
- const t = e.locale === !1 ? (s) => s.toLowerCase() : (s) => s.toLocaleLowerCase(e.locale), o = e.locale === !1 ? (s) => s.toUpperCase() : (s) => s.toLocaleUpperCase(e.locale);
42
- return r.length === 1 ? i.test(r) ? "" : e.pascalCase ? o(r) : t(r) : (r !== t(r) && (r = I(r, t, o)), r = r.replace(T, ""), r = e.preserveConsecutiveUppercase ? E(r, t) : t(r), e.pascalCase && (r = o(r.charAt(0)) + r.slice(1)), A(r, o));
40
+ const r = e.locale === !1 ? (n) => n.toLowerCase() : (n) => n.toLocaleLowerCase(e.locale), o = e.locale === !1 ? (n) => n.toUpperCase() : (n) => n.toLocaleUpperCase(e.locale);
41
+ return t.length === 1 ? i.test(t) ? "" : e.pascalCase ? o(t) : r(t) : (t !== r(t) && (t = E(t, r, o)), t = t.replace(T, ""), t = e.preserveConsecutiveUppercase ? A(t, r) : r(t), e.pascalCase && (t = o(t.charAt(0)) + t.slice(1)), w(t, o));
43
42
  }
44
- function w(r) {
45
- const e = "virtual:storyblok-components", t = "\0" + e;
43
+ function S(t) {
44
+ const e = "virtual:storyblok-components", r = "\0" + e;
46
45
  return {
47
46
  name: "vite-plugin-storyblok-components",
48
47
  async resolveId(o) {
49
48
  if (o === e)
50
- return t;
49
+ return r;
51
50
  },
52
51
  async load(o) {
53
- if (o === t) {
54
- const n = [];
55
- for await (const [s, l] of Object.entries(r)) {
56
- const { id: a } = await this.resolve("/src/" + l + ".astro");
57
- n.push(`import ${f(s)} from "${a}"`);
52
+ if (o === r) {
53
+ const s = [];
54
+ for await (const [n, a] of Object.entries(t)) {
55
+ const { id: l } = await this.resolve("/src/" + a + ".astro");
56
+ s.push(`import ${g(n)} from "${l}"`);
58
57
  }
59
- return `${n.join(";")};export default {${Object.keys(r).map((s) => f(s)).join(",")}}`;
58
+ return `${s.join(";")};export default {${Object.keys(t).map((n) => g(n)).join(",")}}`;
60
59
  }
61
60
  }
62
61
  };
63
62
  }
64
- var S = Object.defineProperty, h = Object.getOwnPropertySymbols, R = Object.prototype.hasOwnProperty, _ = Object.prototype.propertyIsEnumerable, p = (r, e, t) => e in r ? S(r, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : r[e] = t, C = (r, e) => {
65
- for (var t in e || (e = {}))
66
- R.call(e, t) && p(r, t, e[t]);
63
+ var C = Object.defineProperty, h = Object.getOwnPropertySymbols, R = Object.prototype.hasOwnProperty, _ = Object.prototype.propertyIsEnumerable, f = (t, e, r) => e in t ? C(t, e, { enumerable: !0, configurable: !0, writable: !0, value: r }) : t[e] = r, x = (t, e) => {
64
+ for (var r in e || (e = {}))
65
+ R.call(e, r) && f(t, r, e[r]);
67
66
  if (h)
68
- for (var t of h(e))
69
- _.call(e, t) && p(r, t, e[t]);
70
- return r;
67
+ for (var r of h(e))
68
+ _.call(e, r) && f(t, r, e[r]);
69
+ return t;
71
70
  };
72
- let g = !1;
73
- const y = [], x = (r) => new Promise((e, t) => {
74
- if (typeof window > "u" || (window.storyblokRegisterEvent = (n) => {
71
+ let p = !1;
72
+ const b = [], $ = (t) => new Promise((e, r) => {
73
+ if (typeof window > "u" || (window.storyblokRegisterEvent = (s) => {
75
74
  if (window.location === window.parent.location) {
76
75
  console.warn("You are not in Draft Mode or in the Visual Editor.");
77
76
  return;
78
77
  }
79
- g ? n() : y.push(n);
78
+ p ? s() : b.push(s);
80
79
  }, document.getElementById("storyblok-javascript-bridge")))
81
80
  return;
82
81
  const o = document.createElement("script");
83
- o.async = !0, o.src = r, o.id = "storyblok-javascript-bridge", o.onerror = (n) => t(n), o.onload = (n) => {
84
- y.forEach((s) => s()), g = !0, e(n);
82
+ o.async = !0, o.src = t, o.id = "storyblok-javascript-bridge", o.onerror = (s) => r(s), o.onload = (s) => {
83
+ b.forEach((n) => n()), p = !0, e(s);
85
84
  }, document.getElementsByTagName("head")[0].appendChild(o);
86
- }), $ = function(r, e) {
87
- if (!r)
88
- return null;
89
- let t = {};
90
- for (let o in r) {
91
- let n = r[o];
92
- e.indexOf(o) > -1 && n !== null && (t[o] = n);
85
+ });
86
+ var O = Object.defineProperty, P = (t, e, r) => e in t ? O(t, e, { enumerable: !0, configurable: !0, writable: !0, value: r }) : t[e] = r, y = (t, e, r) => (P(t, typeof e != "symbol" ? e + "" : e, r), r);
87
+ const N = function(t, e) {
88
+ const r = {};
89
+ for (const o in t) {
90
+ const s = t[o];
91
+ e.indexOf(o) > -1 && s !== null && (r[o] = s);
93
92
  }
94
- return t;
95
- }, O = (r) => r === "email";
96
- var N = {
97
- nodes: {
98
- horizontal_rule() {
99
- return {
100
- singleTag: "hr"
101
- };
102
- },
103
- blockquote() {
104
- return {
105
- tag: "blockquote"
106
- };
107
- },
108
- bullet_list() {
109
- return {
110
- tag: "ul"
111
- };
112
- },
113
- code_block(r) {
114
- return {
115
- tag: [
116
- "pre",
117
- {
118
- tag: "code",
119
- attrs: r.attrs
120
- }
121
- ]
122
- };
123
- },
124
- hard_break() {
125
- return {
126
- singleTag: "br"
127
- };
128
- },
129
- heading(r) {
130
- return {
131
- tag: `h${r.attrs.level}`
132
- };
133
- },
134
- image(r) {
135
- return {
136
- singleTag: [
137
- {
138
- tag: "img",
139
- attrs: $(r.attrs, ["src", "alt", "title"])
140
- }
141
- ]
142
- };
143
- },
144
- list_item() {
145
- return {
146
- tag: "li"
147
- };
148
- },
149
- ordered_list() {
150
- return {
151
- tag: "ol"
152
- };
153
- },
154
- paragraph() {
155
- return {
156
- tag: "p"
157
- };
93
+ return r;
94
+ }, j = (t) => t === "email", L = () => ({
95
+ singleTag: "hr"
96
+ }), M = () => ({
97
+ tag: "blockquote"
98
+ }), U = () => ({
99
+ tag: "ul"
100
+ }), D = (t) => ({
101
+ tag: [
102
+ "pre",
103
+ {
104
+ tag: "code",
105
+ attrs: t.attrs
158
106
  }
107
+ ]
108
+ }), B = () => ({
109
+ singleTag: "br"
110
+ }), z = (t) => ({
111
+ tag: `h${t.attrs.level}`
112
+ }), F = (t) => ({
113
+ singleTag: [
114
+ {
115
+ tag: "img",
116
+ attrs: N(t.attrs, ["src", "alt", "title"])
117
+ }
118
+ ]
119
+ }), V = () => ({
120
+ tag: "li"
121
+ }), q = () => ({
122
+ tag: "ol"
123
+ }), J = () => ({
124
+ tag: "p"
125
+ }), G = () => ({
126
+ tag: "b"
127
+ }), K = () => ({
128
+ tag: "strike"
129
+ }), H = () => ({
130
+ tag: "u"
131
+ }), W = () => ({
132
+ tag: "strong"
133
+ }), Y = () => ({
134
+ tag: "code"
135
+ }), Q = () => ({
136
+ tag: "i"
137
+ }), X = (t) => {
138
+ const e = x({}, t.attrs), { linktype: r = "url" } = t.attrs;
139
+ return j(r) && (e.href = `mailto:${e.href}`), e.anchor && (e.href = `${e.href}#${e.anchor}`, delete e.anchor), {
140
+ tag: [
141
+ {
142
+ tag: "a",
143
+ attrs: e
144
+ }
145
+ ]
146
+ };
147
+ }, Z = (t) => ({
148
+ tag: [
149
+ {
150
+ tag: "span",
151
+ attrs: t.attrs
152
+ }
153
+ ]
154
+ }), ee = {
155
+ nodes: {
156
+ horizontal_rule: L,
157
+ blockquote: M,
158
+ bullet_list: U,
159
+ code_block: D,
160
+ hard_break: B,
161
+ heading: z,
162
+ image: F,
163
+ list_item: V,
164
+ ordered_list: q,
165
+ paragraph: J
159
166
  },
160
167
  marks: {
161
- bold() {
162
- return {
163
- tag: "b"
164
- };
165
- },
166
- strike() {
167
- return {
168
- tag: "strike"
169
- };
170
- },
171
- underline() {
172
- return {
173
- tag: "u"
174
- };
175
- },
176
- strong() {
177
- return {
178
- tag: "strong"
179
- };
180
- },
181
- code() {
182
- return {
183
- tag: "code"
184
- };
185
- },
186
- italic() {
187
- return {
188
- tag: "i"
189
- };
190
- },
191
- link(r) {
192
- const e = C({}, r.attrs), { linktype: t = "url" } = r.attrs;
193
- return O(t) && (e.href = `mailto:${e.href}`), e.anchor && (e.href = `${e.href}#${e.anchor}`, delete e.anchor), {
194
- tag: [
195
- {
196
- tag: "a",
197
- attrs: e
198
- }
199
- ]
200
- };
201
- },
202
- styled(r) {
203
- return {
204
- tag: [
205
- {
206
- tag: "span",
207
- attrs: r.attrs
208
- }
209
- ]
210
- };
211
- }
168
+ bold: G,
169
+ strike: K,
170
+ underline: H,
171
+ strong: W,
172
+ code: Y,
173
+ italic: Q,
174
+ link: X,
175
+ styled: Z
212
176
  }
213
- };
214
- const P = function(r) {
177
+ }, te = function(t) {
215
178
  const e = {
216
179
  "&": "&amp;",
217
180
  "<": "&lt;",
218
181
  ">": "&gt;",
219
182
  '"': "&quot;",
220
183
  "'": "&#39;"
221
- }, t = /[&<>"']/g, o = RegExp(t.source);
222
- return r && o.test(r) ? r.replace(t, (n) => e[n]) : r;
184
+ }, r = /[&<>"']/g, o = RegExp(r.source);
185
+ return t && o.test(t) ? t.replace(r, (s) => e[s]) : t;
223
186
  };
224
- class L {
187
+ class re {
225
188
  constructor(e) {
226
- e || (e = N), this.marks = e.marks || [], this.nodes = e.nodes || [];
189
+ y(this, "marks"), y(this, "nodes"), e || (e = ee), this.marks = e.marks || [], this.nodes = e.nodes || [];
227
190
  }
228
- addNode(e, t) {
229
- this.nodes[e] = t;
191
+ addNode(e, r) {
192
+ this.nodes[e] = r;
230
193
  }
231
- addMark(e, t) {
232
- this.marks[e] = t;
194
+ addMark(e, r) {
195
+ this.marks[e] = r;
233
196
  }
234
- render(e = {}) {
235
- if (e.content && Array.isArray(e.content)) {
236
- let t = "";
197
+ render(e) {
198
+ if (e && e.content && Array.isArray(e.content)) {
199
+ let r = "";
237
200
  return e.content.forEach((o) => {
238
- t += this.renderNode(o);
239
- }), t;
201
+ r += this.renderNode(o);
202
+ }), r;
240
203
  }
241
204
  return console.warn("The render method must receive an object with a content field, which is an array"), "";
242
205
  }
243
206
  renderNode(e) {
244
- let t = [];
245
- e.marks && e.marks.forEach((n) => {
246
- const s = this.getMatchingMark(n);
247
- s && t.push(this.renderOpeningTag(s.tag));
207
+ const r = [];
208
+ e.marks && e.marks.forEach((s) => {
209
+ const n = this.getMatchingMark(s);
210
+ n && r.push(this.renderOpeningTag(n.tag));
248
211
  });
249
212
  const o = this.getMatchingNode(e);
250
- return o && o.tag && t.push(this.renderOpeningTag(o.tag)), e.content ? e.content.forEach((n) => {
251
- t.push(this.renderNode(n));
252
- }) : e.text ? t.push(P(e.text)) : o && o.singleTag ? t.push(this.renderTag(o.singleTag, " /")) : o && o.html && t.push(o.html), o && o.tag && t.push(this.renderClosingTag(o.tag)), e.marks && e.marks.slice(0).reverse().forEach((n) => {
253
- const s = this.getMatchingMark(n);
254
- s && t.push(this.renderClosingTag(s.tag));
255
- }), t.join("");
213
+ return o && o.tag && r.push(this.renderOpeningTag(o.tag)), e.content ? e.content.forEach((s) => {
214
+ r.push(this.renderNode(s));
215
+ }) : e.text ? r.push(te(e.text)) : o && o.singleTag ? r.push(this.renderTag(o.singleTag, " /")) : o && o.html && r.push(o.html), o && o.tag && r.push(this.renderClosingTag(o.tag)), e.marks && e.marks.slice(0).reverse().forEach((s) => {
216
+ const n = this.getMatchingMark(s);
217
+ n && r.push(this.renderClosingTag(n.tag));
218
+ }), r.join("");
256
219
  }
257
- renderTag(e, t) {
258
- return e.constructor === String ? `<${e}${t}>` : e.map((n) => {
259
- if (n.constructor === String)
260
- return `<${n}${t}>`;
220
+ renderTag(e, r) {
221
+ return e.constructor === String ? `<${e}${r}>` : e.map((o) => {
222
+ if (o.constructor === String)
223
+ return `<${o}${r}>`;
261
224
  {
262
- let s = `<${n.tag}`;
263
- if (n.attrs)
264
- for (let l in n.attrs) {
265
- let a = n.attrs[l];
266
- a !== null && (s += ` ${l}="${a}"`);
225
+ let s = `<${o.tag}`;
226
+ if (o.attrs)
227
+ for (const n in o.attrs) {
228
+ const a = o.attrs[n];
229
+ a !== null && (s += ` ${n}="${a}"`);
267
230
  }
268
- return `${s}${t}>`;
231
+ return `${s}${r}>`;
269
232
  }
270
233
  }).join("");
271
234
  }
@@ -273,74 +236,81 @@ class L {
273
236
  return this.renderTag(e, "");
274
237
  }
275
238
  renderClosingTag(e) {
276
- return e.constructor === String ? `</${e}>` : e.slice(0).reverse().map((o) => o.constructor === String ? `</${o}>` : `</${o.tag}>`).join("");
239
+ return e.constructor === String ? `</${e}>` : e.slice(0).reverse().map((r) => r.constructor === String ? `</${r}>` : `</${r.tag}>`).join("");
277
240
  }
278
241
  getMatchingNode(e) {
279
- if (typeof this.nodes[e.type] == "function")
280
- return this.nodes[e.type](e);
242
+ const r = this.nodes[e.type];
243
+ if (typeof r == "function")
244
+ return r(e);
281
245
  }
282
246
  getMatchingMark(e) {
283
- if (typeof this.marks[e.type] == "function")
284
- return this.marks[e.type](e);
247
+ const r = this.marks[e.type];
248
+ if (typeof r == "function")
249
+ return r(e);
285
250
  }
286
251
  }
287
- var z = (r) => {
288
- if (typeof r != "object" || typeof r._editable > "u")
252
+ var le = (t) => {
253
+ if (typeof t != "object" || typeof t._editable > "u")
289
254
  return {};
290
- const e = JSON.parse(r._editable.replace(/^<!--#storyblok#/, "").replace(/-->$/, ""));
255
+ const e = JSON.parse(t._editable.replace(/^<!--#storyblok#/, "").replace(/-->$/, ""));
291
256
  return {
292
257
  "data-blok-c": JSON.stringify(e),
293
258
  "data-blok-uid": e.id + "-" + e.uid
294
259
  };
295
260
  };
296
- let j;
297
- const M = "https://app.storyblok.com/f/storyblok-v2-latest.js", U = (r, e) => {
298
- r.addNode("blok", (t) => {
261
+ let oe;
262
+ const se = "https://app.storyblok.com/f/storyblok-v2-latest.js", ne = (t, e) => {
263
+ t.addNode("blok", (r) => {
299
264
  let o = "";
300
- return t.attrs.body.forEach((n) => {
301
- o += e(n.component, n);
265
+ return r.attrs.body.forEach((s) => {
266
+ o += e(s.component, s);
302
267
  }), {
303
268
  html: o
304
269
  };
305
270
  });
306
- }, B = (r, e, t) => {
307
- let o = t || j;
271
+ }, ae = (t, e, r) => {
272
+ let o = r || oe;
308
273
  if (!o) {
309
274
  console.error("Please initialize the Storyblok SDK before calling the renderRichText function");
310
275
  return;
311
276
  }
312
- return r === "" ? "" : r ? (e && (o = new L(e.schema), e.resolver && U(o, e.resolver)), o.render(r)) : (console.warn(`${r} is not a valid Richtext object. This might be because the value of the richtext field is empty.
277
+ return t === "" ? "" : t ? (e && (o = new re(e.schema), e.resolver && ne(o, e.resolver)), o.render(t)) : (console.warn(`${t} is not a valid Richtext object. This might be because the value of the richtext field is empty.
313
278
 
314
279
  For more info about the richtext object check https://github.com/storyblok/storyblok-js#rendering-rich-text`), "");
315
- }, H = () => x(M);
316
- function F() {
317
- return globalThis.storyblokApiInstance || console.error("storyblokApiInstance has not been initialized correctly"), storyblokApiInstance;
280
+ }, ie = () => $(se);
281
+ function ce() {
282
+ return globalThis.storyblokApiInstance || console.error("storyblokApiInstance has not been initialized correctly"), globalThis.storyblokApiInstance;
318
283
  }
319
- function V(r, e) {
320
- const t = globalThis.storyblokApiInstance.richTextResolver;
321
- if (!t) {
284
+ function ue(t, e) {
285
+ const r = globalThis.storyblokApiInstance.richTextResolver;
286
+ if (!r) {
322
287
  console.error(
323
288
  "Please initialize the Storyblok SDK before calling the renderRichText function"
324
289
  );
325
290
  return;
326
291
  }
327
- return B(r, e, t);
292
+ return ae(t, e, r);
328
293
  }
329
- function q(r) {
294
+ function de(t = {
295
+ accessToken: "",
296
+ useCustomApi: !1,
297
+ apiOptions: {},
298
+ bridge: !0,
299
+ components: {}
300
+ }) {
330
301
  return {
331
302
  name: "@storyblok/astro",
332
303
  hooks: {
333
- "astro:config:setup": ({ injectScript: e, updateConfig: t }) => {
334
- var n;
335
- t({
304
+ "astro:config:setup": ({ injectScript: e, updateConfig: r }) => {
305
+ r({
336
306
  vite: {
337
307
  plugins: [
338
308
  k(
339
- r.accessToken,
340
- r.useCustomApi,
341
- r.apiOptions
309
+ t.accessToken,
310
+ t.useCustomApi,
311
+ t.apiOptions
342
312
  ),
343
- w(r.components)
313
+ S(t.components)
344
314
  ]
345
315
  }
346
316
  }), e(
@@ -349,7 +319,7 @@ function q(r) {
349
319
  import { storyblokApiInstance } from "virtual:storyblok-init";
350
320
  globalThis.storyblokApiInstance = storyblokApiInstance;
351
321
  `
352
- ), ((n = r.bridge) != null ? n : !0) && e(
322
+ ), t.bridge && e(
353
323
  "page",
354
324
  `
355
325
  import { loadStoryblokBridge } from "@storyblok/astro";
@@ -370,10 +340,11 @@ function q(r) {
370
340
  };
371
341
  }
372
342
  export {
373
- N as RichTextSchema,
374
- q as default,
375
- H as loadStoryblokBridge,
376
- V as renderRichText,
377
- z as storyblokEditable,
378
- F as useStoryblokApi
343
+ re as RichTextResolver,
344
+ ee as RichTextSchema,
345
+ de as default,
346
+ ie as loadStoryblokBridge,
347
+ ue as renderRichText,
348
+ le as storyblokEditable,
349
+ ce as useStoryblokApi
379
350
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@storyblok/astro",
3
- "version": "1.2.0",
3
+ "version": "2.0.0",
4
4
  "description": "Official Astro integration for the Storyblok Headless CMS",
5
5
  "main": "./dist/storyblok-astro.js",
6
6
  "module": "./dist/storyblok-astro.mjs",
@@ -15,9 +15,10 @@
15
15
  },
16
16
  "./StoryblokComponent.astro": "./StoryblokComponent.astro"
17
17
  },
18
+ "types": "./dist/index.d.ts",
18
19
  "scripts": {
19
20
  "dev": "vite build --watch",
20
- "build": "vite build",
21
+ "build": "vite build && tsc",
21
22
  "test": "npm run test:e2e",
22
23
  "test:e2e": "start-server-and-test cy:playground http-get://localhost:3000/ cy:run",
23
24
  "test:e2e-watch": "start-server-and-test cy:playground http-get://localhost:3000/ cy:open",
@@ -27,17 +28,19 @@
27
28
  "prepublishOnly": "npm run build && cp ../README.md ./"
28
29
  },
29
30
  "dependencies": {
30
- "@storyblok/js": "^1.8.6",
31
+ "@storyblok/js": "^2.0.4",
31
32
  "camelcase": "^7.0.0"
32
33
  },
33
34
  "devDependencies": {
34
- "@cypress/vite-dev-server": "^2.0.7",
35
- "@rollup/plugin-dynamic-import-vars": "^1.4.4",
36
- "axios": "^0.27.2",
37
- "cypress": "^10.9.0",
35
+ "@cypress/vite-dev-server": "^5.0.2",
36
+ "@rollup/plugin-dynamic-import-vars": "^2.0.1",
37
+ "cypress": "^12.1.0",
38
38
  "eslint-plugin-cypress": "^2.12.1",
39
- "start-server-and-test": "^1.14.0",
40
- "vite": "^3.2.4"
39
+ "start-server-and-test": "^1.15.2",
40
+ "vite": "^4.0.1",
41
+ "typescript": "4.9.4",
42
+ "@types/node": "18.11.10",
43
+ "astro": "1.6.15"
41
44
  },
42
45
  "repository": {
43
46
  "type": "git",