@rshackleton/astro-portabletext

0.1.0 • Public • Published

Portable Text Renderer for Astro

npm (scoped) semantic-release: angular

Render Portable Text block content with Astro components.

Example

---
// index.astro

import Code from './Code.astro';

// Get some data from Sanity, the "content" field should be a block array
// Ref: https://www.sanity.io/docs/block-type
const { content } = getPageData();
---

<PortableText
  components={{
    types: {
      code: Code,
    },
  }}
  value={content}
/>
---
// Code.astro

interface Props {
  portableText: {
    value: {
      content: string;
    };
  };
}

const { content } = Astro.props.portableText.value;
---

<pre>{content}</pre>

Framework Components (React, Vue, etc)

Whilst the PortableText component can only directly render Astro components, you can still use components from your preferred framework inside those Astro components.

// Code.tsx

type CodeProps = { content: string; }

const Code = (props: CodeProps) => {
  return (
    <pre>{props.content}</pre>
  );
}
---
// Code.astro

import Code from './Code.tsx';

interface Props {
  portableText: {
    value: {
      content: string;
    };
  };
}

const { content } = Astro.props.portableText.value;
---

<Code content={content} client:visible />

Credits

This package wouldn't exist without the official @portabletext/svelte package which provided an enormous amount of reference and inspiration.

License

MIT-licensed. See LICENSE.

Readme

Keywords

Package Sidebar

Install

npm i @rshackleton/astro-portabletext

Weekly Downloads

2

Version

0.1.0

License

mit

Unpacked Size

26.4 kB

Total Files

23

Last publish

Collaborators

  • rshackleton