@storyblok/astro 2.3.5 → 2.4.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
@@ -256,9 +256,10 @@ If you want to deploy a dedicated preview environment with the Bridge enabled, a
256
256
 
257
257
  ### Rendering Rich Text
258
258
 
259
- You can easily render rich text by using the `renderRichText` function that comes with `@storyblok/astro`. Then you can use the [`set:html` directive](https://docs.astro.build/en/reference/directives-reference/#sethtml):
259
+ You can easily render rich text by using either the `renderRichText` function or the `<RichTextRenderer />` component, both of which are included in `@storyblok/astro`.
260
+ Use `renderRichText`, which only supports parsing and returning native HTML tags, if you are not embedding `bloks` in your rich text. Then you can use the [`set:html` directive](https://docs.astro.build/en/reference/directives-reference/#sethtml):
260
261
 
261
- ```html
262
+ ```jsx
262
263
  ---
263
264
  import { renderRichText } from '@storyblok/astro';
264
265
 
@@ -270,6 +271,18 @@ const renderedRichText = renderRichText(blok.text)
270
271
  <div set:html="{renderedRichText}"></div>
271
272
  ```
272
273
 
274
+ Use the `<RichTextRenderer />` component if you are embedding `bloks` in your rich text:
275
+
276
+ ```jsx
277
+ ---
278
+ import { renderRichText, RichTextRenderer } from '@storyblok/astro';
279
+
280
+ const { blok } = Astro.props
281
+ ---
282
+
283
+ <RichTextRenderer richTextData="{blok.richtext}" />
284
+ ```
285
+
273
286
  You can also set a **custom Schema and component resolver** by passing the options as the second parameter of the `renderRichText` function:
274
287
 
275
288
  ```js
@@ -296,6 +309,37 @@ const renderedRichText = renderRichText(blok.text, {
296
309
  });
297
310
  ```
298
311
 
312
+ The same can be done with the `<RichTextRenderer />` component by passing along the options via the `richTextOptions` prop:
313
+
314
+ ```js
315
+ ---
316
+ import { RichTextSchema, renderRichText, RichTextRenderer } from "@storyblok/astro";
317
+ import cloneDeep from "clone-deep";
318
+
319
+ const mySchema = cloneDeep(RichTextSchema); // you can make a copy of the default RichTextSchema
320
+ // ... and edit the nodes and marks, or add your own.
321
+ // Check the base RichTextSchema source here https://github.com/storyblok/storyblok-js-client/blob/v4/source/schema.js
322
+
323
+ const { blok } = Astro.props;
324
+
325
+ const options = {
326
+ schema: mySchema,
327
+ resolver: (component, blok) => {
328
+ switch (component) {
329
+ case "my-custom-component":
330
+ return `<div class="my-component-class">${blok.text}</div>`;
331
+ break;
332
+ default:
333
+ return `Component ${component} not found`;
334
+ }
335
+ },
336
+ };
337
+ ---
338
+
339
+ <RichTextRenderer richTextData="{blok.richtext}" richTextOptions="{options}" />
340
+
341
+ ```
342
+
299
343
  ## API
300
344
 
301
345
  ### useStoryblokApi()
@@ -0,0 +1,32 @@
1
+ ---
2
+ import { renderRichText } from ".";
3
+ import type { ISbRichtext, SbBlokData, SbRichTextOptions } from "./dist/types";
4
+
5
+ import StoryblokComponent from "./StoryblokComponent.astro";
6
+
7
+ export interface Props {
8
+ richTextData?: ISbRichtext;
9
+ richTextOptions?: SbRichTextOptions;
10
+ }
11
+
12
+ const { richTextData, richTextOptions } = Astro.props;
13
+ ---
14
+
15
+ {
16
+ richTextData?.content?.map((richTextNode: ISbRichtext) => {
17
+ if (richTextNode.type === "blok") {
18
+ return richTextNode.attrs.body.map((blok: SbBlokData) => (
19
+ <StoryblokComponent blok={blok} />
20
+ ));
21
+ } else {
22
+ return (
23
+ <Fragment
24
+ set:html={renderRichText(
25
+ { type: richTextNode.type, content: [richTextNode] },
26
+ richTextOptions
27
+ )}
28
+ />
29
+ );
30
+ }
31
+ })
32
+ }
@@ -3,7 +3,7 @@ import type { AstroIntegration } from "astro";
3
3
  import type { ISbConfig, ISbRichtext, SbRichTextOptions } from "./types";
4
4
  export { storyblokEditable, loadStoryblokBridge, RichTextResolver, RichTextSchema, } from "@storyblok/js";
5
5
  export declare function useStoryblokApi(): StoryblokClient;
6
- export declare function renderRichText(data: ISbRichtext, options?: SbRichTextOptions): string;
6
+ export declare function renderRichText(data?: ISbRichtext, options?: SbRichTextOptions): string;
7
7
  export type IntegrationOptions = {
8
8
  /**
9
9
  * The access token from your space.
package/package.json CHANGED
@@ -1,13 +1,14 @@
1
1
  {
2
2
  "name": "@storyblok/astro",
3
- "version": "2.3.5",
3
+ "version": "2.4.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",
7
7
  "files": [
8
8
  "dist",
9
9
  "StoryblokComponent.astro",
10
- "FallbackComponent.astro"
10
+ "FallbackComponent.astro",
11
+ "RichTextRenderer.astro"
11
12
  ],
12
13
  "exports": {
13
14
  ".": {
@@ -16,7 +17,8 @@
16
17
  "require": "./dist/storyblok-astro.js"
17
18
  },
18
19
  "./StoryblokComponent.astro": "./StoryblokComponent.astro",
19
- "./FallbackComponent.astro": "./FallbackComponent.astro"
20
+ "./FallbackComponent.astro": "./FallbackComponent.astro",
21
+ "./RichTextRenderer.astro": "./RichTextRenderer.astro"
20
22
  },
21
23
  "types": "./dist/types/index.d.ts",
22
24
  "scripts": {