# @graphcms/html-to-slate-ast 中文文档

规范地址：https://npmcn.com/package/%40graphcms/html-to-slate-ast/

版本：0\.14\.2
npm 元数据更新时间：2025-12-12T12:04:55.908Z
中文译文更新时间：2026-11-17T06:23:45.229Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40graphcms/html-to-slate-ast
原文对照：https://npmcn.com/package/%40graphcms/html-to-slate-ast/?lang=original
项目主页：https://github.com/hygraph/rich-text#readme
源码仓库：https://github.com/hygraph/rich-text

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：75.5 kB，9 个文件
- 模块格式：ESM + CJS
- TypeScript 类型：包内自带
- 运行依赖：2 个
- peer 依赖：jsdom ^24\.0\.0，slate ^0\.66\.1，slate-hyperscript ^0\.67\.0
- 首次发布：2021-06-23T12:00:59.338Z
- 最新版本发布：2025-12-12T10:41:17.340Z
- 近 30 天下载量：5710（2026-10-17 至 2026-11-15，npm 官方下载统计）
- 已发布版本数：32（近 12 个月 1 个）

## 中文简介

将 HTML 转换为 Hygraph 的 RichTextAST( slate)

## 安装

```sh
npm install @graphcms/html-to-slate-ast
```

也可以使用：`pnpm add @graphcms/html-to-slate-ast`、`yarn add @graphcms/html-to-slate-ast`、`bun add @graphcms/html-to-slate-ast`

## README

# @graphcms/html-to-slate-ast (英语)

HTML 到Hygraph 的 RichTextAST 格式的 Slate AST 转换器.

> QQ 此转换器输出Hygraph 中使用的 Slate AST 的自定义风味, 并且很可能不会输出一个与您自己的 Slate 执行相容的输出 。 但可以随意把它叉出并适应你的需要.

## 使用量

> 说明: 如果使用这个带Node.js的软件包,则需要使用18版或更高版本.

### 1\. 安装

这个软件包需要有软件包 `slate` 财务报告和审定财务报表 `slate-hyperscript` 已安装,以及 `jsdom` 也可以在节点中运行转换器.

```bash
npm install slate@0.66.1 slate-hyperscript@0.67.0 @graphcms/html-to-slate-ast

# for Node.js or isomorphic usage, jsdom is required
npm install jsdom
```

### 2\. 转换数据

如果你正在使用节点J, 你需要使用 `htmlToSlateAST` 函数,返回 a [答应](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises)如果您正在浏览器中使用此软件包,您可以使用 `htmlToSlateASTSync` 函数,它是同步的,不需要 `jsdom`。 。 。 .

```js
import { htmlToSlateAST } from '@graphcms/html-to-slate-ast';
// Or if you are using this package in the browser
import { htmlToSlateASTSync } from '@graphcms/html-to-slate-ast';

async function main() {
  const htmlString = '<div><p>test</p></div>'; // or import from a file or database
  const ast = await htmlToSlateAST(htmlString);
  console.log(JSON.stringify(ast, null, 2));
}

main()
  .then(() => process.exit(0))
  .catch(e => console.error(e));
```

### 3\. 在内容 API 变异中使用它

这种对话的输出与我们 [`RichTextAST`](https://hygraph.com/docs/api-reference/content-api/rich-text-field) GraphQL 类型并可用于导入您丰富的文本字段的内容。 以下是一个变异的例子:

```graphql
mutation newArticle($title: String!, $content: RichTextAST) {
  createArticle(data: { title: $title, content: $content }) {
    id
    title
    content {
      html
      raw
    }
  }
}
```

生成的产出 `htmlToSlateAST` _将代表 `children` 数组_ 会 议 日 程 和 议 程 [列表编辑器对象](https://docs.slatejs.org/api/nodes/editor)然而,在创建或更新 Rich 文本字段的值时,您正在设定编辑器节点本身的值。 这意味着输出应该被转化为富文本兼容对象,例如:

```js
const data = await client.request(newArticleQuery, {
  title: 'Example title for an article',
  content: { children: ast },
});
```

这里,在Slate方面, `$content` 是编辑器节点,所以 `$ast` 数组必须指定为 `children` 键。 如果你不把它交给 `children` 键,该变异将因以下错误而失败.

```
ClientError: could not transform richText: Values should be an array of objects containing raw rich text values.
```

您可以使用 [图克- 请求](https://github.com/prisma-labs/graphql-request) 将数据转换为 Hygraph [这里](https://github.com/hygraph/rich-text/blob/main/packages/html-to-slate-ast/examples/graphql-request-script.js)。 。 。 .

见关于 [丰富的文本字段类型](https://hygraph.com/docs/schema/field-types#rich-text) 财务报告和审定财务报表 [内容 Api 变异](https://hygraph.com/docs/content-api/mutations)。 。 。 .

## 许可证

根据麻省理工学院的许可.

* * *

由Hygraph制作 [加入我们的社区](https://slack.hygraph.com/)来啊!

## 常见问题

### @graphcms/html-to-slate-ast 是什么？

将 HTML 转换为 Hygraph 的 RichTextAST\( slate\)

### 如何安装 @graphcms/html-to-slate-ast？

在项目目录运行 npm install @graphcms/html-to-slate-ast，或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖：slate（^0\.66\.1）、slate-hyperscript（^0\.67\.0）。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖，Yarn 需要手动安装。

### @graphcms/html-to-slate-ast 支持 TypeScript 吗？

支持。包内自带 TypeScript 类型声明（package\.json 声明了 types 入口），安装后即可获得类型提示，无需再安装 @types 包。

### @graphcms/html-to-slate-ast 支持 ESM 还是 CommonJS？

同时提供 ES Module 与 CommonJS 入口，import 和 require\(\) 都可以使用，打包工具会自动选择合适的版本。

### @graphcms/html-to-slate-ast 可以商用吗？

@graphcms/html-to-slate-ast 使用 MIT 许可。宽松许可：允许商业使用、修改和再分发，通常只需在分发时保留原版权与许可声明。以上为概括，不构成法律意见，请以许可原文为准。

### @graphcms/html-to-slate-ast 的下载量有多少？

根据 npm 官方下载统计，@graphcms/html-to-slate-ast 在 2026/10/17 – 2026/11/15 这 30 天内被下载 5,710 次，日均约 190 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @graphcms/html-to-slate-ast 还在维护吗？

最新版本 0\.14\.2 发布于 2025/12/12（11 个月前）；至今共发布 32 个版本，其中过去 12 个月发布了 1 个；首次发布于 2021/06/23。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

