• Stars
    star
    4,431
  • Rank 9,666 (Top 0.2 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 14 years ago
  • Updated 8 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

The essential toolkit for web-developers

Emmet — the essential toolkit for web-developers

Emmet is a web-developer’s toolkit for boosting HTML & CSS code writing.

With Emmet, you can type expressions (abbreviations) similar to CSS selectors and convert them into code fragment with a single keystroke. For example, this abbreviation:

ul#nav>li.item$*4>a{Item $}

...can be expanded into:

<ul id="nav">
    <li class="item1"><a href="">Item 1</a></li>
    <li class="item2"><a href="">Item 2</a></li>
    <li class="item3"><a href="">Item 3</a></li>
    <li class="item4"><a href="">Item 4</a></li>
</ul>

Features

  • 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.
  • 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.
  • 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).
  • Available for most popular syntaxes: use single abbreviation to produce code for most popular syntaxes like HAML, Pug, JSX, SCSS, SASS etc.

Read more about Emmet features

This repo contains only core module for parsing and expanding Emmet abbreviations. Editor plugins are available as separate repos.

This is a monorepo: top-level project contains all the code required for converting abbreviation into code fragment while ./packages folder contains modules for parsing abbreviations into AST and can be used independently (for example, as lexer for syntax highlighting).

Installation

You can install Emmet as a regular npm module:

npm i emmet

Usage

To expand abbreviation, pass it to default function of emmet module:

import expand from 'emmet';

console.log(expand('p>a')); // <p><a href=""></a></p>

By 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:

import expand from 'emmet';

console.log(expand('p10', { type: 'stylesheet' })); // padding: 10px;

A stylesheet abbreviation has slightly different syntax compared to markup one: it doesn’t support nesting and attributes but allows embedded values in element name.

Alternatively, Emmet supports syntaxes with predefined snippets and options:

import expand from 'emmet';

console.log(expand('p10', { syntax: 'css' })); // padding: 10px;
console.log(expand('p10', { syntax: 'stylus' })); // padding 10px

Predefined 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):

import expand from 'emmet';

console.log(expand('p10', {
    syntax: 'my-custom-syntax',
    type: 'stylesheet',
    options: {
        'stylesheet.between': '__',
        'stylesheet.after': '',
    }
})); // padding__10px

You can pass options property as well to shape-up final output or enable/disable various features. See src/config.ts for more info and available options.

Extracting abbreviations from text

A 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:

import expand, { extract } from 'emmet';

const source = 'Hello world ul.tabs>li';
const data = extract(source, 22); // { abbreviation: 'ul.tabs>li' }

console.log(expand(data.abbreviation)); // <ul class="tabs"><li></li></ul>

The 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.

Most 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):

ul>li[title="Foo|"]

E.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:

import { extract } from 'emmet';

const source = 'a div[title] b';
const loc = 11; // right after "title" word

// `lookAhead` is enabled by default
console.log(extract(source, loc)); // { abbreviation: 'div[title]', start: 2, end: 12 }
console.log(extract(source, loc, { lookAhead: false })); // { abbreviation: 'title', start: 6, end: 11 }

By 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:

import { extract } from 'emmet';

const source = 'a{b}';
const loc = 3; // right after "b"

console.log(extract(source, loc)); // { abbreviation: 'a{b}', start: 0, end: 4 }


// Stylesheet abbreviations does not have `{text}` syntax
console.log(extract(source, loc, { type: 'stylesheet' })); // { abbreviation: 'b', start: 2, end: 3 }

Extract abbreviation with custom prefix

Lots 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.:

var div // `div` is a valid abbreviation, Emmet may transform it to `<div></div>`

A possible solution for this problem it to use prefix for abbreviation: abbreviation can be successfully extracted only if its preceded with given prefix.

import { extract } from 'emmet';

const source1 = '() => div';
const source2 = '() => <div';

extract(source1, source1.length); // Finds `div` abbreviation
extract(source2, source2.length); // Finds `div` abbreviation too

extract(source1, source1.length, { prefix: '<' }); // No match, `div` abbreviation is not preceded with `<` prefix
extract(source2, source2.length, { prefix: '<' }); // Finds `div` since it preceded with `<` prefix

With 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.

More Repositories

1

emmet-atom

Emmet support for Atom
CoffeeScript
767
star
2

pyv8-binaries

Archive of pre-compiled PyV8 binaries
710
star
3

brackets-emmet

