# `@portabletext/sanity-bridge`

Converts between Sanity schemas and Portable Text schemas.

The main job is `sanitySchemaToPortableTextSchema`. A compiled Sanity
schema is lazy: types resolve through getters and class instances, and
finding out what is allowed inside a Portable Text field means walking
that graph. The editor wants the opposite, one plain, self-contained
object it can read synchronously, serialize, and hand to renderers and
plugins without knowing anything about Sanity. So the bridge walks the
type graph once, up front, and expands it into exactly that: which
styles, lists, decorators, annotations, block objects, and inline
objects a field allows, with every nested block inside a container
carrying the restrictions that apply at that exact position.

Expanding up front is also why the output can be bigger than the schema
that produced it. A type shows up in the output at every position that
embeds it, and self-referencing types are cut off rather than expanded
forever (see "Recursive schemas" below).

## Installation

```bash
npm install @portabletext/sanity-bridge
```

## Usage

### Convert Sanity schema to Portable Text schema

```ts
import {sanitySchemaToPortableTextSchema} from '@portabletext/sanity-bridge'
import {Schema} from '@sanity/schema'
import {defineField, defineType} from '@sanity/types'

/**
 * Define the Sanity Schema
 */

const imageType = defineType({
  name: 'custom image',
  title: 'Image',
  type: 'object',
  fields: [
    defineField({
      name: 'url',
      type: 'string',
    }),
  ],
})

const stockTickerType = defineType({
  name: 'stock-ticker',
  type: 'object',
  fields: [
    defineField({
      name: 'symbol',
      type: 'string',
    }),
  ],
})

const portableTextType = defineType({
  type: 'array',
  name: 'body',
  of: [
    {
      type: 'block',
      name: 'block',
      styles: [
        {title: 'Normal', value: 'normal'},
        {title: 'H1', value: 'h1'},
        {title: 'H2', value: 'h2'},
        {title: 'H3', value: 'h3'},
        {title: 'H4', value: 'h4'},
        {title: 'H5', value: 'h5'},
        {title: 'H6', value: 'h6'},
        {title: 'Quote', value: 'blockquote'},
      ],
      marks: {
        annotations: [
          {
            name: 'comment',
            type: 'object',
            fields: [{type: 'string', name: 'text'}],
          },
          {
            name: 'link',
            type: 'object',
            fields: [{type: 'string', name: 'href'}],
          },
        ],
      },
      of: [{type: 'stock-ticker'}],
    },
    {type: 'custom image'},
  ],
})

/**
 * Compile the Sanity Schema
 */
const sanitySchema = Schema.compile({
  types: [portableTextType, imageType, stockTickerType],
}).get('body')

/**
 * Turn the Sanity Schema into a Portable Text Schema
 */
const portableTextSchema = sanitySchemaToPortableTextSchema(sanitySchema)
```

#### Container sub-schemas

When a block object holds an array field with a nested `{type: 'block'}`
member (a code block, callout, table cell, ...), the nested block is
resolved from its own Sanity definition: its styles, decorators,
annotations, lists, and inline objects come from that block as Sanity
compiles it, not from the top-level block.

```ts
const codeBlockType = defineType({
  type: 'object',
  name: 'code-block',
  fields: [
    defineField({
      type: 'array',
      name: 'lines',
      of: [
        // A code line: only the `code` style, no marks, no inline objects.
        {
          type: 'block',
          styles: [{title: 'Code', value: 'code'}],
          lists: [],
          marks: {decorators: [], annotations: []},
          of: [],
        },
      ],
    }),
  ],
})
```

In the converted schema the `code-block`'s line carries those restrictions,
so an editor that gates on the sub-schema offers no decorators, annotations,
or headings inside it.

