1 | ## HTML cleaner and beautifier
|
2 |
|
3 | Do you have crappy HTML? I do!
|
4 |
|
5 | ```html
|
6 | <table width="100%" border="0" cellspacing="0" cellpadding="0">
|
7 | <tr>
|
8 | <td height="31"><b>Currently we have these articles available:</b>
|
9 |
|
10 | <!-- This is so ugly! -->
|
11 | <blockquote>
|
12 | <p><a href="foo.html">The History of Foo</a><br />
|
13 | An <span color="red">informative</span> piece of <FONT FACE="ARIAL">information</FONT>.</p>
|
14 | <p><a href="bar.html">A Horse Walked Into a Bar</a><br/> The bartender said
|
15 | "Why the long face?"</p>
|
16 | </blockquote>
|
17 | </td>
|
18 | </tr>
|
19 | </table>
|
20 | ```
|
21 |
|
22 | Can you stand to look at it? Just look at those blank lines and random line breaks, trailing spaces, mixed tabs, deprecated tags... it's outrageous!
|
23 |
|
24 | Let's clean it up...
|
25 |
|
26 | ```bash
|
27 | $ npm install clean-html
|
28 | ```
|
29 |
|
30 | ```javascript
|
31 | var cleaner = require('clean-html'),
|
32 | fs = require('fs'),
|
33 | file = process.argv[2],
|
34 | options = {'pretty': true};
|
35 |
|
36 | fs.readFile(file, function (err, data) {
|
37 | process.stdout.write(cleaner.clean(data, options) + '\n');
|
38 | });
|
39 | ```
|
40 |
|
41 | Sanity restored!
|
42 |
|
43 | ```html
|
44 | <table>
|
45 | <tr>
|
46 | <td>
|
47 | <b>Currently we have these articles available:</b>
|
48 | <blockquote>
|
49 | <p>
|
50 | <a href="foo.html">The History of Foo</a><br>
|
51 | An <span>informative</span> piece of information.
|
52 | </p>
|
53 | <p>
|
54 | <a href="bar.html">A Horse Walked Into a Bar</a><br>
|
55 | The bartender said "Why the long face?"
|
56 | </p>
|
57 | </blockquote>
|
58 | </td>
|
59 | </tr>
|
60 | </table>
|
61 | ```
|