# @slate-serializers/html

> Convert Slate JSON to HTML and HTML to Slate. Built on htmlparser2 and shared DOM config with @slate-serializers/dom.

Latest version **2.8.5** (published 2026-10-24) · MIT license · 0 weekly downloads

## Install

```sh
npm install @slate-serializers/html
pnpm add @slate-serializers/html
yarn add @slate-serializers/html
bun add @slate-serializers/html
```

## Health

**Score 65/100 (B)** — status: active.

Positive: has types; no vulnerabilities; has provenance; recently updated; high maintenance score.

Warnings: low downloads; no esm support.

## Facts

| | |
|---|---|
| Version | 2.8.5 |
| Published | 2026-10-24 |
| First published | 2023-07-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 10 |
| Unpacked size | 67 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 94 |
| Maintainers | thompsonsj |
| Keywords | slate, slate-js, serializer, rich-text, html, slate-to-html, html-to-slate, htmlparser2, deserialize |

## Links

- npm: https://www.npmjs.com/package/@slate-serializers/html
- Repository: https://github.com/thompsonsj/slate-serializers
- Homepage: https://github.com/thompsonsj/slate-serializers/tree/main/packages/html#readme
- Issues: https://github.com/thompsonsj/slate-serializers/issues
- npm.io page: https://npm.io/package/@slate-serializers/html

## Dependencies (10)

