-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtool-13-Markdown-Preview-Editor.html
More file actions
541 lines (460 loc) · 18.6 KB
/
Copy pathtool-13-Markdown-Preview-Editor.html
File metadata and controls
541 lines (460 loc) · 18.6 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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tool 13 - Markdown Preview Editor - Developer Toolbox</title>
<link rel="stylesheet" href="assets/core.css">
<!-- CodeMirror for Editor -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.13/codemirror.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.13/theme/monokai.min.css">
<style>
.md-grid {
display: grid;
grid-template-columns: 1fr;
gap: 2rem;
margin-top: 2rem;
height: calc(100vh - 250px);
min-height: 600px;
}
@media (min-width: 900px) {
.md-grid {
grid-template-columns: 1fr 1fr;
}
}
.panel-container {
display: flex;
flex-direction: column;
gap: 1rem;
height: 100%;
}
.panel-header {
display: flex;
justify-content: space-between;
align-items: center;
}
.btn-controls {
display: flex;
gap: 0.5rem;
flex-wrap: wrap;
}
/* Editor Container */
.editor-container {
flex: 1;
border: 2px solid var(--border-color);
border-radius: var(--radius-md);
overflow: hidden;
display: flex;
flex-direction: column;
transition: all 0.3s;
}
.editor-container:focus-within {
border-color: var(--accent-primary);
box-shadow: 0 0 0 2px rgba(187, 134, 252, 0.2);
}
.CodeMirror {
flex: 1;
height: auto;
font-family: var(--font-mono);
font-size: 1rem;
line-height: 1.5;
background: rgba(0, 0, 0, 0.5) !important;
}
/* Preview Container */
.preview-container {
flex: 1;
background: rgba(255, 255, 255, 0.05);
border: 2px solid var(--border-color);
border-radius: var(--radius-md);
padding: 2rem;
overflow-y: auto;
color: var(--text-primary);
font-family: var(--font-ui);
line-height: 1.6;
}
/* Markdown Styles inside Preview */
.preview-container h1,
.preview-container h2,
.preview-container h3,
.preview-container h4,
.preview-container h5,
.preview-container h6 {
color: var(--text-primary);
margin-top: 1.5em;
margin-bottom: 0.5em;
font-weight: 600;
line-height: 1.2;
}
.preview-container h1 {
font-size: 2.5em;
border-bottom: 1px solid var(--border-color);
padding-bottom: 0.3em;
}
.preview-container h2 {
font-size: 2em;
border-bottom: 1px solid var(--border-color);
padding-bottom: 0.3em;
}
.preview-container h3 {
font-size: 1.5em;
}
.preview-container p {
margin-top: 0;
margin-bottom: 16px;
}
.preview-container a {
color: var(--accent-primary);
text-decoration: none;
}
.preview-container a:hover {
text-decoration: underline;
}
.preview-container ul,
.preview-container ol {
padding-left: 2em;
margin-top: 0;
margin-bottom: 16px;
}
.preview-container blockquote {
padding: 0 1em;
color: var(--text-secondary);
border-left: 0.25em solid var(--accent-primary);
margin: 0 0 16px 0;
background: rgba(187, 134, 252, 0.05);
}
.preview-container code {
padding: 0.2em 0.4em;
margin: 0;
font-size: 85%;
background-color: rgba(255, 255, 255, 0.1);
border-radius: 6px;
font-family: var(--font-mono);
}
.preview-container pre {
padding: 16px;
overflow: auto;
font-size: 85%;
line-height: 1.45;
background-color: #1e1e1e !important;
border-radius: 6px;
border: 1px solid var(--border-color);
}
.preview-container pre code {
background-color: transparent;
padding: 0;
border-radius: 0;
}
.preview-container table {
border-spacing: 0;
border-collapse: collapse;
width: 100%;
margin-bottom: 16px;
}
.preview-container table th,
.preview-container table td {
padding: 6px 13px;
border: 1px solid var(--border-color);
}
.preview-container table tr:nth-child(2n) {
background-color: rgba(255, 255, 255, 0.02);
}
.preview-container hr {
height: 0.25em;
padding: 0;
margin: 24px 0;
background-color: var(--border-color);
border: 0;
}
.preview-container img {
max-width: 100%;
box-sizing: content-box;
background-color: transparent;
}
/* 3D background container */
#canvas-container {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: -1;
opacity: 0.3;
pointer-events: none;
}
</style>
</head>
<body class="theme-dark">
<div id="canvas-container"></div>
<main class="main-content" style="height: 100vh; display: flex; flex-direction: column;">
<div class="container" style="flex:1; display:flex; flex-direction:column; max-width: 1600px;">
<div class="tool-header">
<div>
<a href="index.html" class="back-link">← Back to Toolbox</a>
<h1>13. Markdown Preview Editor</h1>
<p style="color: var(--text-secondary); margin-top: 0.5rem;">Live rendering of GitHub-Flavored
Markdown.</p>
</div>
<div>
<select id="theme-select" aria-label="Select Theme">
<option value="dark">Dark Theme</option>
<option value="light">Light Theme</option>
<option value="solarized">Solarized</option>
<option value="neon">Neon Glow</option>
<option value="highcontrast">High Contrast</option>
</select>
</div>
</div>
<div class="md-grid">
<!-- Editor Panel -->
<div class="glass-panel panel-container">
<div class="panel-header">
<h3>Markdown</h3>
<div class="btn-controls">
<button id="btn-clear" class="btn">Clear</button>
<button id="btn-example" class="btn">Example</button>
</div>
</div>
<div class="editor-container" id="editor-wrapper">
<!-- CodeMirror mounts here -->
</div>
</div>
<!-- Preview Panel -->
<div class="glass-panel panel-container">
<div class="panel-header">
<h3>Preview</h3>
<div class="btn-controls">
<button id="btn-copy-html" class="btn btn-primary"
style="background:#03DAC6; color:#000;">Copy HTML</button>
</div>
</div>
<div class="preview-container" id="preview-box" class="markdown-body">
<!-- Rendered HTML appears here -->
</div>
</div>
</div>
</div>
</main>
<!-- Three.js and GSAP for 3D Background -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.12.2/gsap.min.js"></script>
<!-- CodeMirror for Editor -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.13/codemirror.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.13/mode/markdown/markdown.min.js"></script>
<!-- Marked.js for parsing Markdown -->
<script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
<script src="assets/core.js"></script>
<script>
// --- Setup Markdown Parser ---
// Configure Marked to use GitHub Flavored Markdown (GFM)
marked.setOptions({
gfm: true,
breaks: true, // translate \n to <br> naturally
headerIds: true,
mangle: false, // Don't escape emails
pedantic: false,
smartLists: true, // Use smarter list behavior than original markdown.
smartypants: false // Don't convert quotes to smart quotes
});
// --- Core Logic ---
const previewBox = document.getElementById('preview-box');
let editorInstance = null;
const defaultText = `# Welcome to Developer Toolbox!
This is a live Markdown editor. Type on the left, see it rendered on the right.
## Features Supported:
- **Bold**, *Italics*, \`Inline Code\`
- [Links](https://github.com)
- Lists (Ordered and Unordered)
- Blockquotes
> "The best way to predict the future is to invent it."
> — Alan Kay
### Code Blocks
\`\`\`javascript
function initializeMarkdown() {
console.log("Markdown ready!");
renderer.render(text);
}
\`\`\`
### Tables
| Header 1 | Header 2 |
| -------- | -------- |
| Cell 1 | Cell 2 |
| Cell 3 | Cell 4 |
### Task Lists
- [x] Write code
- [x] Test code
- [ ] Deploy code
*Happy documenting!*
`;
function updatePreview(val) {
previewBox.innerHTML = marked.parse(val);
if (window.triggerAnim) window.triggerAnim('type');
}
// Initialize CodeMirror
editorInstance = CodeMirror(document.getElementById('editor-wrapper'), {
value: defaultText,
mode: "markdown",
theme: document.body.classList.contains('theme-light') ? "default" : "monokai",
lineNumbers: true,
lineWrapping: true,
autofocus: true
});
// Listen for changes
let typeTimeout;
editorInstance.on('change', () => {
updatePreview(editorInstance.getValue());
// Trigger 3D book flip
clearTimeout(typeTimeout);
if (window.triggerAnim) window.triggerAnim('flip');
typeTimeout = setTimeout(() => {
if (window.triggerAnim) window.triggerAnim('idle');
}, 500);
});
// Initial render
updatePreview(defaultText);
// Theme sync listener via MutationObserver on body
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.attributeName === 'class' && editorInstance) {
const isDark = document.body.classList.contains('theme-light') ? false : true;
editorInstance.setOption("theme", isDark ? "monokai" : "default");
}
});
});
observer.observe(document.body, { attributes: true });
// Setup Buttons
document.getElementById('btn-clear').addEventListener('click', () => {
editorInstance.setValue('');
});
document.getElementById('btn-example').addEventListener('click', () => {
editorInstance.setValue(defaultText);
});
document.getElementById('btn-copy-html').addEventListener('click', (e) => {
const btn = e.target;
const html = marked.parse(editorInstance.getValue());
navigator.clipboard.writeText(html);
const originalText = btn.textContent;
btn.textContent = 'Copied!';
setTimeout(() => btn.textContent = originalText, 1500);
});
// --- 3D Background Setup (Floating Book / Pages) ---
let scene, camera, renderer, bookGroup;
let leftPage, rightPage, spine;
const init3D = () => {
const container = document.getElementById('canvas-container');
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 100);
camera.position.z = 20;
renderer = new THREE.WebGLRenderer({ alpha: true, antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
container.appendChild(renderer.domElement);
const ambientLight = new THREE.AmbientLight(0xffffff, 0.6);
scene.add(ambientLight);
const dirLight = new THREE.DirectionalLight(0xffffff, 0.8);
dirLight.position.set(5, 10, 5);
scene.add(dirLight);
const pointLight = new THREE.PointLight(0xBB86FC, 1, 50);
pointLight.position.set(-5, 0, 5);
scene.add(pointLight);
const pointLight2 = new THREE.PointLight(0x03DAC6, 1, 50);
pointLight2.position.set(5, 0, 5);
scene.add(pointLight2);
bookGroup = new THREE.Group();
// Materials
const coverMat = new THREE.MeshStandardMaterial({ color: 0x1e1e1e, roughness: 0.8 });
const pageMat = new THREE.MeshStandardMaterial({
color: 0x33d9b2,
roughness: 0.4,
metalness: 0.1,
side: THREE.DoubleSide,
transparent: true,
opacity: 0.9
});
// Spine
spine = new THREE.Mesh(new THREE.BoxGeometry(0.5, 6, 0.2), coverMat);
bookGroup.add(spine);
// Left Page Pivot Group
leftPage = new THREE.Group();
leftPage.position.set(-0.25, 0, 0); // attached to left of spine
const lPageMesh = new THREE.Mesh(new THREE.PlaneGeometry(4, 5.8), pageMat);
lPageMesh.position.set(-2, 0, 0); // move mesh out from pivot
lPageMesh.rotation.x = -Math.PI / 2; // lay flat initially, wait building book right way up
// Re-orient for standing book
lPageMesh.rotation.set(0, 0, 0);
leftPage.add(lPageMesh);
leftPage.rotation.y = Math.PI / 8; // slightly open
bookGroup.add(leftPage);
// Right Page Pivot Group
rightPage = new THREE.Group();
rightPage.position.set(0.25, 0, 0); // attached to right of spine
const rPageMesh = new THREE.Mesh(new THREE.PlaneGeometry(4, 5.8), pageMat);
rPageMesh.position.set(2, 0, 0);
rightPage.add(rPageMesh);
rightPage.rotation.y = -Math.PI / 8; // slightly open
bookGroup.add(rightPage);
// Add some "flipping" pages in between
const flippingPages = [];
for (let i = 0; i < 3; i++) {
const pGroup = new THREE.Group();
pGroup.position.set(0, 0, 0);
const pMesh = new THREE.Mesh(new THREE.PlaneGeometry(3.9, 5.7), new THREE.MeshStandardMaterial({
color: 0xBB86FC, side: THREE.DoubleSide, transparent: true, opacity: 0.5
}));
pMesh.position.set(1.95, 0, 0);
pGroup.add(pMesh);
pGroup.rotation.y = (Math.random() * -Math.PI / 2) + Math.PI / 4; // Random states
bookGroup.add(pGroup);
flippingPages.push(pGroup);
}
// Tilt book towards camera for top-downish view
bookGroup.rotation.x = 0.5;
bookGroup.position.y = -2;
scene.add(bookGroup);
window.triggerAnim = (state) => {
if (state === 'flip') {
// Randomly animate the inner pages when typing
flippingPages.forEach((p, i) => {
gsap.to(p.rotation, {
y: (Math.random() * -Math.PI / 1.5) + Math.PI / 3, // flip back and forth randomly
duration: 0.3 + (Math.random() * 0.2),
ease: "power1.inOut"
});
});
} else if (state === 'idle') {
// settle pages near right side
flippingPages.forEach((p, i) => {
gsap.to(p.rotation, {
y: -Math.PI / 8 + (i * -0.05), // stack near right page
duration: 1,
ease: "power2.out"
});
});
}
};
const animate = () => {
requestAnimationFrame(animate);
// Idle hover of the entire book
bookGroup.position.y = Math.sin(Date.now() * 0.001) * 0.5 - 2;
bookGroup.rotation.z = Math.sin(Date.now() * 0.0005) * 0.05;
bookGroup.rotation.y = Math.sin(Date.now() * 0.0003) * 0.1;
renderer.render(scene, camera);
};
animate();
window.addEventListener('resize', () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
if (window.innerWidth < 900) {
camera.position.z = 25;
} else {
camera.position.z = 20;
}
});
window.dispatchEvent(new Event('resize'));
};
if (document.getElementById('canvas-container')) {
init3D();
}
</script>
</body>
</html>