Sanity has no notion of a nested block inheriting from an enclosing block, so
an undeclared property resolves to Sanity's defaults for that block. Declare
whatever a container should allow on the block member itself. The resulting
`Schema` follows the resolution rules documented in
[`@portabletext/schema`](../schema/README.md#containers-and-sub-schemas).

#### Recursive schemas

Schemas are allowed to reference themselves. An accordion whose body is
the same Portable Text array it lives in, or a footnote that carries a
rich-text field of its own, both convert fine. The conversion doesn't
loop forever trying to expand them.

When the conversion runs into a type it is already in the middle of
expanding, it stops and leaves a marker instead of expanding again:

- A named object type becomes a bare reference, just `{type: 'accordion'}`.
  The full declaration still exists at the top level, and that is where
  the editor looks it up.
- An inline object or annotation that loops back into itself gets empty
  `fields` at the point of repetition. Everything up to that point is
  fully declared.

One thing to keep in mind: every place a type is embedded gets its own
resolved sub-schema, so a schema where many types embed each other
produces output that grows with the number of embedding positions, not
just the number of types.

That growth is steepest when rich-text fields sit inside the inline
objects they list, because every nested block expands its inline objects
again. When the full conversion would expand inline objects and
annotations more than 100,000 times, the schema converts to a compact
form instead. Up to that, the output is the full conversion.

The compact form differs from the full conversion in two ways:

- A nested block whose annotations and inline objects both match the
  root block's leaves both out, but only where `compileSchema`, and
  `getSubSchema` called on the output without `compileSchema`, both fill
  them in from the root's lists. Elsewhere it keeps them, for example
  directly inside an array without a block member, next to a block that
  lists different types, or inside an object type listed in an array.
- Each inline object and annotation is expanded once and reused at the
  positions that list it. Some positions deep inside recursive
  structures resolve an object type with no fields, so content inserted
  there loses those fields.

### Convert an uncompiled definition

An uncompiled array definition only carries the types nested inside it.
When one of its fields references a sibling type by name, convert it with
`sanitySchemaDefinitionToPortableTextSchema` and pass the siblings through
`options.types`:

```ts
import {sanitySchemaDefinitionToPortableTextSchema} from '@portabletext/sanity-bridge'
import {defineField, defineType} from '@sanity/types'

const customUrl = defineType({
  name: 'customUrl',
  type: 'object',
  fields: [defineField({name: 'href', type: 'url'})],
})

const richText = defineType({
  name: 'richText',
  type: 'array',
  of: [
    {
      type: 'block',
      name: 'block',
      marks: {
        annotations: [
          {
            name: 'customLink',
            type: 'object',
            fields: [{name: 'link', type: 'customUrl'}],
          },
        ],
      },
    },
  ],
})

const portableTextSchema = sanitySchemaDefinitionToPortableTextSchema(
  richText,
  {types: [customUrl]},
)
```

`options.types` can be the complete schema type list, including the
portable text field itself. When a reference cannot be resolved, the
thrown error names the missing type.

### Keep hold of the original Sanity types

The expanded Portable Text schema is what the editor runs on, but
Studio-side code usually wants the original Sanity types back: they
carry everything the expansion drops, validation rules, previews,
custom components. `createPortableTextMemberSchemaTypes` buckets the
members of a Portable Text array into their Sanity types, so render
callbacks and inputs can join back by name and work with the real
thing.

```ts
import {createPortableTextMemberSchemaTypes} from '@portabletext/sanity-bridge'

const memberTypes = createPortableTextMemberSchemaTypes(sanitySchema)

memberTypes.blockObjects // the original Sanity types, not conversions
memberTypes.inlineObjects
memberTypes.annotations
```

### Resolve the Sanity types at a position

Inside containers, what a position allows depends on where it sits: a
table cell's blocks are not the root's blocks. `getSanitySubSchema`
answers "which Sanity types apply here?" for a path into a value,
walking down to the nearest Portable-Text-shaped ancestor and
bucketizing its members.

```ts
import {getSanitySubSchema} from '@portabletext/sanity-bridge'

const subSchema = getSanitySubSchema(sanitySchema, value, [
  {_key: 'table1'},
  'rows',
  {_key: 'row1'},
  'cells',
  {_key: 'cell1'},
  'content',
  {_key: 'block1'},
])
```

This is the Sanity-side counterpart of the editor's own sub-schema
resolution over the expanded schema. The two answer the same question
in their respective type universes and are meant to agree position for
position.
