@ciolabs/html-email-formatter

中文文档
0.0.5发布于 2026/02/20Apache-2.0 WITH Commons-ClauseTypeScriptESM + CJS构建来源已验证近 30 天下载 2万

Format and prettify HTML email content

原始使用文档
文档目录7 节
  1. Why?
  2. Install
  3. Usage
  4. With Options
  5. How It Works
  6. API
  7. emailFormatter(html, options?)

@ciolabs/html-email-formatter

Format and prettify HTML email content with conditional comment support

Email HTML often contains Microsoft Outlook conditional comments (<!--[if mso]>) that need special handling during formatting. Standard HTML formatters can break these comments or misalign their indentation. This package safely formats email HTML while preserving conditional comment structure and whitespace.

Why?

When building HTML emails, you often need to include conditional comments for Microsoft Outlook compatibility:

<div>
  <!--[if mso]>
<table><tr><td>
<![endif]-->
  <p>Email content</p>
  <!--[if mso]>
</td></tr></table>
<![endif]-->
</div>

Standard HTML formatters either:

  • Break the conditional comment syntax
  • Misalign the opening and closing tags
  • Strip important whitespace around comments

This formatter handles these edge cases correctly.

Install

npm install @ciolabs/html-email-formatter

Usage

import emailFormatter from '@ciolabs/html-email-formatter';

const html = `
<div>
<!--[if mso]>
<table><tr><td>
<![endif]-->
<p>Email content</p>
<!--[if mso]>
</td></tr></table>
<![endif]-->
</div>
`;

const formatted = emailFormatter(html);
console.log(formatted);

Output:

<div>
  <!--[if mso]>
  <table>
    <tr>
      <td>
  <![endif]-->
  <p>Email content</p>
  <!--[if mso]>
      </td>
    </tr>
  </table>
  <![endif]-->
</div>

With Options

The formatter accepts options from both pretty and js-beautify:

const formatted = emailFormatter(html, {
  indent_size: 4,
  wrap_line_length: 80,
  preserve_newlines: false,
});

How It Works

  1. Temporarily closes conditional comments - Converts <!--[if mso]>content<![endif]--> to <!--[if mso]>-->content<!--<![endif]--> so formatters can process the inner HTML
  2. Preserves comment whitespace - Uses @ciolabs/html-preserve-comment-whitespace to maintain spacing around comments
  3. Formats the HTML - Runs the modified HTML through standard formatters
  4. Restores conditional comments - Converts back to original conditional comment syntax
  5. Aligns indentation - Ensures opening and closing tags have consistent indentation

API

emailFormatter(html, options?)

Formats HTML email content while preserving conditional comments.

Parameters:

Returns:

  • (string) - The formatted HTML

Common Options:

  • indent_size (number) - Number of spaces for indentation (default: 2)
  • wrap_line_length (number) - Line length before wrapping (default: 80)
  • preserve_newlines (boolean) - Whether to preserve existing newlines (default: true)
  • max_preserve_newlines (number) - Maximum consecutive newlines to preserve (default: 2)

版本与发布记录

@ciolabs/html-email-formatter 共发布 6 个版本,首次发布于 2025/09/19;过去 12 个月发布了 3 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.0.5latest2026/02/208 个月前
0.0.42026/02/208 个月前
0.0.32025/11/1811 个月前
0.0.22025/10/011.1 年前
0.0.12025/09/191.1 年前
0.0.02025/09/191.1 年前

关于 @ciolabs/html-email-formatter 的常见问题

@ciolabs/html-email-formatter 是什么?

Format and prettify HTML email content

如何安装 @ciolabs/html-email-formatter?

在项目目录运行 npm install @ciolabs/html-email-formatter,或使用 pnpm add / yarn add / bun add。

@ciolabs/html-email-formatter 支持 TypeScript 吗?

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

@ciolabs/html-email-formatter 支持 ESM 还是 CommonJS?

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

@ciolabs/html-email-formatter 可以商用吗?

@ciolabs/html-email-formatter 使用 Apache-2.0 WITH Commons-Clause 许可。声明的许可为 Apache-2.0 WITH Commons-Clause,请阅读许可原文确认使用条件。以上为概括,不构成法律意见,请以许可原文为准。

@ciolabs/html-email-formatter 的下载量有多少?

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

@ciolabs/html-email-formatter 还在维护吗?

最新版本 0.0.5 发布于 2026/02/20(8 个月前);至今共发布 6 个版本,其中过去 12 个月发布了 3 个;首次发布于 2025/09/19。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。