forked from kerzol/markdown-mathjax
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditor.html
More file actions
851 lines (740 loc) · 23.6 KB
/
editor.html
File metadata and controls
851 lines (740 loc) · 23.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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
<!DOCTYPE html>
<html>
<head>
<title>Live markdown editor based on MathJax and Marked</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- based on MathJax/test/sample-dynamic-2.html -->
<!-- Add Font Awesome for icons -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
<!-- Add modern CSS framework -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">
<style>
:root {
--bg-color: #ffffff;
--text-color: #333333;
--border-color: #dddddd;
--highlight-color: #73A0C5;
--editor-bg: #ffffff;
--preview-bg: #ffffff;
}
.dark-mode {
--bg-color: #1e1e1e;
--text-color: #f0f0f0;
--border-color: #444444;
--highlight-color: #4a8bbf;
--editor-bg: #2d2d2d;
--preview-bg: #2d2d2d;
}
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
line-height: 1.6;
color: var(--text-color);
background-color: var(--bg-color);
transition: background-color 0.3s ease, color 0.3s ease;
margin: 0;
padding: 20px;
}
.container {
max-width: 1200px;
margin: 0 auto;
}
.header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 20px;
}
.controls {
display: flex;
gap: 10px;
margin-bottom: 15px;
}
.btn {
background-color: var(--highlight-color);
color: white;
border: none;
padding: 8px 15px;
border-radius: 4px;
cursor: pointer;
transition: background-color 0.2s;
}
.btn:hover {
opacity: 0.9;
}
.toggle-switch {
position: relative;
display: inline-block;
width: 60px;
height: 30px;
}
.toggle-switch input {
opacity: 0;
width: 0;
height: 0;
}
.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ccc;
transition: .4s;
border-radius: 30px;
}
.slider:before {
position: absolute;
content: "";
height: 22px;
width: 22px;
left: 4px;
bottom: 4px;
background-color: white;
transition: .4s;
border-radius: 50%;
}
input:checked + .slider {
background-color: var(--highlight-color);
}
input:checked + .slider:before {
transform: translateX(30px);
}
.toolbar {
display: flex;
flex-wrap: wrap;
gap: 5px;
margin-bottom: 10px;
padding: 5px;
background-color: rgba(0,0,0,0.05);
border-radius: 4px;
}
.toolbar button {
background-color: transparent;
border: 1px solid var(--border-color);
border-radius: 4px;
padding: 5px 10px;
cursor: pointer;
color: var(--text-color);
transition: background-color 0.2s;
}
.toolbar button:hover {
background-color: rgba(0,0,0,0.1);
}
.editor-container {
display: flex;
flex-direction: column;
gap: 20px;
}
@media (min-width: 768px) {
.editor-container {
flex-direction: row;
}
.editor-section, .preview-section {
flex: 1;
}
}
.editor-section, .preview-section {
display: flex;
flex-direction: column;
}
.section-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 5px;
font-weight: bold;
}
textarea {
width: 100%;
height: 400px;
padding: 15px;
border: 1px solid var(--border-color);
border-radius: 4px;
resize: vertical;
font-family: monospace;
font-size: 14px;
line-height: 1.5;
background-color: var(--editor-bg);
color: var(--text-color);
transition: border-color 0.3s, background-color 0.3s, color 0.3s;
}
textarea:focus {
outline: none;
border-color: var(--highlight-color);
box-shadow: 0 0 0 2px rgba(115, 160, 197, 0.2);
}
.preview {
height: 400px;
padding: 15px;
border: 1px solid var(--border-color);
border-radius: 4px;
overflow-y: auto;
background-color: var(--preview-bg);
transition: border-color 0.3s, background-color 0.3s;
}
.fullscreen {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
z-index: 9999;
padding: 20px;
background-color: var(--bg-color);
}
.fullscreen textarea, .fullscreen .preview {
height: calc(100vh - 150px);
}
.hint {
text-align: right;
color: var(--text-color);
opacity: 0.7;
font-size: small;
margin-top: 10px;
}
.hint a {
color: var(--highlight-color);
text-decoration: none;
}
.hint a:hover {
text-decoration: underline;
}
/* Original styles - keeping and adjusting */
blockquote {
color: var(--text-color);
opacity: 0.8;
padding-left: 10px;
padding-top: 3px;
padding-bottom: 3px;
margin: 0;
border-left: 3px solid var(--highlight-color);
}
blockquote > blockquote {
border-color: #CF908D;
}
blockquote > blockquote > blockquote {
border-color: #57C59C;
}
blockquote > blockquote > blockquote > blockquote {
border-color: #C957F8;
}
blockquote > blockquote > blockquote > blockquote > blockquote {
border-color: #F8B829;
}
blockquote > blockquote > blockquote > blockquote > blockquote > blockquote {
border-color: #8DA1C5;
}
blockquote > blockquote > blockquote > blockquote > blockquote > blockquote > blockquote {
border-color: #888;
}
blockquote p:first-of-type {
margin-top: 0;
}
blockquote p:last-of-type {
margin-bottom: 0;
}
/* Code blocks */
pre {
background-color: rgba(0,0,0,0.05);
padding: 10px;
border-radius: 4px;
overflow-x: auto;
}
code {
font-family: 'Courier New', Courier, monospace;
background-color: rgba(0,0,0,0.05);
padding: 2px 4px;
border-radius: 3px;
}
pre code {
background-color: transparent;
padding: 0;
}
/* Tables */
table {
border-collapse: collapse;
width: 100%;
margin: 15px 0;
}
table, th, td {
border: 1px solid var(--border-color);
}
th, td {
padding: 8px 12px;
text-align: left;
}
th {
background-color: rgba(0,0,0,0.05);
}
/* Mobile responsiveness */
@media (max-width: 768px) {
body {
padding: 10px;
}
.header {
flex-direction: column;
align-items: flex-start;
gap: 10px;
}
.controls {
flex-wrap: wrap;
}
/* Tabs for mobile view */
.tabs {
display: flex;
margin-bottom: 15px;
border-bottom: 1px solid var(--border-color);
}
.tab {
padding: 10px 15px;
cursor: pointer;
background-color: transparent;
border: none;
color: var(--text-color);
font-weight: bold;
border-bottom: 3px solid transparent;
}
.tab.active {
border-bottom: 3px solid var(--highlight-color);
color: var(--highlight-color);
}
.editor-section, .preview-section {
display: none;
width: 100%;
}
.editor-section.active, .preview-section.active {
display: block;
}
}
/* Hide tabs on larger screens */
@media (min-width: 769px) {
.tabs {
display: none;
}
.editor-section, .preview-section {
display: block !important;
}
}
</style>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
showProcessingMessages: false,
tex2jax: { inlineMath: [['$','$'],['\\(','\\)']] },
TeX: { equationNumbers: {autoNumber: "AMS"} }
});
</script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
<script type="text/javascript" src="lib/marked/lib/marked.js"></script>
<script>
marked.setOptions({
renderer: new marked.Renderer(),
gfm: true,
tables: true,
breaks: false,
pedantic: false,
sanitize: false, // IMPORTANT, because we do MathJax before markdown,
// however we do escaping in 'CreatePreview'.
smartLists: true,
smartypants: false
});
</script>
<script>
var Preview = {
delay: 50, // delay after keystroke before updating
preview: null, // filled in by Init below
buffer: null, // filled in by Init below
timeout: null, // store setTimout id
mjRunning: false, // true when MathJax is processing
oldText: null, // used to check if an update is needed
//
// Get the preview and buffer DIV's
//
Init: function () {
this.preview = document.getElementById("marked-mathjax-preview");
this.buffer = document.getElementById("marked-mathjax-preview-buffer");
this.textarea = document.getElementById("marked-mathjax-input");
},
//
// Switch the buffer and preview, and display the right one.
// (We use visibility:hidden rather than display:none since
// the results of running MathJax are more accurate that way.)
//
SwapBuffers: function () {
var buffer = this.preview;
var preview = this.buffer;
this.buffer = buffer;
this.preview = preview;
buffer.style.display = "none";
buffer.style.position = "absolute";
preview.style.position = "";
preview.style.display = "";
},
//
// This gets called when a key is pressed in the textarea.
// We check if there is already a pending update and clear it if so.
// Then set up an update to occur after a small delay (so if more keys
// are pressed, the update won't occur until after there has been
// a pause in the typing).
// The callback function is set up below, after the Preview object is set up.
//
Update: function () {
if (this.timeout) {clearTimeout(this.timeout)}
this.timeout = setTimeout(this.callback,this.delay);
},
//
// Creates the preview and runs MathJax on it.
// If MathJax is already trying to render the code, return
// If the text hasn't changed, return
// Otherwise, indicate that MathJax is running, and start the
// typesetting. After it is done, call PreviewDone.
//
CreatePreview: function () {
Preview.timeout = null;
if (this.mjRunning) return;
var text = this.textarea.value;
if (text === this.oldtext) return;
text = this.Escape(text); //Escape tags before doing stuff
this.buffer.innerHTML = this.oldtext = text;
this.mjRunning = true;
MathJax.Hub.Queue(
["Typeset",MathJax.Hub,this.buffer],
["PreviewDone",this],
["resetEquationNumbers", MathJax.InputJax.TeX]
);
},
//
// Indicate that MathJax is no longer running,
// do markdown over MathJax's result,
// and swap the buffers to show the results.
//
PreviewDone: function () {
this.mjRunning = false;
text = this.buffer.innerHTML;
text = this.PartialDescape(text);
this.buffer.innerHTML = marked (text);
this.SwapBuffers();
},
Escape: function (html, encode) {
return html
.replace(!encode ? /&(?!#?\w+;)/g : /&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
},
PartialDescape: function (html) {
var lines = html.split('\n');
var out = '';
// is true when we are
// ```
// inside a code block
// ```
var inside_code = false;
for (var i = 0; i < lines.length; i++) {
// a hack to properly rendre the blockquotes
if (lines[i].startsWith('>')) {
lines[i] = lines[i].replace(/>/g, '>');
}
// rendrer properly stuff like this
// ```c
// if (a > b)
// ```
if (inside_code) {
// inside the code we descape stuff
lines[i] = lines[i]
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, '\'');
}
if (lines[i].startsWith('```')) {
inside_code = ! inside_code;
}
out += lines[i] + '\n';
}
return out;
},
// The idea here is to perform fast updates.
// See http://stackoverflow.com/questions/11228558/let-pagedown-and-mathjax-work-together/21563171?noredirect=1#comment46869312_21563171
// But our implementation is a bit buggy: flickering, bad rendering when someone types very fast.
//
// If you want to enable such buggy fast updates, you should
// add something like onkeypress="Preview.UpdateKeyPress(event)" to textarea's attributes.
UpdateKeyPress: function (event) {
if (event.keyCode < 16 || event.keyCode > 47) {
this.preview.innerHTML = '<p>' + marked(this.textarea.value) + '</p>';
this.buffer.innerHTML = '<p>' + marked(this.textarea.value) + '</p>';
}
this.Update();
}
};
//
// Cache a callback to the CreatePreview action
//
Preview.callback = MathJax.Callback(["CreatePreview",Preview]);
Preview.callback.autoReset = true; // make sure it can run more than once</script>
</head>
<body>
<div class="container">
<div class="header">
<h1>Markdown + MathJax Editor</h1>
<div class="theme-toggle">
<label class="toggle-switch">
<input type="checkbox" id="darkModeToggle">
<span class="slider"></span>
</label>
<span id="themeLabel">Dark Mode</span>
</div>
</div>
<div class="controls">
<button class="btn" id="saveBtn"><i class="fas fa-save"></i> Save as MD</button>
<button class="btn" id="copyBtn"><i class="fas fa-copy"></i> Copy HTML</button>
<button class="btn" id="clearBtn"><i class="fas fa-trash-alt"></i> Clear</button>
<a href="https://github.com/iamakashtechie/markdown-mathjax-viewer" class="btn" target="_blank">
<i class="fab fa-github"></i> Source
</a>
</div>
<div class="tabs">
<button class="tab active" id="editorTab">Editor</button>
<button class="tab" id="previewTab">Preview</button>
</div>
<div class="editor-container">
<div class="editor-section active">
<div class="section-header">
<span>Editor</span>
<button class="btn-sm" id="editorFullscreen"><i class="fas fa-expand"></i></button>
</div>
<div class="toolbar">
<button title="Bold" onclick="insertMarkdown('**', '**')"><i class="fas fa-bold"></i></button>
<button title="Italic" onclick="insertMarkdown('*', '*')"><i class="fas fa-italic"></i></button>
<button title="Heading" onclick="insertMarkdown('## ', '')"><i class="fas fa-heading"></i></button>
<button title="Link" onclick="insertMarkdown('[', '](url)')"><i class="fas fa-link"></i></button>
<button title="Image" onclick="insertMarkdown('')"><i class="fas fa-image"></i></button>
<button title="Code" onclick="insertMarkdown('```\n', '\n```')"><i class="fas fa-code"></i></button>
<button title="List" onclick="insertMarkdown('- ', '')"><i class="fas fa-list"></i></button>
<button title="Blockquote" onclick="insertMarkdown('> ', '')"><i class="fas fa-quote-right"></i></button>
<button title="Table" onclick="insertTable()"><i class="fas fa-table"></i></button>
<button title="Math Inline" onclick="insertMarkdown('$', '$')"><i class="fas fa-square-root-alt"></i></button>
<button title="Math Block" onclick="insertMarkdown('$$\n', '\n$$')"><i class="fas fa-superscript"></i></button>
</div>
<textarea id="marked-mathjax-input"
onkeyup="Preview.Update()"
name="comment"
autofocus
placeholder="Type your markdown and LaTeX here...">
```
#include<cmath>
#include <stdio.h>
int main()
{
// printf() displays the string inside quotation
printf("Hello, World!");
return 0;
}
```
##### test
$ \Gamma, G$
$A_G A_\Gamma$
> test
>> $\Gamma$ [Example](http://exmaple.com)
<marquee> NOT! </marquee>
$$
\begin{array}{|c|c|c|c|}
\hline
1& 1 > -1 & 1 & -1 \\ \hline
& 3 & 55 & 44\\ \hline
& 4 & 93 & 33\\ \hline
& 5 & 6 & 22\\ \hline
\end{array}$$
```
if (1 <b> a </b> 2)
```
</textarea>
<div class="hint">Use
<a href="http://en.wikibooks.org/wiki/LaTeX/Mathematics" target="_blank">$\LaTeX$</a>
to type formulæ
and <a href="https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet" target="_blank">markdown</a> to format text.
</div>
</div>
<div class="preview-section">
<div class="section-header">
<span>Preview</span>
<button class="btn-sm" id="previewFullscreen"><i class="fas fa-expand"></i></button>
</div>
<div class="preview" id="marked-mathjax-preview"></div>
<div class="preview" id="marked-mathjax-preview-buffer"
style="display:none;
position:absolute;
top:0; left: 0"></div>
</div>
</div>
</div>
<script>
Preview.Init();
Preview.Update();
// Enhanced functionality
document.addEventListener('DOMContentLoaded', function() {
// Dark mode toggle
const darkModeToggle = document.getElementById('darkModeToggle');
const themeLabel = document.getElementById('themeLabel');
// Check for saved theme preference or prefer-color-scheme
const savedTheme = localStorage.getItem('theme');
const prefersDark = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
if (savedTheme === 'dark' || (!savedTheme && prefersDark)) {
document.body.classList.add('dark-mode');
darkModeToggle.checked = true;
themeLabel.textContent = 'Light Mode';
}
darkModeToggle.addEventListener('change', function() {
if (this.checked) {
document.body.classList.add('dark-mode');
localStorage.setItem('theme', 'dark');
themeLabel.textContent = 'Light Mode';
} else {
document.body.classList.remove('dark-mode');
localStorage.setItem('theme', 'light');
themeLabel.textContent = 'Dark Mode';
}
});
// Save to markdown file
document.getElementById('saveBtn').addEventListener('click', function() {
const content = document.getElementById('marked-mathjax-input').value;
const blob = new Blob([content], { type: 'text/markdown' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'document.md';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
});
// Copy HTML
document.getElementById('copyBtn').addEventListener('click', function() {
const htmlContent = document.getElementById('marked-mathjax-preview').innerHTML;
// Create temporary textarea for copying
const textarea = document.createElement('textarea');
textarea.value = htmlContent;
document.body.appendChild(textarea);
textarea.select();
try {
document.execCommand('copy');
alert('HTML copied to clipboard!');
} catch (err) {
console.error('Failed to copy: ', err);
alert('Failed to copy HTML to clipboard');
}
document.body.removeChild(textarea);
});
// Clear editor
document.getElementById('clearBtn').addEventListener('click', function() {
if (confirm('Are you sure you want to clear the editor?')) {
document.getElementById('marked-mathjax-input').value = '';
Preview.Update();
}
});
// Fullscreen mode for editor
document.getElementById('editorFullscreen').addEventListener('click', function() {
const editorSection = document.querySelector('.editor-section');
editorSection.classList.toggle('fullscreen');
const icon = this.querySelector('i');
if (editorSection.classList.contains('fullscreen')) {
icon.classList.remove('fa-expand');
icon.classList.add('fa-compress');
} else {
icon.classList.remove('fa-compress');
icon.classList.add('fa-expand');
}
});
// Fullscreen mode for preview
document.getElementById('previewFullscreen').addEventListener('click', function() {
const previewSection = document.querySelector('.preview-section');
previewSection.classList.toggle('fullscreen');
const icon = this.querySelector('i');
if (previewSection.classList.contains('fullscreen')) {
icon.classList.remove('fa-expand');
icon.classList.add('fa-compress');
} else {
icon.classList.remove('fa-compress');
icon.classList.add('fa-expand');
}
});
// Tab switching for mobile view
const editorTab = document.getElementById('editorTab');
const previewTab = document.getElementById('previewTab');
const editorSection = document.querySelector('.editor-section');
const previewSection = document.querySelector('.preview-section');
editorTab.addEventListener('click', function() {
editorTab.classList.add('active');
previewTab.classList.remove('active');
editorSection.classList.add('active');
previewSection.classList.remove('active');
});
previewTab.addEventListener('click', function() {
previewTab.classList.add('active');
editorTab.classList.remove('active');
previewSection.classList.add('active');
editorSection.classList.remove('active');
});
});
// Function to insert markdown syntax
function insertMarkdown(before, after) {
const textarea = document.getElementById('marked-mathjax-input');
const start = textarea.selectionStart;
const end = textarea.selectionEnd;
const selectedText = textarea.value.substring(start, end);
const replacement = before + selectedText + after;
textarea.value = textarea.value.substring(0, start) + replacement + textarea.value.substring(end);
// Set cursor position based on whether text was selected
if (selectedText.length > 0) {
textarea.selectionStart = start;
textarea.selectionEnd = start + replacement.length;
} else {
textarea.selectionStart = start + before.length;
textarea.selectionEnd = start + before.length;
}
textarea.focus();
Preview.Update();
}
// Function to insert a markdown table template
function insertTable() {
const tableTemplate = `
| Header 1 | Header 2 | Header 3 |
|----------|----------|----------|
| Row 1 | Data | Data |
| Row 2 | Data | Data |
`;
insertMarkdown(tableTemplate, '');
}
// Auto-save functionality
let autoSaveInterval = setInterval(function() {
const content = document.getElementById('marked-mathjax-input').value;
localStorage.setItem('markdown-content', content);
}, 5000);
// Load saved content on page load
window.addEventListener('load', function() {
const savedContent = localStorage.getItem('markdown-content');
if (savedContent && document.getElementById('marked-mathjax-input').value.trim() === '') {
document.getElementById('marked-mathjax-input').value = savedContent;
Preview.Update();
}
});
// Debounce function for smoother preview updates
function debounce(func, wait) {
let timeout;
return function() {
const context = this;
const args = arguments;
clearTimeout(timeout);
timeout = setTimeout(() => func.apply(context, args), wait);
};
}
// Override the Update method with debounced version for smoother preview
const originalUpdate = Preview.Update;
Preview.Update = debounce(originalUpdate.bind(Preview), 300);
</script>
</body>
</html>