astro-portabletext
v1.0.1
Published
A flexible and customizable library for rendering Portable Text content in your Astro projects
Maintainers
Readme
astro-portabletext
A flexible library for rendering Portable Text content in Astro projects.
Prerequisites:
- Astro (
>=4.6.0) - Node.js (
>=22.12.0)
Installation:
npm install astro-portabletextFeatures
- Core components for common rich text elements.
- Custom rendering using
slotsor your owncomponents. - Flexible control with the
renderfunction viausePortableText. - Built with TypeScript support.
Demonstration
View live examples:
Resources
Usage
Display rich text with sensible defaults, or customize the output using components and/or slots.
Components
---
import { PortableText } from "astro-portabletext";
const payload = [
// Portable Text payload
];
const components = {
// Custom components
};
---
<PortableText value={payload} {components} />Read the custom components documentation
Slots
---
import { PortableText } from "astro-portabletext";
const payload = [
// Portable Text payload
];
const components = {
// Optional: custom components
};
---
<PortableText value={payload} {components}>
<fragment slot="mark">
{({ Component, props, children }) => (
<Component {...props} class="mark">{children}</Component>
)}
</fragment>
</PortableText>
<style>
.mark:where(strong) {
/* styles */
}
</style>API
PortableText component properties
| Property | Type | Description |
| -------------------- | ----------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------- |
| value | array | object | Portable Text payload. |
| components | object | Optional. Map components to node types or properties. |
| onMissingComponent | function | boolean | Optional. Handle unknown types or disable warnings. Defaults to console warnings. |
| listNestingMode | "html" | "direct" | Optional. List nesting mode. Defaults to html. See ToolkitListNestMode |
Utilities
Import utility functions directly from the package root.
import {
mergeComponents,
spanToPlainText,
toPlainText,
usePortableText,
} from "astro-portabletext";Read the utility functions documentation
Types
Import TypeScript types from the types export.
import type { ... } from "astro-portabletext/types";Read the type definitions documentation
Contributing
We welcome contributions to improve astro-portabletext!
If you find a bug or have a feature request, please open an issue. If you'd like to contribute code, feel free to submit a pull request.
