![Meet Socket at BlackHat and DEF CON in Las Vegas](https://cdn.sanity.io/images/cgdhsj6q/production/4a3876139ffd3878bb3e7800a14cf4044245dca7-1080x834.jpg?w=400&fit=max&auto=format)
Security News
Meet Socket at BlackHat and DEF CON in Las Vegas
Come meet the Socket team at BlackHat and DEF CON! We're sponsoring some fun networking events and we would love to see you there.
@portabletext/types
Advanced tools
Package description
@portabletext/types is a TypeScript library that provides type definitions for Portable Text, a JSON-based rich text format. It helps developers to work with Portable Text data structures in a type-safe manner.
Type Definitions for Portable Text
This feature provides type definitions for Portable Text blocks, ensuring that the data structure adheres to the expected format. The code sample demonstrates how to define a Portable Text block with a single span child.
import { PortableTextBlock } from '@portabletext/types';
const exampleBlock: PortableTextBlock = {
_type: 'block',
_key: 'a1b2c3',
children: [
{
_type: 'span',
_key: 'd4e5f6',
text: 'Hello, world!',
marks: []
}
],
markDefs: [],
style: 'normal'
};
Type Definitions for Annotations and Marks
This feature provides type definitions for annotations and marks within Portable Text. The code sample shows how to define a link annotation with a URL.
import { PortableTextMarkDefinition } from '@portabletext/types';
const exampleMarkDef: PortableTextMarkDefinition = {
_type: 'link',
_key: 'g7h8i9',
href: 'https://example.com'
};
Type Definitions for Custom Portable Text Objects
This feature allows developers to define custom Portable Text objects with specific properties. The code sample demonstrates how to define a custom image object with a URL and alt text.
import { PortableTextObject } from '@portabletext/types';
const exampleCustomObject: PortableTextObject = {
_type: 'image',
_key: 'j1k2l3',
url: 'https://example.com/image.jpg',
alt: 'An example image'
};
Slate is a completely customizable framework for building rich text editors. Unlike @portabletext/types, which focuses on type definitions for a specific JSON-based format, Slate provides a more comprehensive solution for creating and managing rich text content, including a React-based editor.
Draft.js is a JavaScript rich text editor framework, built by Facebook. It provides a set of tools for building rich text editors, including type definitions and utilities for managing content state. While @portabletext/types focuses on type safety for Portable Text, Draft.js offers a more extensive set of features for building and managing rich text editors.
ProseMirror is a toolkit for building rich text editors with a focus on extensibility and customizability. It provides a robust set of tools for managing document state and rendering content. Compared to @portabletext/types, ProseMirror offers a more comprehensive solution for building rich text editors, including a wide range of plugins and extensions.
Readme
TypeScript types for Portable Text
npm install --save @portabletext/types
See https://portabletext.github.io/types/
import type {
PortableTextBlock,
PortableTextSpan,
PortableTextLink
} from '@portabletext/types'
const headingSpan: PortableTextSpan = {
_type: 'span',
_key: '5p4n',
text: 'A simple Portable Text heading block',
marks: [],
}
const myBlocks: PortableTextBlock[] = [
{
_type: 'block',
_key: 'abc123',
style: 'h1',
children: [headingSpan],
markDefs: [],
},
{
_type: 'block',
_key: 'xyz987',
style: 'normal',
children: [
{_type: 'span', _key: 'c7', text: 'Check out the ', marks: []}
{_type: 'span', _key: 'x2', text: 'TypeScript definitions', marks: ['m4hl1nk']},
{_type: 'span', _key: 'u5', text: ' if you are using TS!', marks: []}
],
markDefs: [
{
_key: 'm4hl1nk',
_type: 'link',
href: 'https://github.com/portabletext/types'
}
],
},
]
/**
* Very specific Portable Text block, where:
* - Only link and author references (custom) are allowed as marks
* - Only Portable Text spans and `localCurrency` objects can be inline children
* - Blocks can only be of style `normal` or `call-to-action` (custom)
* - List items can only be of type `bullet`
*/
type MyCustomBlock = PortableTextListItemBlock<
PortableTextLink | AuthorReference, // `M`: Marks
PortableTextSpan | LocalCurrency, // `C`: Children
'normal' | 'call-to-action', // `S`: Styles
'bullet' // `L`: List item types
>
MIT © Sanity.io
FAQs
Shared TypeScript definitions for core Portable Text data structures
We found that @portabletext/types demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 9 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Come meet the Socket team at BlackHat and DEF CON! We're sponsoring some fun networking events and we would love to see you there.
Security News
Learn how Socket's 'Non-Existent Author' alert helps safeguard your dependencies by identifying npm packages published by deleted accounts. This is one of the fastest ways to determine if a package may be abandoned.
Security News
In July, the Python Software Foundation mounted a quick response to address a leaked GitHub token, elected new board members, and added more members to the team supporting PSF and PyPI infrastructure.