@googleworkspace/google-docs-hast

中文文档
1.0.5发布于 2024/03/29Apache-2.0TypeScriptES Module近 30 天下载 8967

Convert a Google Doc JSON representation to an HTML abstract syntax tree.

原始使用文档
文档目录8 节
  1. Description
  2. Install
  3. Usage
  4. Images
  5. Named styles
  6. Text styles
  7. Anchor links
  8. Unsupported Features

npm Test Release Docs

Description

Converts the JSON representation of a Google Docs document into an HTML abstract syntax tree (HAST) which can be serialized to HTML or converted to Markdown.

Note: This library does not intend to match the rendering by Google Docs.

Install

Install using NPM or similar.

npm i @googleworkspace/google-docs-hast

Usage

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);

To get the serialized representation of the HTML AST, use the rehype-stringify package.

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);

Images

All <img> elements should be post-processed as the src attribute is only valid for a short time and is of the pattern https://lh6.googleusercontent.com/....

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;
  }
});

Named styles

Named styles are converted to an HTML element matching the following table.

Named StyleHTML
Title<h1 class="title"></h1>
Subtitle<p class="subtitle"></p>
Heading 1<h1 class="heading-1"></h1>
Heading 2<h2 class="heading-2"></h2>
Heading 3<h3 class="heading-3"></h3>
Heading 4<h4 class="heading-4"></h4>
Heading 5<h5 class="heading-5"></h5>
Heading 6<h6 class="heading-6"></h6>
Normal Text<p class="normal-text"></p>

Text styles

Text styles are converted to an HTML element: <i>, <strong>, <s>, <sub>, <sup>, and <u>.

If there is no direct mapping, a <span> with CSS is used to support features such as text color and font. This can be disabled with { styles: false }.

Header IDs are in the form id="h.wn8l66qm9m7y" when exported from the Google Docs API. By default, header tag IDs are updated to match their text content. See github-slugger for more information on how this is done.

For example, the following html:

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

becomes:

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

This can be disabled with { prettyHeaderIds: false}.

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

Unsupported Features

Some features of Google Docs are not currently supported by this library. This list is not exhaustive.

TypeSupportedBug
Styles applied to embedded objects including borders, rotations, transparency❌
documentStyle including pageSize, margins, etc❌
namedStyles ( only added as class name on the appropriate tag )❌
Page numbers❌
Page breaks❌
Equations❌
Columns❌
Suggestions❌
Bookmarks❌

Note: This library does not intend to match the rendering by Google Docs.


This is not an official Google product.

版本与发布记录

@googleworkspace/google-docs-hast 共发布 7 个版本,首次发布于 2022/10/26;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.5latest2024/03/292 年前
1.0.42024/03/152 年前
1.0.32023/03/143 年前
1.0.22023/03/083 年前
1.0.12022/11/243 年前
1.0.02022/11/243 年前

关于 @googleworkspace/google-docs-hast 的常见问题

@googleworkspace/google-docs-hast 是什么?

Convert a Google Doc JSON representation to an HTML abstract syntax tree.

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