-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpaste-rich-text.html
More file actions
334 lines (293 loc) · 10.9 KB
/
paste-rich-text.html
File metadata and controls
334 lines (293 loc) · 10.9 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Rich Text HTML Extractor</title>
<link rel="stylesheet" href="common.css">
<style>
body {
display: flex;
flex-direction: column;
height: 100vh;
overflow: hidden;
}
.container {
flex: 1;
min-height: 0;
}
.pane {
display: flex;
flex-direction: column;
min-height: 0;
}
#pasteArea {
flex: 1;
border: none;
padding: 16px;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
font-size: 0.9375rem;
line-height: 1.7;
outline: none;
background: var(--card-bg);
color: var(--text-primary);
overflow: auto;
min-height: 0;
}
.paste-status {
display: none;
align-items: center;
gap: 6px;
font-size: 0.75rem;
color: var(--accent-mint);
font-weight: 500;
}
.paste-status.visible {
display: flex;
}
.paste-status svg {
width: 14px;
height: 14px;
}
#htmlOutput {
flex: 1;
min-height: 0;
}
#previewContent {
flex: 1;
padding: 16px;
overflow: auto;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
font-size: 0.9375rem;
line-height: 1.7;
background: var(--card-bg);
color: var(--text-primary);
min-height: 0;
}
#previewContent:empty::before {
content: "HTML preview will appear here...";
color: var(--text-muted);
}
.toggle-switch {
position: relative;
display: inline-flex;
align-items: center;
gap: 8px;
cursor: pointer;
user-select: none;
}
.toggle-switch input {
position: absolute;
opacity: 0;
width: 0;
height: 0;
}
.toggle-slider {
position: relative;
width: 44px;
height: 24px;
background: var(--input-border);
border-radius: 12px;
transition: background-color 0.2s;
}
.toggle-slider::before {
content: '';
position: absolute;
top: 2px;
left: 2px;
width: 20px;
height: 20px;
background: white;
border-radius: 50%;
transition: transform 0.2s;
box-shadow: var(--shadow-sm);
}
.toggle-switch input:checked + .toggle-slider {
background: var(--accent-blue);
}
.toggle-switch input:checked + .toggle-slider::before {
transform: translateX(20px);
}
.toggle-switch input:focus + .toggle-slider {
box-shadow: 0 0 0 3px var(--ring-color);
}
.toggle-label {
font-size: 0.875rem;
color: var(--text-secondary);
}
.info-text {
text-align: center;
color: var(--text-muted);
font-size: 0.8125rem;
margin-top: 8px;
}
</style>
</head>
<body>
<a href="index.html" class="back-link">
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<path d="M19 12H5M12 19l-7-7 7-7"/>
</svg>
All Tools
</a>
<header>
<h1>Rich Text HTML Extractor</h1>
<p class="subtitle">Paste formatted text to extract HTML</p>
</header>
<div class="controls">
<button id="clearBtn" class="secondary">Clear All</button>
<label class="toggle-switch">
<input type="checkbox" id="stripAttributes" checked>
<span class="toggle-slider" aria-hidden="true"></span>
<span class="toggle-label">Strip attributes</span>
</label>
</div>
<div class="container">
<div class="pane">
<div class="pane-header">
<span class="label">
<svg class="icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<path d="M16 4h2a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2h2"></path>
<rect x="8" y="2" width="8" height="4" rx="1" ry="1"></rect>
</svg>
Paste Area
</span>
<span id="pasteStatus" class="paste-status">
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5">
<polyline points="20 6 9 17 4 12"></polyline>
</svg>
Extracted
</span>
</div>
<textarea id="pasteArea" placeholder="Paste rich text here (Ctrl+V or ⌘V)..." spellcheck="false"></textarea>
</div>
<div class="pane">
<div class="pane-header">
<span class="label">
<svg class="icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<polyline points="16 18 22 12 16 6"></polyline>
<polyline points="8 6 2 12 8 18"></polyline>
</svg>
HTML Output
</span>
<button id="copyBtn" class="copy-btn">Copy</button>
</div>
<textarea id="htmlOutput" readonly placeholder="HTML code will appear here..." spellcheck="false"></textarea>
</div>
<div class="pane">
<div class="pane-header">
<span class="label">
<svg class="icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<path d="M1 12s4-8 11-8 11 8 11 8-4 8-11 8-11-8-11-8z"></path>
<circle cx="12" cy="12" r="3"></circle>
</svg>
Preview
</span>
</div>
<div id="previewContent"></div>
</div>
</div>
<script>
const pasteArea = document.getElementById('pasteArea');
const pasteStatus = document.getElementById('pasteStatus');
const htmlOutput = document.getElementById('htmlOutput');
const previewContent = document.getElementById('previewContent');
const clearBtn = document.getElementById('clearBtn');
const copyBtn = document.getElementById('copyBtn');
const stripAttributesCheckbox = document.getElementById('stripAttributes');
let rawHtml = '';
const voidElements = new Set(['area','base','br','col','embed','hr','img','input','link','meta','source','track','wbr']);
function prettyPrint(html) {
const doc = new DOMParser().parseFromString(html, 'text/html');
const lines = [];
function serialize(node, indent) {
if (node.nodeType === Node.TEXT_NODE) {
const text = node.textContent.trim();
if (text) lines.push(' '.repeat(indent) + text);
} else if (node.nodeType === Node.ELEMENT_NODE) {
const tag = node.tagName.toLowerCase();
let attrs = '';
for (const attr of node.attributes) {
attrs += ` ${attr.name}="${attr.value}"`;
}
if (voidElements.has(tag)) {
lines.push(' '.repeat(indent) + `<${tag}${attrs}>`);
} else if (node.children.length === 0 && node.textContent.trim().length < 80) {
lines.push(' '.repeat(indent) + `<${tag}${attrs}>${node.textContent.trim()}</${tag}>`);
} else {
lines.push(' '.repeat(indent) + `<${tag}${attrs}>`);
for (const child of node.childNodes) {
serialize(child, indent + 1);
}
lines.push(' '.repeat(indent) + `</${tag}>`);
}
}
}
for (const child of doc.body.childNodes) {
serialize(child, 0);
}
return lines.join('\n');
}
function stripAllAttributes(html) {
const doc = new DOMParser().parseFromString(html, 'text/html');
function processNode(node) {
if (node.nodeType === Node.ELEMENT_NODE) {
while (node.attributes.length > 0) {
node.removeAttribute(node.attributes[0].name);
}
for (const child of node.childNodes) {
processNode(child);
}
}
}
for (const child of doc.body.childNodes) {
processNode(child);
}
return doc.body.innerHTML;
}
function updateOutput() {
if (!rawHtml) {
htmlOutput.value = '';
previewContent.innerHTML = '';
return;
}
let outputHtml = stripAttributesCheckbox.checked
? stripAllAttributes(rawHtml)
: rawHtml;
htmlOutput.value = prettyPrint(outputHtml);
previewContent.innerHTML = outputHtml;
}
pasteArea.addEventListener('paste', function(e) {
const html = e.clipboardData.getData('text/html');
if (html) {
e.preventDefault();
rawHtml = html;
pasteArea.value = '';
updateOutput();
pasteStatus.classList.add('visible');
}
});
stripAttributesCheckbox.addEventListener('change', updateOutput);
clearBtn.addEventListener('click', () => {
pasteArea.value = '';
pasteStatus.classList.remove('visible');
htmlOutput.value = '';
previewContent.innerHTML = '';
rawHtml = '';
pasteArea.focus();
});
copyBtn.addEventListener('click', () => {
if (!htmlOutput.value) return;
navigator.clipboard.writeText(htmlOutput.value).then(() => {
const originalText = copyBtn.textContent;
copyBtn.textContent = 'Copied!';
copyBtn.classList.add('copied');
setTimeout(() => {
copyBtn.textContent = originalText;
copyBtn.classList.remove('copied');
}, 2000);
});
});
</script>
</body>
</html>