@emmetio/abbreviation

中文文档
2.3.3发布于 2023/05/10MITTypeScriptESM + CJS近 30 天下载 2203.5万

Emmet standalone abbreviation parser

原始使用文档
文档目录4 节
  1. Usage
  2. Abbreviation syntax
  3. Operators
  4. Groups

Emmet markup abbreviation parser

Parses given Emmet markup abbreviation into AST. Parsing is performed in two steps: first it tokenizes given abbreviation (useful for syntax highlighting in editors) and then tokens are analyzed and converted into AST nodes as plain, JSON-serializable objects.

Note that AST tree in most cases cannot be used directly for output: for example, AST node produced from .foo.bar element misses element name and contains two class attributes with foo and bar values (not a single class with foo bar value).

Usage

You can install it via npm:

npm install @emmetio/abbreviation

Then add it into your project:

import parse from '@emmetio/abbreviation';

const tree = parse('div#foo>span.bar*3');
/* {
    type: 'Abbreviation',
    children: [{
        type: 'AbbreviationNode',
        name: 'div',
        attributes: [...],
        children: [...]
    }]
} */

The returned tree contains AbbreviationNode items: a node with name, attributes and/or text content. E.g. an element that can be represented somehow. Repeated and grouped nodes like a>(b+c)*3 are automatically converted and duplicated as distinct AbbreviationNode with distinct .repeat property which identifies node in repeating sequence.

Abbreviation syntax

Emmet abbreviation element has the following basic parts:

name.class#id[attributes?, ...]{text value}*repeater/
  • name — element name, like div, span etc. Stored as node.name property.
  • [attributes] — list of attributes. Each attribute is stored as AbbreviationAttribute instance and can be accessed by node.getAttribute(name). Each attribute can be written in different formats:
    • attr — attribute with empty value.
    • attr=value — attribute with value. The value may contain any character except space or ].
    • attr="value" or attr='value' — attribute with value in quotes. Quotes are automatically removed. Expression values like attr={value} are supported and can be identified by valueType: "expression" property.
    • attr. — boolean attribute, e.g. attribute without value, like required in <input>.
    • !attr – implicit attribute, will be outputted if its value is not empty. Used as a placeholder to preserve attribute order in output.
    • ./non/attr/value — value for default attribute. In other words, anything that doesn’t match a attribute name characters. Can be a single- or double-quotted as well. Default attribute is stored with null as name and should be used later, for example, to resolve predefined attributes.
  • .class — shorthand for class attribute. Note that an element can have multiple classes, like .class1.class2.class3.
  • #id — shorthand for id attribute.
  • {text} — node’s text content
  • *N — element repeater, tells parser to create N copies of given node.
  • / — optional self-closing operator. Marks element with node.selfClosing = true.

Operators

Each element of abbreviation must be separated with any of these operators:

elem1+elem2>elem3
  • + — sibling operator, adds next element as a next sibling of current element in tree.
  • > — child operator, adds next element as a child of current element.
  • ^ — climb-up operator, adds next element as a child of current element’s parent node. Multiple climb-up operators are allowed, each operator moves one level up by tree.

Groups

A set of elements could be grouped using (), mostly for repeating and for easier elements nesting:

a>(b>c+d)*4+(e+f)

Groups can be optionally concatenated with + operator.

版本与发布记录

@emmetio/abbreviation 共发布 45 个版本,首次发布于 2016/11/12;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.3.3latest2023/05/103 年前
2.3.22023/05/023 年前
2.3.12023/03/313 年前
2.3.02023/03/313 年前
2.2.32022/02/114 年前
2.2.22021/03/225 年前

关于 @emmetio/abbreviation 的常见问题

@emmetio/abbreviation 是什么?

Emmet standalone abbreviation parser

如何安装 @emmetio/abbreviation?

在项目目录运行 npm install @emmetio/abbreviation,或使用 pnpm add / yarn add / bun add。

@emmetio/abbreviation 支持 TypeScript 吗?

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

@emmetio/abbreviation 支持 ESM 还是 CommonJS?

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

@emmetio/abbreviation 可以商用吗?

@emmetio/abbreviation 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@emmetio/abbreviation 的下载量有多少?

根据 npm 官方下载统计,@emmetio/abbreviation 在 2026/10/14 – 2026/11/12 这 30 天内被下载 22,034,569 次,日均约 734,486 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@emmetio/abbreviation 还在维护吗?

最新版本 2.3.3 发布于 2023/05/10(3 年前);至今共发布 45 个版本,其中过去 12 个月发布了 0 个;首次发布于 2016/11/12。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。