UNPKG

7.59 kBMarkdownView Raw
1# Emmet — the essential toolkit for web-developers
2
3Emmet is a web-developer’s toolkit for boosting HTML & CSS code writing.
4
5With Emmet, you can type expressions (_abbreviations_) similar to CSS selectors and convert them into code fragment with a single keystroke. For example, this abbreviation:
6
7```
8ul#nav>li.item$*4>a{Item $}
9```
10
11...can be expanded into:
12
13```html
14<ul id="nav">
15 <li class="item1"><a href="">Item 1</a></li>
16 <li class="item2"><a href="">Item 2</a></li>
17 <li class="item3"><a href="">Item 3</a></li>
18 <li class="item4"><a href="">Item 4</a></li>
19</ul>
20```
21
22## Features
23
24* **Familiar syntax**: as a web-developer, you already know how to use Emmet. Abbreviation syntax is similar to CSS Selectors with shortcuts for id, class, custom attributes, element nesting and so on.
25* **Dynamic snippets**: unlike default editor snippets, Emmet abbreviations are dynamic and parsed as-you-type. No need to predefine them for each project, just type `MyComponent>custom-element` to convert any word into a tag.
26* **CSS properties shortcuts**: Emmet provides special syntax for CSS properties with embedded values. For example, `bd1-s#f.5` will be expanded to `border: 1px solid rgba(255, 255, 255, 0.5)`.
27* **Available for most popular syntaxes**: use single abbreviation to produce code for most popular syntaxes like HAML, Pug, JSX, SCSS, SASS etc.
28
29[Read more about Emmet features](https://docs.emmet.io)
30
31This repo contains only core module for parsing and expanding Emmet abbreviations. Editor plugins are available as [separate repos](https://github.com/emmetio).
32
33This is a *monorepo*: top-level project contains all the code required for converting abbreviation into code fragment while [`./packages`](/packages) folder contains modules for parsing abbreviations into AST and can be used independently (for example, as lexer for syntax highlighting).
34
35### Installation
36
37You can install Emmet as a regular npm module:
38
39```bash
40npm i emmet
41```
42
43## Usage
44
45To expand abbreviation, pass it to default function of `emmet` module:
46
47```js
48import expand from 'emmet';
49
50console.log(expand('p>a')); // <p><a href=""></a></p>
51```
52
53By default, Emmet expands *markup* abbreviation, e.g. abbreviation used for producing nested elements with attributes (like HTML, XML, HAML etc.). If you want to expand *stylesheet* abbreviation, you should pass it as a `type` property of second argument:
54
55```js
56import expand from 'emmet';
57
58console.log(expand('p10', { type: 'stylesheet' })); // padding: 10px;
59```
60
61A stylesheet abbreviation has slightly different syntax compared to markup one: it doesn’t support nesting and attributes but allows embedded values in element name.
62
63Alternatively, Emmet supports *syntaxes* with predefined snippets and options:
64
65```js
66import expand from 'emmet';
67
68console.log(expand('p10', { syntax: 'css' })); // padding: 10px;
69console.log(expand('p10', { syntax: 'stylus' })); // padding 10px
70```
71
72Predefined syntaxes already have `type` attribute which describes whether given abbreviation is markup or stylesheet, but if you want to use it with your custom syntax name, you should provide `type` config option as well (default is `markup`):
73
74```js
75import expand from 'emmet';
76
77console.log(expand('p10', {
78 syntax: 'my-custom-syntax',
79 type: 'stylesheet',
80 options: {
81 'stylesheet.between': '__',
82 'stylesheet.after': '',
83 }
84})); // padding__10px
85```
86
87You can pass `options` property as well to shape-up final output or enable/disable various features. See [`src/config.ts`](src/config.ts) for more info and available options.
88
89## Extracting abbreviations from text
90
91A common workflow with Emmet is to type abbreviation somewhere in source code and then expand it with editor action. To support such workflow, abbreviations must be properly _extracted_ from source code:
92
93```js
94import expand, { extract } from 'emmet';
95
96const source = 'Hello world ul.tabs>li';
97const data = extract(source, 22); // { abbreviation: 'ul.tabs>li' }
98
99console.log(expand(data.abbreviation)); // <ul class="tabs"><li></li></ul>
100```
101
102The `extract` function accepts source code (most likely, current line) and character location in source from which abbreviation search should be started. The abbreviation is searched in backward direction: the location pointer is moved backward until it finds abbreviation bound. Returned result is an object with `abbreviation` property and `start` and `end` properties which describe location of extracted abbreviation in given source.
103
104Most current editors automatically insert closing quote or bracket for `(`, `[` and `{` characters so when user types abbreviation that uses attributes or text, it will end with the following state (`|` is caret location):
105
106```
107ul>li[title="Foo|"]
108```
109
110E.g. caret location is not at the end of abbreviation and must be moved a few characters ahead. The `extract` function is able to handle such cases with `lookAhead` option (enabled by default). This this option enabled, `extract` method automatically detects auto-inserted characters and adjusts location, which will be available as `end` property of the returned result:
111
112```js
113import { extract } from 'emmet';
114
115const source = 'a div[title] b';
116const loc = 11; // right after "title" word
117
118// `lookAhead` is enabled by default
119console.log(extract(source, loc)); // { abbreviation: 'div[title]', start: 2, end: 12 }
120console.log(extract(source, loc, { lookAhead: false })); // { abbreviation: 'title', start: 6, end: 11 }
121```
122
123By default, `extract` tries to detect _markup_ abbreviations (see above). _stylesheet_ abbreviations has slightly different syntax so in order to extract abbreviations for stylesheet syntaxes like CSS, you should pass `type: 'stylesheet'` option:
124
125```js
126import { extract } from 'emmet';
127
128const source = 'a{b}';
129const loc = 3; // right after "b"
130
131console.log(extract(source, loc)); // { abbreviation: 'a{b}', start: 0, end: 4 }
132
133
134// Stylesheet abbreviations does not have `{text}` syntax
135console.log(extract(source, loc, { type: 'stylesheet' })); // { abbreviation: 'b', start: 2, end: 3 }
136```
137
138### Extract abbreviation with custom prefix
139
140Lots of developers uses React (or similar) library for writing UI code which mixes JS and XML (JSX) in the same source code. Since _any_ Latin word can be used as Emmet abbreviation, writing JSX code with Emmet becomes pain since it will interfere with native editor snippets and distract user with false positive abbreviation matches for variable names, methods etc.:
141
142```js
143var div // `div` is a valid abbreviation, Emmet may transform it to `<div></div>`
144```
145
146A possible solution for this problem it to use _prefix_ for abbreviation: abbreviation can be successfully extracted only if its preceded with given prefix.
147
148```js
149import { extract } from 'emmet';
150
151const source1 = '() => div';
152const source2 = '() => <div';
153
154extract(source1, source1.length); // Finds `div` abbreviation
155extract(source2, source2.length); // Finds `div` abbreviation too
156
157extract(source1, source1.length, { prefix: '<' }); // No match, `div` abbreviation is not preceded with `<` prefix
158extract(source2, source2.length, { prefix: '<' }); // Finds `div` since it preceded with `<` prefix
159```
160
161With `prefix` option, you can customize your experience with Emmet in any common syntax (HTML, CSS and so on) if user is distracted too much with Emmet completions for any typed word. A `prefix` may contain multiple character but the last one *must* be a character which is not part of Emmet abbreviation. Good candidates are `<`, `&`, `→` (emoji or Unicode symbol) and so on.