Skip to content

Commit 5458105

Browse files
Update README.md
1 parent 864cc68 commit 5458105

File tree

1 file changed

+74
-72
lines changed

1 file changed

+74
-72
lines changed

README.md

Lines changed: 74 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,27 @@
1-
<img src="media/logo.svg" width="150" height="150">
2-
3-
eslint-plugin-html
4-
==================
5-
6-
[![NPM version](https://img.shields.io/npm/v/eslint-plugin-html.svg)](https://www.npmjs.com/package/eslint-plugin-html)
7-
[![Build Status](https://travis-ci.org/BenoitZugmeyer/eslint-plugin-html.svg?branch=master)](https://travis-ci.org/BenoitZugmeyer/eslint-plugin-html)
8-
9-
10-
This [`ESLint`](http://eslint.org) plugin allows linting and fixing inline scripts contained in HTML
11-
files.
12-
13-
Migration to v4
14-
---------------
15-
16-
`eslint-plugin-html` v4 requires at least ESLint v4.7. This is because a lot of internal changes
17-
occured in ESLint v4.7, including a [new API to support autofixing in
18-
preprocessors](https://eslint.org/docs/developer-guide/working-with-plugins#processors-in-plugins).
19-
If you are still using an older version of ESLint, please consider upgrading, or keep using
20-
`eslint-plugin-html` v3.
21-
22-
The big feature (and breaking change) in `eslint-plugin-html` v4 is the ability to chose how [scopes
23-
are shared between script tags in the same HTML file](#multiple-scripts-tags-in-a-html-file).
24-
25-
26-
Migration to v3
27-
---------------
28-
29-
If you are considering upgrading to v3, please read [this guide](MIGRATION_TO_V3.md).
30-
31-
Usage
32-
-----
1+
<div align="center">
2+
<img src="media/logo.svg" width="150" height="150">
3+
<h1>eslint-plugin-html</h1>
4+
<a href="https://www.npmjs.com/package/eslint-plugin-html"><img alt="NPM version" src="https://img.shields.io/npm/v/eslint-plugin-html.svg"></a>
5+
<a href="https://travis-ci.org/BenoitZugmeyer/eslint-plugin-html"><img alt="Build Status" src="https://travis-ci.org/BenoitZugmeyer/eslint-plugin-html.svg?branch=master"></a>
6+
<p>A <a href="http://eslint.org">ESLint</a> plugin to lint and fix inline scripts contained in HTML files.</p>
7+
</div>
8+
9+
* [Usage](#usage)
10+
* [Multiple scripts tags in a HTML file](#multiple-scripts-tags-in-a-html-file)
11+
* [XML Support](#xml-support)
12+
* [Settings](#settings)
13+
* [`html/html-extensions`](#htmlhtml-extensions)
14+
* [`html/xml-extensions`](#htmlxml-extensions)
15+
* [`html/indent`](#htmlindent)
16+
* [`html/report-bad-indent`](#htmlreport-bad-indent)
17+
* [`html/javascript-mime-types`](#htmljavascript-mime-types)
18+
* [Troubleshooting](#troubleshooting)
19+
* [No file linted when running `eslint` on a directory](#no-file-linted-when-running-eslint-on-a-directory)
20+
* [Linting templates (or PHP)](#linting-templates-or-php)
21+
* [Linting Vue files](#linting-vue-files)
22+
* [Migration from older versions](#migration-from-older-versions)
23+
24+
## Usage
3325

3426
Simply install via `npm install --save-dev eslint-plugin-html` and add the plugin to your ESLint
3527
configuration. See
@@ -45,23 +37,17 @@ Example:
4537
}
4638
```
4739

48-
Note: by default, when executing the `eslint` command on a directory, only `.js` files will be
49-
linted. You will have to specify extra extensions with the `--ext` option. Example: `eslint --ext
50-
.html,.js src` will lint both `.html` and `.js` files in the `src` directory. See [ESLint
51-
documentation](http://eslint.org/docs/user-guide/command-line-interface#ext).
52-
53-
Multiple scripts tags in a HTML file
54-
------------------------------------
40+
## Multiple scripts tags in a HTML file
5541

5642
When linting a HTML with multiple script tags, this plugin tries to emulate the browser behavior by
57-
sharing the global scope between scripts by default. This behavior doesn't apply to "module"
43+
sharing the global scope between scripts by default. This behavior doesn't apply to "module"
5844
scripts (ie: `<script type="module">` and most transpiled code), where [each script tag gets its own
5945
top-level scope](http://exploringjs.com/es6/ch_modules.html#_modules).
6046

6147
ESLint has already [an
6248
option](https://eslint.org/docs/user-guide/configuring#specifying-parser-options) to tell the parser
63-
if the script are modules. `eslint-plugin-html` will use this option as well to know if the scopes
64-
should be shared (the default) or not. To change this, just set it in your ESLint configuration:
49+
if the script are modules. `eslint-plugin-html` will use this option as well to know if the scopes
50+
should be shared (the default) or not. To change this, just set it in your ESLint configuration:
6551

6652
```
6753
{
@@ -75,42 +61,38 @@ To illustrate this behavior, consider this HTML extract:
7561

7662
```html
7763
<script>
78-
var foo = 1;
64+
var foo = 1;
7965
</script>
8066

8167
<script>
82-
alert(foo);
68+
alert(foo);
8369
</script>
8470
```
8571

8672
This is perfectly valid by default, and the ESLint rules `no-unused-vars` and `no-undef` shouldn't
87-
complain. But if those scripts are considerated as ES modules, `no-unused-vars` should report an
73+
complain. But if those scripts are considerated as ES modules, `no-unused-vars` should report an
8874
error in the first script, and `no-undef` should report an error in the second script.
8975

9076
### History
9177

9278
In `eslint-plugin-html` v1 and v2, script code were concatenated and linted in a single pass, so
93-
the scope were always shared. This caused [some issues](MIGRATION_TO_V3.md), so in v3 all scripts
94-
were linted separately, and scopes were never shared. In v4, the plugin still lint scripts
79+
the scope were always shared. This caused [some issues](MIGRATION_TO_V3.md), so in v3 all scripts
80+
were linted separately, and scopes were never shared. In v4, the plugin still lint scripts
9581
separately, but makes sure global variables are declared and used correctly in the non-module case.
9682

97-
98-
XML support
99-
-----------
83+
## XML support
10084

10185
This plugin parses HTML and XML markup slightly differently, mainly when considering `CDATA`
10286
sections:
103-
* in XML, any data inside a `CDATA` section will be considered as raw text (not XML) and the `CDATA`
87+
88+
- in XML, any data inside a `CDATA` section will be considered as raw text (not XML) and the `CDATA`
10489
delimiter will be droped ;
105-
* in HTML, there is no such thing for `<script>` tags: the `CDATA` delimiter is considered as normal
90+
- in HTML, there is no such thing for `<script>` tags: the `CDATA` delimiter is considered as normal
10691
text and thus, part of the script.
10792

93+
## Settings
10894

109-
Settings
110-
--------
111-
112-
> Note: all settings can be written either as `"html/key": value` or in a nested object `"html": {
113-
> "key": value }`
95+
> Note: all settings can be written either as `"html/key": value` or in a nested object `"html": { "key": value }`
11496
11597
### `html/html-extensions`
11698

@@ -127,7 +109,6 @@ You can set your own list of HTML extensions by using this setting. Example:
127109
}
128110
```
129111

130-
131112
### `html/xml-extensions`
132113

133114
By default, this plugin will only consider files ending with those extensions as XML: `.xhtml`,
@@ -142,7 +123,6 @@ By default, this plugin will only consider files ending with those extensions as
142123
}
143124
```
144125

145-
146126
### `html/indent`
147127

148128
By default, the code between `<script>` tags is dedented according to the first non-empty line. The
@@ -161,7 +141,6 @@ value with a `+` to be relative to the `<script>` tag indentation. Example:
161141
}
162142
```
163143

164-
165144
### `html/report-bad-indent`
166145

167146
By default, this plugin won't warn if it encounters a problematic indentation (ex: a line is under
@@ -178,7 +157,6 @@ display errors. Example:
178157
}
179158
```
180159

181-
182160
### `html/javascript-mime-types`
183161

184162
By default, the code between `<script>` tags is considered as JavaScript code only if there is no
@@ -197,31 +175,55 @@ If a MIME type starts with a `/`, it will be considered as a regular expression.
197175
}
198176
```
199177

200-
Troubleshooting
201-
---------------
178+
## Troubleshooting
179+
180+
### No file linted when running `eslint` on a directory
181+
182+
By default, when executing the `eslint` command on a directory, only `.js` files will be linted. You
183+
will have to specify extra extensions with the `--ext` option. Example: `eslint --ext .html,.js src`
184+
will lint both `.html` and `.js` files in the `src` directory. See [ESLint
185+
documentation](http://eslint.org/docs/user-guide/command-line-interface#ext).
186+
202187

203188
### Linting templates (or PHP)
204189

205-
`eslint-plugin-html` won't evaluate or remove your template markup. If you have template markup in
190+
`eslint-plugin-html` won't evaluate or remove your template markup. If you have template markup in
206191
your script tags, the resulting script may not be valid JavaScript, so `ESLint` will fail to parse
207192
it.
208193

209194
One possible hacky workaround to make sure the code is valid JavaScript is to put your template
210-
markup inside a comment. When the template is rendered, the generated JS code must start with a new
211-
line, so it will be written below the comment. PHP example:
195+
markup inside a comment. When the template is rendered, the generated JS code must start with a new
196+
line, so it will be written below the comment. PHP example:
212197

213198
```html
214199
<script>
215-
var mydata;
216-
// <?= "\n mydata = " . json_encode($var) . ";" ?>
217-
console.log(mydata);
200+
var mydata;
201+
// <?= "\n mydata = " . json_encode($var) . ";" ?>
202+
console.log(mydata);
218203
</script>
219204
```
220205

221-
222206
### Linting VUE files
223207

224208
Initially, [`eslint-plugin-vue`](https://github.com/vuejs/eslint-plugin-vue) was using
225-
`eslint-plugin-html` to lint code inside script tags. Since v3, `eslint-plugin-vue` is using its
226-
own parser, so it is *incompatible* with `eslint-plugin-html`. You should use `eslint-plugin-vue`
209+
`eslint-plugin-html` to lint code inside script tags. Since v3, `eslint-plugin-vue` is using its
210+
own parser, so it is _incompatible_ with `eslint-plugin-html`. You should use `eslint-plugin-vue`
227211
exclusively and remove `eslint-plugin-html` from your dependencies if you still have it.
212+
213+
## Migration from older versions
214+
215+
### To v4
216+
217+
`eslint-plugin-html` v4 requires at least ESLint v4.7. This is because a lot of internal changes
218+
occured in ESLint v4.7, including a [new API to support autofixing in
219+
preprocessors](https://eslint.org/docs/developer-guide/working-with-plugins#processors-in-plugins).
220+
If you are still using an older version of ESLint, please consider upgrading, or keep using
221+
`eslint-plugin-html` v3.
222+
223+
The big feature (and breaking change) in `eslint-plugin-html` v4 is the ability to chose how [scopes
224+
are shared between script tags in the same HTML file](#multiple-scripts-tags-in-a-html-file).
225+
226+
### To v3
227+
228+
If you are considering upgrading to v3, please read [this guide](MIGRATION_TO_V3.md).
229+

0 commit comments

Comments
 (0)