@ciolabs/html-preserve-comment-whitespace

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

Preserves the presence or lack thereof of whitespace surrounding HTML comments

原始使用文档
文档目录8 节
  1. Install
  2. Usage
  3. API
  4. preserve(html)
  5. restore(html, comments, options)
  6. Types
  7. CommentData
  8. RestoreOptions

@ciolabs/preserve-comment-whitespace

Preserves the presence or lack thereof of whitespace surrounding HTML comments.

HTML formatters don't always properly handle HTML comments. This package attempts to properly maintain the whitespace around the HTML comments.

Some known issues:

Install

npm install @ciolabs/preserve-comment-whitespace

Usage

import { preserve, restore } from '@ciolabs/preserve-comment-whitespace';
import beautify from 'js-beautify';

const html = `<div><div><!-- my html comment --></div></div>`;

const comments = preserve(html);
const formatted = beautify.html(html);
//=> <div>\n    <div>\n        <!-- my html comment -->\n    </div>\n</div>
const formattedAndRestored = restore(formatted, comments);
//=> <div>\n    <div><!-- my html comment --></div>\n</div>

API

preserve(html)

Returns an Array containing the objects describing the HTML comments.

restore(html, comments, options)

Returns a string where the whitespace around the HTML comments is restored.

Note: the processing between preserve and restore should not add or remove any comments. If the number of comments given don't match the number of comments found in the given HTML, restore will return the given string, unprocessed.

html

string | required

String of HTML after any formatting that would have affected the whitespace.

comments

CommentData[] | defaults to []

The Array returned from preserve.

options

RestoreOptions | defaults to { restoreInline: true }

Configuration for how to restore the comment whitespace.

Accept restoreInline. If true, comments that were originally inline (i.e. not on their own lines) will be restored to be inline. Otherwise, it will accept the new line placement.

Types

CommentData

interface CommentData {
  leadingWhitespace: string;
  trailingWhitespace: string;
  hasLeadingWhitespace: boolean;
  hasTrailingWhitespace: boolean;
}

RestoreOptions

interface RestoreOptions {
  restoreInline: boolean;
}

版本与发布记录

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

版本发布日期距今
0.0.3latest2026/02/208 个月前
0.0.22026/02/208 个月前
0.0.12025/09/191.1 年前
0.0.02025/09/191.1 年前

关于 @ciolabs/html-preserve-comment-whitespace 的常见问题

@ciolabs/html-preserve-comment-whitespace 是什么?

Preserves the presence or lack thereof of whitespace surrounding HTML comments

如何安装 @ciolabs/html-preserve-comment-whitespace?

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

@ciolabs/html-preserve-comment-whitespace 支持 TypeScript 吗?

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

@ciolabs/html-preserve-comment-whitespace 支持 ESM 还是 CommonJS?

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

@ciolabs/html-preserve-comment-whitespace 可以商用吗?

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

@ciolabs/html-preserve-comment-whitespace 的下载量有多少?

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

@ciolabs/html-preserve-comment-whitespace 还在维护吗?

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