-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.html
216 lines (177 loc) · 5.53 KB
/
index.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>WPHTML Converter</title>
<meta name="description" content="Convert WordPress block HTML to its JavaScript object or PHP array forms.">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" href="https://happyprime.co/wp-content/uploads/2019/10/HappyPrime-mark-300x300.png"
sizes="192x192" />
<link rel="apple-touch-icon" href="https://happyprime.co/wp-content/uploads/2019/10/HappyPrime-mark-300x300.png" />
<style>
body {
font-family: sans-serif;
color: #333;
}
div {
max-width: 720px;
margin: 5rem auto 0;
}
div ~ div {
max-width: 960px;
margin: 2rem auto 0;
}
textarea {
width: 100%;
height: 33vh;
}
button {
margin-top: 1rem;
height: 2rem;
}
pre {
margin: 2rem 0;
background: #efefef;
padding: 1.5rem;
overflow: auto;
border: 1px solid #ddd;
}
pre a {
color: rgb(0, 0, 255);
display: block;
width: 100%;
text-align: right;
}
</style>
</head>
<body>
<div>
<h1>WPHTML Converter</h1>
<p>Convert WordPress block HTML to its JavaScript object or PHP array forms.</p>
<textarea></textarea>
<button id="convert-to-js" type="submit">Convert to JS</button>
<button id="convert-to-php" type="submit">Convert to PHP</button>
</div>
<div>
<pre><a id="copy-code" href="">Copy code</a><code id="generated-code"></code></pre>
<p>Built by <a href="https://happyprime.co/">Happy Prime</a>, <a
href="https://github.com/happyprime/wphtml-converter/">contributors welcome</a>.</p>
</div>
<script type="module">
import { parse } from './parser.js?ver=af7ff631d32';
{
/**
* Auto-indent a string of JavaScript code to fit our expected
* copy/paste patterns.
*
* Props to everyone's code who powered ChatGPT because this took
* like 3 minutes to prompt. 🙃
*
* @param {string} code
* @returns {string}
*/
const format = (code) => {
const indentString = '\t';
let indentLevel = 0;
let output = '';
// Patterns for matching opening brackets, closing brackets, and commas.
const openingBrackets = /(\[|\{)/g;
const closingBrackets = /(\]|\})/g;
const commas = /,/g;
// Split the code string into individual characters.
const characters = code.split('');
for (let i = 0; i < characters.length; i++) {
const char = characters[i];
if (char.match(openingBrackets)) {
// Increase the indent level after an opening bracket.
indentLevel++;
output += char + '\n' + indentString.repeat(indentLevel);
} else if (char.match(closingBrackets)) {
// Decrease the indent level after a closing bracket.
indentLevel--;
output += '\n' + indentString.repeat(indentLevel) + char;
} else if (char.match(commas)) {
// Add a new line after commas.
output += char + '\n' + indentString.repeat(indentLevel);
} else {
output += char;
}
}
// Remove extra whitespace from empty brackets
output = output.replace(/\[\s*\]/g, '[]');
output = output.replace(/\{\s*\}/g, '{}');
return output;
}
/**
* Parse a parsed block into a string representation of a
* JavaScript object.
*
* @param {object} block
* @returns {string}
*/
const parseBlock = (block) => {
let data = `['${block.blockName}',${JSON.stringify(block.attrs, null, "")},[`;
block.innerBlocks?.forEach((innerBlock) => {
data += parseBlock(innerBlock);
});
data += `]],`;
return data;
};
/**
* Convert WPHTML from the page's textarea into a string representation of
* the JavaScript object.
*
* @returns {string}
*/
const getJSCode = () => {
// Remove any newlines or tabs to avoid null blocks.
let wphtml = document.querySelector('textarea').value.replace(/\n|\t/g, '');
// Parse the HTML into a list of blocks.
const blocks = parse(wphtml);
let data = '';
blocks.forEach(block => { data += parseBlock(block) });
return format(data);
}
/**
* Handle a request to convert WPHTML to a string representation of
* a JavaScript object.
*
* @param {Event} evt
*/
const handleConvertJS = (evt) => {
evt.preventDefault();
document.querySelector('code').innerText = getJSCode();
document.querySelector('#copy-code').innerText = 'Copy JS code';
}
/**
* Handle a request to convert WPHTML to a string representation of
* a PHP array.
*
* @param {Event} evt
*/
const handleConvertPHP = (evt) => {
evt.preventDefault();
let data = getJSCode();
// Replace characters { and [ with their PHP counterparts.
data = data.replace(/{/g, 'array(');
data = data.replace(/\[/g, 'array(');
// Replace characters } and ] with their PHP counterparts.
data = data.replace(/}/g, ')');
data = data.replace(/]/g, ')');
// Replace characters " with '
data = data.replace(/"/g, "'");
// Replace characters : with => if within ''
data = data.replace(/'([^']*)':/g, "'$1' => ");
document.querySelector('code').innerText = data;
document.querySelector('#copy-code').innerText = 'Copy PHP code';
}
document.querySelector('#convert-to-js').addEventListener('click', handleConvertJS);
document.querySelector('#convert-to-php').addEventListener('click', handleConvertPHP);
document.querySelector('#copy-code').addEventListener('click', (evt) => {
evt.preventDefault();
navigator.clipboard.writeText(document.querySelector('#generated-code').innerText);
})
}
</script>
</body>
</html>