- [slate](https://npm.io/package/slate.md) ^0.126.2
- [tslib](https://npm.io/package/tslib.md) 2.8.1
- [domutils](https://npm.io/package/domutils.md) ^4.0.0
- [css-select](https://npm.io/package/css-select.md) ^7.0.0
- [domhandler](https://npm.io/package/domhandler.md) ^6.0.0
- [htmlparser2](https://npm.io/package/htmlparser2.md) ^12.0.0
- [html-entities](https://npm.io/package/html-entities.md) ^2.6.0
- [dom-serializer](https://npm.io/package/dom-serializer.md) ^3.1.1
- [slate-hyperscript](https://npm.io/package/slate-hyperscript.md) ^0.125.0
- [@slate-serializers/dom](https://npm.io/package/@slate-serializers/dom.md) 2.8.5

## Alternatives

- [@tsparticles/shape-image](https://npm.io/package/@tsparticles/shape-image.md) — 303.7K weekly downloads
- [@tsparticles/shape-line](https://npm.io/package/@tsparticles/shape-line.md) — 233.7K weekly downloads
- [stringify-attributes](https://npm.io/package/stringify-attributes.md) — 58.6K weekly downloads
- [mobile-drag-drop](https://npm.io/package/mobile-drag-drop.md) — 46.3K weekly downloads
- [@comunica/actor-rdf-parse-html](https://npm.io/package/@comunica/actor-rdf-parse-html.md) — 29.2K weekly downloads

## Recent versions

- 2.8.5 (latest) — 2026-10-24
- 2.8.4 — 2026-10-23
- 2.8.3 — 2026-10-19
- 2.8.2 — 2026-10-10
- 2.8.1 — 2026-10-09
- 2.8.0 — 2026-10-02
- 2.7.0 — 2026-09-28
- 2.6.0 — 2026-09-15
- 2.5.5 — 2026-07-26
- 2.5.4 — 2026-07-20
- 2.5.3 — 2026-04-20
- 2.5.2 — 2026-04-20
- 2.5.1 — 2026-04-18
- 2.5.0 — 2026-04-18
- 2.4.0 — 2026-04-18
- … 28 more at https://npm.io/package/@slate-serializers/html/versions

## README

# @slate-serializers/html

Convert [Slate](https://www.npmjs.com/package/slate) JSON objects to HTML and vice versa.

**Docs:** [all packages](https://github.com/thompsonsj/slate-serializers/blob/main/README.md) · [demo](https://thompsonsj.github.io/slate-serializers-demo) — [slateToHtml](https://thompsonsj.github.io/slate-serializers-demo/slate-to-html/docs) · [htmlToSlate](https://thompsonsj.github.io/slate-serializers-demo/html-to-slate/docs)

## Table of contents

- [Engineering](#engineering)
- [Install](#install)
- [Usage](#usage)
  - [slateToHtml](#slatetohtml)
  - [htmlToSlate](#htmltoslate)
- [See also](#see-also)

## Engineering

For parser choices, whitespace, Payload round-trip limits, and entity encoding, see [Engineering decisions](https://github.com/thompsonsj/slate-serializers/blob/main/docs/engineering.md). `htmlToSlate` decodes entities into text; it does not keep `&amp;` in the Slate tree.

## Install

```bash
yarn add @slate-serializers/html
# or
npm install @slate-serializers/html
```

## Usage

### slateToHtml

```ts
import { slateToHtml } from '@slate-serializers/html'

const slate = [
  {
    children: [
      {
        text: 'Heading 1',
      },
    ],
    type: 'h1',
  },
  {
    children: [
      {
        text: 'Paragraph 1',
      },
    ],
    type: 'p',
  },
]

const serializedToHtml = slateToHtml(slate)
// output
// <h1>Heading 1</h1><p>Paragraph 1</p>
```

#### Configuration

By default, `slateToHtml` incorporates transformation rules based on the example in [Deserializing | Serializing | Slate](https://docs.slatejs.org/concepts/10-serializing#deserializing).

##### Payload CMS

If you are using [Payload CMS](https://payloadcms.com/), import the Payload configuration file and pass it as a parameter to the serializer.

```ts
import { slateToHtml, payloadSlateToHtmlConfig } from '@slate-serializers/html'

const slate = [
  {
    children: [
      {
        text: 'Heading 1',
      },
    ],
    type: 'h1',
  },
]

const serializedToHtml = slateToHtml(slate, payloadSlateToHtmlConfig)
```

##### Custom configuration

You can create your own configuration file that implements your schema. See [packages/dom/src/lib/config/payload.ts](https://github.com/thompsonsj/slate-serializers/blob/main/packages/dom/src/lib/config/payload.ts) for an example of how to extend the default configuration or copy [packages/dom/src/lib/config/default.ts](https://github.com/thompsonsj/slate-serializers/blob/main/packages/dom/src/lib/config/default.ts) and rewrite it as appropriate.

| Option  | Description | Default |
| - | - | - |
| `markMap` | Map Slate JSON properties to HTML formatting element tags. Accepts an array of HTML element tag names. | See [default config](https://github.com/thompsonsj/slate-serializers/blob/main/packages/dom/src/lib/config/default.ts). Example: `{ code: ['code'], /* ... */ }` |
| `elementMap` | Map Slate JSON `type` values to HTML element tags. Use `elementTransforms` for more control over the returned element. | See [default config](https://github.com/thompsonsj/slate-serializers/blob/main/packages/dom/src/lib/config/default.ts). Example: `{ paragraph: 'p', /* ... */ }` |
| `markTransforms` | Define transform functions for Slate JSON properties. Overrides and corresponding values in `markMap`. Functions return a [`domhandler` `Element`](https://github.com/fb55/domhandler); use `import { Element } from '@slate-serializers/dom'`. | See [default config](https://github.com/thompsonsj/slate-serializers/blob/main/packages/dom/src/lib/config/default.ts). |
| `elementTransforms` | Define transform functions for Slate JSON node types. Overrides and corresponding values in `elementMap`. | See [default config](https://github.com/thompsonsj/slate-serializers/blob/main/packages/dom/src/lib/config/default.ts). |
| `encodeEntities` | See [cheeriojs/dom-serializer - encodeEntities](https://github.com/cheeriojs/dom-serializer#encodeentities) | `false` |
| `alwaysEncodeBreakingEntities` | Encode `&`, `<` and `>` regardless of other option settings. | `true` |
| `alwaysEncodeCodeEntities` | Encode entities in `<code>` and `<pre>` tags regardless of other option settings. | `false` |
| `convertLineBreakToBr` | Convert `\n` line breaks in Slate text nodes to an HTML `<br>` element. | `false` |


### htmlToSlate

```ts
import { htmlToSlate } from '@slate-serializers/html'

const html = `<h1>Heading 1</h1><p>Paragraph 1</p>`

const serializedToSlate = htmlToSlate(html)
// output
/*
[
  {
    children: [
      {
        text: 'Heading 1',
      },
    ],
    type: 'h1',
  },
  {
    children: [
      {
        text: 'Paragraph 1',
      },
    ],
    type: 'p',
  },
]
*/
```

#### Configuration

By default, `htmlToSlate` incorporates transformation rules based on the example in [HTML | Serializing | Slate](https://docs.slatejs.org/concepts/10-serializing#html).

##### Payload CMS

If you are using [Payload CMS](https://payloadcms.com/), import the Payload configuration file and pass it as a parameter to the serializer.

```ts
import { htmlToSlate, payloadHtmlToSlateConfig } from '@slate-serializers/html'

const html = `<h1>Heading 1</h1><p>Paragraph 1</p>`

const serializedToSlate = htmlToSlate(html, payloadHtmlToSlateConfig)
```

#### Options

Create your own configuration file that implements your schema.

- See [packages/html/src/lib/serializers/htmlToSlate/config/payload.ts](https://github.com/thompsonsj/slate-serializers/blob/main/packages/html/src/lib/serializers/htmlToSlate/config/payload.ts) for an example of how to extend the default configuration; or
- copy [packages/html/src/lib/serializers/htmlToSlate/config/default.ts](https://github.com/thompsonsj/slate-serializers/blob/main/packages/html/src/lib/serializers/htmlToSlate/config/default.ts) and rewrite it as appropriate.

For more detailed documentation and examples, see [htmlToSlate | `slate-serializers`](https://thompsonsj.github.io/slate-serializers-demo/html-to-slate/docs).

| Option  | Description | Default |
| - | - | - |
| `textTags` | Define transform functions for HTML formatting elements. | See [default config](https://github.com/thompsonsj/slate-serializers/blob/main/packages/html/src/lib/serializers/htmlToSlate/config/default.ts). Examples: [textTags.spec.ts](https://github.com/thompsonsj/slate-serializers/blob/main/packages/html/src/lib/tests/htmlToSlate/configuration/textTags.spec.ts). |
| `elementTags` | Define transform functions for HTML element tag names. | See [default config](https://github.com/thompsonsj/slate-serializers/blob/main/packages/html/src/lib/serializers/htmlToSlate/config/default.ts). Example `{ p: () => ({ type: 'p' }), /* ... */ }`. |
| `htmlPreProcessString` | Perform operations on the HTML string before serialization. | See [default config](https://github.com/thompsonsj/slate-serializers/blob/main/packages/html/src/lib/serializers/htmlToSlate/config/default.ts) (replaces `<pre>` with `<code>` for parsing). |
| `filterWhitespaceNodes` | Remove whitespace that does not contribute meaning. | `true` |
| `convertBrToLineBreak` | Convert br tags to line breaks in Slate. | `true` |
| `brStrategy` | How converted `<br>` tags are represented: `block` (default, historical empty-block / context `\n` behavior) or `newline` (always `\n`, coalesce plain text, collapse breaks before a following block). | `'block'` |
| `trimWhiteSpace` | Collapse runs of whitespace in text to a single space. | `true` |
| `liftWrappedBlocks` | When an unmapped wrapper (`div`, `section`, `body`, …) contains only blocks, lift those blocks to the top level. | `true` |

## See also

- [Repository config docs](https://github.com/thompsonsj/slate-serializers/tree/main/docs/config) — Markdown companions to the tables above.
- [SlateToReact](https://www.npmjs.com/package/@slate-serializers/react) — React output with the same option keys as `slateToHtml`; see [flat config](https://github.com/thompsonsj/slate-serializers/blob/main/docs/config/slateToReact.md).

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