-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathBlockWithRenderFunction.astro
39 lines (35 loc) · 1.11 KB
/
BlockWithRenderFunction.astro
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
---
/**
* Works with `v0.11.0+`
*
* This `Block` component demonstrates how to customize Portable Text block rendering
* using the `render` function from `usePortableText`.
*
* `render` allows fine-grained control over child node rendering and can be used
* within `block`, `list`, `listItem`, and `mark` components as they contain child nodes.
*
* This example customizes the rendering of `h1` blocks to replace the word "rocket"
* with a 🚀 emoji.
*/
// @ts-nocheck
import type { BlockProps } from "astro-portabletext/types";
import { usePortableText } from "astro-portabletext";
type Props = BlockProps;
const { node, isInline, index, ...attrs } = Astro.props;
const { getDefaultComponent, render } = usePortableText(node);
const styleIs = (style: string) => style === node.style;
const DefaultBlock = getDefaultComponent(); // Returns `astro-portabletext` Block component
---
{
styleIs("h1") ? (
<h1 {...attrs}>
{render({
text: ({ props }) => props.node.text.replace("rocket", "🚀"),
})}
</h1>
) : (
<DefaultBlock {...Astro.props}>
<slot />
</DefaultBlock>
)
}