@graphcms/html-to-slate-ast

中文文档
0.14.2发布于 2025/12/12MITTypeScriptESM + CJS近 30 天下载 5710

将 HTML 转换为 Hygraph 的 RichTextAST( slate)

中文使用文档
文档目录5 节
  1. 使用量
  2. 1. 安装
  3. 2. 转换数据
  4. 3. 在内容 API 变异中使用它
  5. 许可证

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

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

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

使用量

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

1. 安装

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

npm install [email protected] [email protected] @graphcms/html-to-slate-ast

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

2. 转换数据

如果你正在使用节点J, 你需要使用 htmlToSlateAST 函数,返回 a 答应如果您正在浏览器中使用此软件包,您可以使用 htmlToSlateASTSync 函数,它是同步的,不需要 jsdom。 。 。 .

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 GraphQL 类型并可用于导入您丰富的文本字段的内容。 以下是一个变异的例子:

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

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

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.

您可以使用 图克- 请求 将数据转换为 Hygraph 这里。 。 。 .

见关于 丰富的文本字段类型 财务报告和审定财务报表 内容 Api 变异。 。 。 .

许可证

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


由Hygraph制作 加入我们的社区来啊!

版本与发布记录

@graphcms/html-to-slate-ast 共发布 32 个版本,首次发布于 2021/06/23;过去 12 个月发布了 1 个版本。

版本发布日期距今
0.14.2latest2025/12/1211 个月前
0.14.12024/01/312 年前
0.14.02023/12/062 年前
0.14.0-canary.5canary2023/12/062 年前
0.14.0-canary.42023/12/062 年前
0.14.0-canary.32023/12/062 年前

关于 @graphcms/html-to-slate-ast 的常见问题

@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 和提交记录判断。