-
Notifications
You must be signed in to change notification settings - Fork 0
/
formatter.html
116 lines (98 loc) · 3.25 KB
/
formatter.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Formatter</title>
<link rel="stylesheet" type="text/css" href="main.css">
<script src="https://unpkg.com/[email protected]/standalone.js"></script>
<script src="https://unpkg.com/[email protected]/plugins/babel.js"></script>
<script src="https://unpkg.com/[email protected]/plugins/estree.js"></script>
<script src="https://unpkg.com/[email protected]/plugins/postcss.js"></script>
<script src="https://unpkg.com/[email protected]/plugins/html.js"></script>
<script src="https://unpkg.com/[email protected]"></script>
<script src="https://unpkg.com/[email protected]/dist/csso.js"></script>
<script src="https://unpkg.com/[email protected]/dist/htmlminifier.umd.bundle.min.js"></script>
<style>
.split {
margin-top: 0.5em;
}
textarea {
font-family: monospace;
}
select {
width: fit-content;
flex-shrink: 1;
}
</style>
</head>
<body>
<header>
<h1><a href="index.html">Tools</a></h1>
</header>
<main>
<header class="bar">
<h2>Formatter</h2>
<select>
<option value="babel" selected>Javascript</option>
<option value="html">HTML</option>
<option value="css">CSS</option>
</select>
</header>
<textarea placeholder="Enter code..." rows="10" cols="50"></textarea>
<div class="split">
<button onclick="minify()">Minify</button>
<button onclick="format()">Format</button>
</div>
<noscript>
<small>This tool requires Javascript to work.</small>
</noscript>
</main>
<script>
const textarea = document.querySelector("textarea");
const language = document.querySelector("select");
async function format() {
const code = textarea.value;
const parser = language.value;
try {
const formatted = await prettier.format(code, {
parser: parser,
plugins: prettierPlugins
});
textarea.value = formatted;
} catch(e) {
alert(`Error: ${e.message}`);
}
}
async function minify() {
const code = textarea.value;
const parser = language.value;
let minified;
try {
switch (parser) {
case "babel":
minified = await Terser.minify(code);
textarea.value = minified.code;
break;
case "html":
textarea.value = await HTMLMinifier.minify(code, {
collapseWhitespace: true,
removeComments: true,
minifyCSS: true,
minifyJS: true,
});
break;
case "css":
minified = csso.minify(code);
textarea.value = minified.css;
break;
}
} catch(e) {
alert(`Error: ${e.message}`);
}
}
</script>
<script src="//instant.page/5.2.0" type="module" integrity="sha384-jnZyxPjiipYXnSU0ygqeac2q7CVYMbh84q0uHVRRxEtvFPiQYbXWUorga2aqZJ0z"></script>
</body>
</html>