# @portabletext/block-tools

> Sanity-flavored HTML to Portable Text conversion (wraps @portabletext/html)

Latest version **6.0.1** (published 2026-09-15) · MIT license · 0 weekly downloads

## Install

```sh
npm install @portabletext/block-tools
pnpm add @portabletext/block-tools
yarn add @portabletext/block-tools
bun add @portabletext/block-tools
```

## Health

**Score 55/100 (C)** — status: active.

Positive: no vulnerabilities; recently updated; high maintenance score.

Warnings: low downloads; no types; no esm support.

## Facts

| | |
|---|---|
| Version | 6.0.1 |
| Published | 2026-09-15 |
| First published | 2025-01-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 271 |
| Author | Sanity.io |
| Maintainers | kmelve, rexxars, skogsmaskin, mariuslundgard, bjoerge, rbotten, sanity-io, snorreeb, josef-sanity, stipsan, christianhg |
| Keywords | block-tools, portable-text |

## Links

- npm: https://www.npmjs.com/package/@portabletext/block-tools
- Repository: https://github.com/portabletext/editor
- Homepage: https://www.sanity.io/
- Issues: https://github.com/portabletext/editor/issues
- npm.io page: https://npm.io/package/@portabletext/block-tools

## Alternatives

- [@lexical/table](https://npm.io/package/@lexical/table.md) — 3.0M weekly downloads
- [mantine-datatable](https://npm.io/package/mantine-datatable.md) — 98.2K weekly downloads
- [react-native-collapsible-tab-view](https://npm.io/package/react-native-collapsible-tab-view.md) — 70.6K weekly downloads
- [@handsontable/vue3](https://npm.io/package/@handsontable/vue3.md) — 16.1K weekly downloads
- [vuewordcloud](https://npm.io/package/vuewordcloud.md) — 7.2K weekly downloads

## Recent versions

- 6.0.1 (latest) — 2026-09-15
- 2.0.0-canary.0 (canary) — 2025-07-17
- 6.0.0 — 2026-08-25
- 5.2.0 — 2026-08-17
- 5.1.14 — 2026-08-10
- 5.1.13 — 2026-08-10
- 5.1.12 — 2026-07-21
- 5.1.11 — 2026-07-10
- 5.1.10 — 2026-07-09
- 5.1.9 — 2026-06-25
- 5.1.8 — 2026-06-24
- 5.1.7 — 2026-06-19
- 5.1.6 — 2026-06-19
- 5.1.5 — 2026-06-18
- 5.1.4 — 2026-06-11
- … 102 more at https://npm.io/package/@portabletext/block-tools/versions

## README

# `@portabletext/block-tools`

> Sanity-flavored HTML to Portable Text conversion

This package wraps [`@portabletext/html`](../html) for use with Sanity schemas. If you're not using Sanity's schema system, use `@portabletext/html` directly - it has the same features with a simpler API.

## When to use which

| Package                     | Use when                                                        |
| --------------------------- | --------------------------------------------------------------- |
| `@portabletext/html`        | Standalone projects, custom schemas, or any non-Sanity context  |
| `@portabletext/block-tools` | Sanity projects where you already have a compiled Sanity schema |

## Usage

```ts
import {htmlToBlocks} from '@portabletext/block-tools'
import {Schema} from '@sanity/schema'

const defaultSchema = Schema.compile({
  name: 'myBlog',
  types: [
    {
      type: 'object',
      name: 'blogPost',
      fields: [
        {
          title: 'Title',
          type: 'string',
          name: 'title',
        },
        {
          title: 'Body',
          name: 'body',
          type: 'array',
          of: [{type: 'block'}],
        },
      ],
    },
  ],
})

const blockContentType = defaultSchema
  .get('blogPost')
  .fields.find((field) => field.name === 'body').type

const blocks = htmlToBlocks(
  '<html><body><h1>Hello world!</h1></body></html>',
  blockContentType,
)
```

### `htmlToBlocks(html, blockContentType, options?)`

Converts HTML to Portable Text blocks using a Sanity block content schema type.

Internally delegates to `@portabletext/html` after converting the Sanity schema. Supports the same `parseHtml`, `rules`, and `keyGenerator` options. See the [`@portabletext/html` README](../html/README.md) for full documentation on rules, whitespace handling, image matchers, and paste source support.

**NOTE:** To use in Node.js, you need to provide a `parseHtml` option - generally using `JSDOM`:

```ts
import {JSDOM} from 'jsdom'

const blocks = htmlToBlocks(html, blockContentType, {
  parseHtml: (html) => new JSDOM(html).window.document,
})
```

### `normalizeBlock(block, options?)`

Normalize a block object structure to ensure it has `_key`, `_type`, `children`, and `markDefs`.

```ts
import {normalizeBlock} from '@portabletext/block-tools'

const normalized = normalizeBlock({
  _type: 'block',
  children: [{_type: 'span', text: 'Hello', marks: ['strong']}],
})
// => { _key: '...', _type: 'block', children: [{ _key: '...', _type: 'span', text: 'Hello', marks: ['strong'] }], markDefs: [] }
```

## License

MIT

---
_Source: https://npm.io/package/@portabletext/block-tools · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
