Skip to content

Commit c97a974

Browse files
committed
feat: format html & js code on page load
1 parent 94455e8 commit c97a974

File tree

3 files changed

+133
-25
lines changed

3 files changed

+133
-25
lines changed

package-lock.json

Lines changed: 110 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"dependencies": {
1313
"codemirror": "5.53.2",
1414
"dom-accessibility-api": "^0.4.3",
15+
"js-beautify": "^1.11.0",
1516
"lodash.debounce": "4.0.8",
1617
"lz-string": "^1.4.4",
1718
"pretty-format": "26.0.1",

src/lib/state.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import {
33
decompressFromEncodedURIComponent,
44
} from 'lz-string';
55

6+
import beautify from 'js-beautify';
7+
68
function unindent(string) {
79
return string.replace(/[ \t]*[\n][ \t]*/g, '\n');
810
}
@@ -16,17 +18,35 @@ function save({ html, js }) {
1618
history.replaceState(null, '', window.location.pathname + '#' + state);
1719
}
1820

21+
const beautifyOptions = {
22+
indent_size: 2,
23+
wrap_line_length: 72,
24+
wrap_attributes: 'force-expand-multiline',
25+
};
26+
1927
function load() {
2028
const [htmlCompressed, jsCompressed] = window.location.hash
2129
.slice(1)
2230
.split('&');
2331

2432
return {
25-
html: htmlCompressed && decompressFromEncodedURIComponent(htmlCompressed),
26-
js: jsCompressed && decompressFromEncodedURIComponent(jsCompressed),
33+
html:
34+
htmlCompressed &&
35+
beautify.html(
36+
decompressFromEncodedURIComponent(htmlCompressed),
37+
beautifyOptions,
38+
),
39+
js:
40+
jsCompressed &&
41+
beautify.js(
42+
decompressFromEncodedURIComponent(jsCompressed),
43+
beautifyOptions,
44+
),
2745
};
2846
}
2947

48+
window.beautify = beautify;
49+
3050
function updateTitle(text) {
3151
const title = document.title.split(':')[0];
3252

0 commit comments

Comments
 (0)