# @googleworkspace/google-docs-hast 中文文档

规范地址：https://npmcn.com/package/%40googleworkspace/google-docs-hast/

版本：1\.0\.5
npm 元数据更新时间：2024-03-29T13:37:35.261Z
中文译文更新时间：2026-11-16T20:41:21.257Z
软件包许可：Apache-2\.0

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

原始 npm 页面：https://www.npmjs.com/package/%40googleworkspace/google-docs-hast
原文对照：https://npmcn.com/package/%40googleworkspace/google-docs-hast/?lang=original
项目主页：https://github.com/googleworkspace/google-docs-hast
源码仓库：https://github.com/googleworkspace/google-docs-hast

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

- 包体积（解压后）：277 kB，44 个文件
- 模块格式：ES Module
- TypeScript 类型：包内自带
- 运行依赖：3 个
- 首次发布：2022-10-26T15:32:54.314Z
- 最新版本发布：2024-03-29T13:37:35.066Z
- 近 30 天下载量：8967（2026-10-17 至 2026-11-15，npm 官方下载统计）
- 已发布版本数：7（近 12 个月 0 个）

## 中文简介

将 Google Doc JSON 表达式转换为 HTML 抽象语法树 .

## 安装

```sh
npm install @googleworkspace/google-docs-hast
```

也可以使用：`pnpm add @googleworkspace/google-docs-hast`、`yarn add @googleworkspace/google-docs-hast`、`bun add @googleworkspace/google-docs-hast`

## README

![](https://npmcn.com/package/%40googleworkspace/google-docs-hast/assets/logo.jpg)

[![npm](https://img.shields.io/npm/v/@googleworkspace/google-docs-hast)](https://www.npmjs.com/package/@googleworkspace/google-docs-hast) [![Test](https://github.com/googleworkspace/google-docs-hast/actions/workflows/test.yml/badge.svg)](https://github.com/googleworkspace/google-docs-hast/actions/workflows/test.yml) ![Release](https://github.com/googleworkspace/google-docs-hast/workflows/Release/badge.svg) [![Docs](https://img.shields.io/badge/documentation-api-brightgreen)](https://googleworkspace.github.io/google-docs-hast/)

## 说明

将 Google Docs 文档的 JSON 代表转换为 [HTML 抽象语法树( HAST)](https://github.com/syntax-tree/hast) 它可以被序列化为HTML,也可以被转换为Markdown.

> **说明:** 这个图书馆的确是 **没有** 打算与Google Docs的渲染相匹配.

## 安装

利用国家预防机制或类似机制安装.

```sh
npm i @googleworkspace/google-docs-hast
```

## 使用量

```js
import { toHast } from "@googleworkspace/google-docs-hast";

// Retrieve document from API, https://developers.google.com/docs/api
const doc = ...;

// Convert the document to an HTML AST.
const tree = toHast(doc);
```

为了获得HTML AST的序列化代表,请使用 [重排字符串](https://www.npmjs.com/package/rehype-stringify) 软件包.

```js
import { unified } from "unified";
import rehypeStringify from "rehype-stringify";

// Convert the document to an HTML string.
const html = unified()
  .use(rehypeStringify, { collapseEmptyAttributes: true })
  .stringify(tree);
```

### 图像

全体 `<img>` 各项内容应作为 `src` 属性只在短时间内有效,并且属于模式 `https://lh6.googleusercontent.com/...`。 。 。 .

```js
import { visit } from "unist-util-visit";

visit(tree, (node) => {
  if (node.type === "element" && node.tagName === "img") {
    const { src } = node.properties;
    // download, store, and replace the src attribute
    node.properties.src = newSrc;
  }
});
```

### 命名样式

命名样式转换为匹配下表的 HTML 元素.

| 命名样式 | HTML 语言 |
| --- | --- |
| 标题 | `<h1 class="title"></h1>` |
| 字幕 | `<p class="subtitle"></p>` |
| 标题1 | `<h1 class="heading-1"></h1>` |
| 标题2 | `<h2 class="heading-2"></h2>` |
| 标题3 | `<h3 class="heading-3"></h3>` |
| 标题4 | `<h4 class="heading-4"></h4>` |
| 标题5 | `<h5 class="heading-5"></h5>` |
| 标题6 | `<h6 class="heading-6"></h6>` |
| 普通文本 | `<p class="normal-text"></p>` |

### 文本样式

文本样式转换为 HTML 元素 : `<i>`, (中文(简体) ), `<strong>`, (中文(简体) ), `<s>`, (中文(简体) ), `<sub>`, (中文(简体) ), `<sup>`,以及 `<u>`。 。 。 .

如果没有直接绘图,a `<span>` cSS用于支持文本颜色和字体等特性. 此选项可禁用 `{ styles: false }`。 。 。 .

### 锁定链接

标题标识在表格中 `id="h.wn8l66qm9m7y"` 当从 Google Docs API 导出时。 默认情况下,会更新标题标记ID,以匹配其文本内容. 见 [滑轮](https://www.npmjs.com/package/github-slugger) 关于如何做到这一点的更多信息.

例如,以下html:

```html
<h1 class="heading-1" id="h.wn8l66qm9m7y">A heading</h1>
```

改为:

```html
<h1 class="heading-1" id="a-heading">A heading</h1>
```

此选项可禁用 `{ prettyHeaderIds: false}`。 。 。 .

```js
const tree = toHast(doc, { prettyHeaderIds: false });
```

## 不支持的特性

Google Docs的一些功能目前不为这个库所支持. 这份清单并非详尽无遗.

| 类型 | 支助 | 错误 |
| --- | --- | --- |
| 适用于嵌入对象的样式,包括边框、旋转、透明度 | ❌ |  |
| `documentStyle` 包括页码、边距等 | ❌ |  |
| `namedStyles` (仅作为类名添加在相应的标签上) | ❌ |  |
| 页码 | ❌ |  |
| 分页符 | ❌ |  |
| 方程式 | ❌ |  |
| 列数 | ❌ |  |
| 建议 | ❌ |  |
| 书签 | ❌ |  |

> **说明:** 这个图书馆的确是 **没有** 打算与Google Docs的渲染相匹配.

* * *

这不是Google的官方产品.

## 常见问题

### @googleworkspace/google-docs-hast 是什么？

将 Google Doc JSON 表达式转换为 HTML 抽象语法树 \.

### 如何安装 @googleworkspace/google-docs-hast？

在项目目录运行 npm install @googleworkspace/google-docs-hast，或使用 pnpm add / yarn add / bun add。

### @googleworkspace/google-docs-hast 支持 TypeScript 吗？

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

### @googleworkspace/google-docs-hast 支持 ESM 还是 CommonJS？

以 ES Module 形式发布，推荐使用 import 引入。在 CommonJS 项目中，Node\.js 20\.19\+ / 22\.12\+ 可以直接 require\(\) 不含顶层 await 的 ES Module，更早的版本需要改用动态 import\(\)。

### @googleworkspace/google-docs-hast 可以商用吗？

@googleworkspace/google-docs-hast 使用 Apache-2\.0 许可。宽松许可：允许商业使用、修改和再分发，并附带明确的专利授权；分发时需保留许可与 NOTICE 文件，修改过的文件要注明改动。以上为概括，不构成法律意见，请以许可原文为准。

### @googleworkspace/google-docs-hast 的下载量有多少？

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

### @googleworkspace/google-docs-hast 还在维护吗？

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