Emmet plugin for Brackets editor
JavaScript
485
star
4

emmet-eclipse

Emmet for Eclipse
JavaScript
343
star
5

emmet-docs

Emmet Docs & Tutorials
JavaScript
270
star
6

livestyle-sublime-old

Live bi-directional CSS edit of new generation
Python
260
star
7

sublime-text-plugin

The essential toolkit for web-developers
Python
253
star
8

sublime-tern

TernJS plugin for Sublime Text
JavaScript
222
star
9

Emmet.codaplugin

Emmet plugin for Coda
JavaScript
206
star
10

re-view

A Google Chrome extension for displaying responsive breakpoints view
JavaScript
204
star
11

codemirror

Emmet plugin for CodeMirror online editor
JavaScript
160
star
12

livestyle-web

Emmet LiveStyle web-site
CSS
120
star
13

textarea

Emmet plugin for <textarea>
JavaScript
119
star
14

Emmet.tmplugin

Emmet plugin for TextMate
JavaScript
97
star
15

dreamweaver

Emmet for Dreamweaver
JavaScript
85
star
16

snippets

Emmet HTML snippets
JavaScript
71
star
17

codemirror-plugin

Emmet plugin for CodeMirror web editor
TypeScript
69
star
18

Emmet.sugar

Emmet plugin for Espresso editor
JavaScript
58
star
19

atom-plugin

Experimental Atom plugin for new Emmet
JavaScript
48
star
20

emmet-objc

Objective-C to JavaScript binding of Emmet
JavaScript
48
star
21

py-emmet

Emmet abbreviation parser and expander, implemented in Python
Python
39
star
22

expand-abbreviation

Reference implementation of Emmet abbreviation expander
JavaScript
35
star
23

codemirror6-plugin

WIP Plugin for CodeMirror 6
TypeScript
34
star
24

nova-plugin

Emmet plugin for Panic’s Nova editor
TypeScript
26
star
25

web

Project web-site
JavaScript
19
star
26

pspad

Emmet plugin for PSPad
JavaScript
16
star
27

re-view-chrome

Emmet Re:view 2 extension for Google Chrome
JavaScript
15
star
28

textmate2

Emmet plugin for TextMate 2
8
star
29

math-expression

Parse & evaluate simple math expressions
TypeScript
8
star
30

abbreviation

Emmet standalone abbreviation parser (WIP)
JavaScript
8
star
31

monaco-plugin

Emmet for Monaco editor
TypeScript
5
star
32

html-transform

Prepares given Emmet abbreviation for HTML output
JavaScript
4
star
33

extract-abbreviation

Extracts Emmet abbreviation from string
JavaScript
4
star
34

html-matcher

Matches HTML/XML tag pairs in source code
TypeScript
4
star
35

markup-formatters

Outputs parsed Emmet abbreviation as markup (HTML, HAML, Slim, Pug etc.)
JavaScript
4
star
36

css-parser

CSS/LESS/SCSS fast and minimalistic parser
JavaScript
4
star
37

Emmetoss

Extended snippets for Emmet toolkit
3
star
38

lorem

Transforms parsed Emmet abbreviation node into Lorem Ipsum stub text
JavaScript
2
star
39

xml-diff

Diff and patch contents of XML documents
TypeScript
2
star
40

string-stream

String stream reader, used in many Emmet projects to parse strings
JavaScript
2
star
41

snippets-registry

JavaScript module for storing and retrieving Emmet snippets
JavaScript
2
star
42

stream-reader

Reads text as stream
JavaScript
2
star
43

action-utils

Utilities for Emmet actions
TypeScript
1
star
44

css-snippets-resolver

Resolves CSS snippets for parsed Emmet abbreviation
JavaScript
1
star
45

re-view-web

Emmet Re:view web-site
CSS
1
star
46

css-abbreviation

Emmet CSS abbreviation parser
JavaScript
1
star
47

css-matcher

Fast and small CSS/LESS/SCSS matcher
TypeScript
1
star
48

stylesheet-analyzer

Implementation of LESS and SCSS preprocessors, used for in-editor code analysis
JavaScript
1
star
49

lodash

A private copy of Lo-Dash lib, without template() method
JavaScript
1
star
50

field-parser

Fields (tab-stops) parser
JavaScript
1
star
51

stream-reader-utils

Common utility methods for stream reader
JavaScript
1
star
52

implicit-tag

Resolves best matching child tag name for given parent name
JavaScript
1
star