-
-
Notifications
You must be signed in to change notification settings - Fork 153
Expand file tree
/
Copy pathnotes-to-markdown.html
More file actions
620 lines (527 loc) · 14.5 KB
/
notes-to-markdown.html
File metadata and controls
620 lines (527 loc) · 14.5 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Notes to Markdown</title>
<style>
* {
box-sizing: border-box;
}
body {
font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
max-width: 1200px;
margin: 0 auto;
padding: 20px;
background: #f5f5f7;
color: #1d1d1f;
line-height: 1.6;
}
h1 {
color: #1d1d1f;
margin-bottom: 10px;
font-weight: 600;
}
.instructions {
background: #fff;
border-left: 4px solid #ffcc00;
padding: 15px;
margin-bottom: 20px;
border-radius: 4px;
box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}
.paste-area {
width: 100%;
min-height: 120px;
padding: 15px;
border: 2px dashed #86868b;
border-radius: 8px;
background: #fff;
color: #1d1d1f;
font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
font-size: 16px;
margin-bottom: 20px;
outline: none;
transition: border-color 0.3s, box-shadow 0.3s;
}
.paste-area:focus {
border-color: #ffcc00;
border-style: solid;
box-shadow: 0 0 0 3px rgba(255, 204, 0, 0.3);
}
.paste-area::placeholder {
color: #86868b;
}
.results {
display: flex;
flex-direction: column;
gap: 20px;
}
.section {
background: #fff;
border-radius: 12px;
padding: 20px;
box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}
.section h2 {
margin-top: 0;
color: #1d1d1f;
font-size: 18px;
font-weight: 600;
border-bottom: 1px solid #e5e5e5;
padding-bottom: 10px;
margin-bottom: 15px;
}
.code-output {
width: 100%;
min-height: 200px;
padding: 15px;
border: 1px solid #e5e5e5;
border-radius: 8px;
background: #fafafa;
color: #1d1d1f;
font-family: 'SF Mono', Monaco, 'Courier New', Courier, monospace;
font-size: 14px;
resize: vertical;
overflow-x: auto;
white-space: pre-wrap;
}
.button-group {
display: flex;
gap: 10px;
margin-bottom: 15px;
flex-wrap: wrap;
}
button {
padding: 10px 20px;
background: #ffcc00;
color: #1d1d1f;
border: none;
border-radius: 8px;
cursor: pointer;
font-size: 14px;
font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
font-weight: 600;
transition: all 0.2s;
}
button:hover {
background: #e6b800;
}
button:active {
background: #cca300;
}
button.copied {
background: #007aff;
color: #fff;
}
.preview-container {
background: #fff;
border: 1px solid #e5e5e5;
border-radius: 8px;
padding: 15px;
overflow-x: auto;
}
.preview-container a {
color: #007aff;
text-decoration: none;
}
.preview-container a:hover {
text-decoration: underline;
}
.empty-state {
text-align: center;
color: #86868b;
padding: 40px;
font-size: 18px;
}
.error {
background: #fff;
border-left: 4px solid #ff3b30;
padding: 15px;
margin-bottom: 20px;
border-radius: 4px;
color: #ff3b30;
}
details {
margin-top: 10px;
}
summary {
cursor: pointer;
color: #86868b;
font-size: 14px;
padding: 8px 0;
user-select: none;
}
summary:hover {
color: #1d1d1f;
}
.rtf-debug {
width: 100%;
min-height: 150px;
max-height: 400px;
padding: 15px;
border: 1px solid #e5e5e5;
border-radius: 8px;
background: #f0f0f0;
color: #666;
font-family: 'SF Mono', Monaco, 'Courier New', Courier, monospace;
font-size: 12px;
resize: vertical;
overflow: auto;
white-space: pre-wrap;
word-break: break-all;
margin-top: 10px;
}
@media (max-width: 768px) {
body {
padding: 10px;
}
h1 {
font-size: 24px;
}
.button-group {
flex-direction: column;
}
button {
width: 100%;
}
}
</style>
</head>
<body>
<h1>Notes to Markdown</h1>
<div class="instructions">
<strong>Instructions:</strong> Paste content from Apple Notes below. The RTF data will be converted to Markdown and HTML with hyperlinks preserved.
</div>
<textarea class="paste-area" placeholder="Click here and paste your Apple Notes content (Cmd+V)..."></textarea>
<div class="results" id="results">
<div class="empty-state">Paste Apple Notes content to see the conversion</div>
</div>
<script>
const pasteArea = document.querySelector('.paste-area');
const resultsDiv = document.getElementById('results');
function parseRTFHyperlinks(rtf) {
const results = [];
let i = 0;
while (i < rtf.length) {
// Look for hyperlink field pattern
const fieldStart = rtf.indexOf('{\\field{\\*\\fldinst{HYPERLINK', i);
if (fieldStart === -1) break;
// Find the URL
const urlStart = rtf.indexOf('"', fieldStart);
if (urlStart === -1) break;
const urlEnd = rtf.indexOf('"', urlStart + 1);
if (urlEnd === -1) break;
const url = rtf.substring(urlStart + 1, urlEnd);
// Find the link text in fldrslt
const fldrsltStart = rtf.indexOf('{\\fldrslt', fieldStart);
if (fldrsltStart === -1) break;
// Extract the text, handling nested braces
let braceCount = 0;
let textStart = -1;
let textEnd = -1;
for (let j = fldrsltStart; j < rtf.length; j++) {
if (rtf[j] === '{') {
braceCount++;
} else if (rtf[j] === '}') {
braceCount--;
if (braceCount === 0) {
textEnd = j;
break;
}
}
}
if (textEnd === -1) break;
// Extract text content from fldrslt, removing RTF control codes
let fldrsltContent = rtf.substring(fldrsltStart + 9, textEnd);
let linkText = extractTextFromRTF(fldrsltContent);
results.push({
start: fieldStart,
end: textEnd + 1,
url: url,
text: linkText.trim()
});
i = textEnd + 1;
}
return results;
}
function extractTextFromRTF(rtf) {
let text = '';
let i = 0;
while (i < rtf.length) {
const char = rtf[i];
if (char === '\\') {
// Handle control sequence
let j = i + 1;
// Check for escaped characters
if (j < rtf.length) {
const nextChar = rtf[j];
if (nextChar === '\\' || nextChar === '{' || nextChar === '}') {
text += nextChar;
i = j + 1;
continue;
}
if (nextChar === '\n' || nextChar === '\r') {
text += '\n';
i = j + 1;
continue;
}
// Handle hex characters \'xx
if (nextChar === "'") {
j++;
let hexCode = '';
while (j < rtf.length && /[0-9a-fA-F]/.test(rtf[j]) && hexCode.length < 2) {
hexCode += rtf[j];
j++;
}
if (hexCode.length === 2) {
text += String.fromCharCode(parseInt(hexCode, 16));
}
i = j;
continue;
}
}
// Read control word
let controlWord = '';
while (j < rtf.length && /[a-z]/i.test(rtf[j])) {
controlWord += rtf[j];
j++;
}
// Read numeric parameter
let numParam = '';
while (j < rtf.length && /[-0-9]/.test(rtf[j])) {
numParam += rtf[j];
j++;
}
// Skip optional space after control word
if (j < rtf.length && rtf[j] === ' ') {
j++;
}
// Handle Unicode
if (controlWord === 'u') {
const codePoint = parseInt(numParam);
if (!isNaN(codePoint)) {
if (codePoint < 0) {
text += String.fromCharCode(65536 + codePoint);
} else {
text += String.fromCharCode(codePoint);
}
}
// Skip replacement character
if (j < rtf.length && rtf[j] === '?') {
j++;
}
}
i = j;
} else if (char === '{' || char === '}') {
i++;
} else if (char === '\n' || char === '\r') {
i++;
} else {
text += char;
i++;
}
}
return text;
}
function stripRTFHeader(rtf) {
// Remove the RTF header including fonttbl, colortbl, expandedcolortbl, and other preamble
// Find the end of the header section (after all the table definitions)
// Remove font table
rtf = rtf.replace(/\{\\fonttbl[^}]*(\{[^}]*\})*[^}]*\}/g, '');
// Remove color table
rtf = rtf.replace(/\{\\colortbl[^}]*\}/g, '');
// Remove expanded color table
rtf = rtf.replace(/\{\\\*\\expandedcolortbl[^}]*\}/g, '');
// Remove RTF header declaration
rtf = rtf.replace(/^\{\\rtf1\\ansi[^\\]*/, '{');
rtf = rtf.replace(/\\cocoartf\d+/g, '');
rtf = rtf.replace(/\\cocoatextscaling\d+/g, '');
rtf = rtf.replace(/\\cocoaplatform\d+/g, '');
rtf = rtf.replace(/\\deftab\d+/g, '');
return rtf;
}
function rtfToMarkdownAndHtml(rtf) {
// Strip the RTF header/preamble before processing
rtf = stripRTFHeader(rtf);
const hyperlinks = parseRTFHyperlinks(rtf);
// Build output by processing the RTF and inserting markdown links
let markdown = '';
let html = '';
let lastEnd = 0;
// Get all plain text between hyperlinks
function getPlainTextBetween(start, end) {
let segment = rtf.substring(start, end);
// Remove paragraph formatting
segment = segment.replace(/\\pard[^\\}]*/g, '');
segment = segment.replace(/\\pardeftab\d+/g, '');
segment = segment.replace(/\\slleading\d+/g, '');
segment = segment.replace(/\\partightenfactor\d+/g, '');
segment = segment.replace(/\\pardirnatural/g, '');
// Handle line breaks
segment = segment.replace(/\\\\/g, '\n');
// Extract text
return extractTextFromRTF(segment);
}
for (const link of hyperlinks) {
// Add text before this link
const textBefore = getPlainTextBetween(lastEnd, link.start);
markdown += textBefore;
html += escapeHtml(textBefore);
// Add the link
if (link.text) {
markdown += `[${link.text}](${link.url})`;
html += `<a href="${escapeHtml(link.url)}">${escapeHtml(link.text)}</a>`;
} else {
markdown += link.url;
html += `<a href="${escapeHtml(link.url)}">${escapeHtml(link.url)}</a>`;
}
lastEnd = link.end;
}
// Add remaining text
const textAfter = getPlainTextBetween(lastEnd, rtf.length);
markdown += textAfter;
html += escapeHtml(textAfter);
// Clean up the output
markdown = cleanupText(markdown);
html = cleanupText(html);
// Convert newlines to <br> for HTML
html = html.replace(/\n/g, '<br>\n');
return { markdown, html };
}
function cleanupText(text) {
// Remove any remaining control sequences that slipped through
text = text.replace(/\\cf\d+/g, '');
text = text.replace(/\\f\d+/g, '');
text = text.replace(/\\fs\d+/g, '');
text = text.replace(/\\b\d*/g, '');
text = text.replace(/\\i\d*/g, '');
text = text.replace(/\\[a-z]+\d*/g, '');
// Clean up extra whitespace but preserve intentional line breaks
text = text.replace(/[ \t]+/g, ' ');
text = text.replace(/\n +/g, '\n');
text = text.replace(/ +\n/g, '\n');
text = text.replace(/\n{3,}/g, '\n\n');
return text.trim();
}
function escapeHtml(text) {
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}
pasteArea.addEventListener('paste', async (e) => {
e.preventDefault();
const clipboardData = e.clipboardData;
resultsDiv.innerHTML = '';
if (!clipboardData) {
resultsDiv.innerHTML = '<div class="empty-state">No clipboard data detected</div>';
return;
}
// Try RTF first (Apple Notes uses RTF)
const rtfData = clipboardData.getData('text/rtf');
let markdown = '';
let html = '';
if (rtfData) {
try {
const result = rtfToMarkdownAndHtml(rtfData);
markdown = result.markdown;
html = result.html;
} catch (error) {
resultsDiv.innerHTML = `<div class="error">Error parsing RTF: ${error.message}</div>`;
return;
}
} else {
// Fall back to plain text
const plainText = clipboardData.getData('text/plain');
if (plainText) {
markdown = plainText;
html = escapeHtml(plainText).replace(/\n/g, '<br>\n');
} else {
resultsDiv.innerHTML = '<div class="error">No supported format detected in clipboard.</div>';
return;
}
}
// Create Markdown section
const markdownSection = document.createElement('div');
markdownSection.className = 'section';
markdownSection.innerHTML = `
<h2>Markdown</h2>
<div class="button-group">
<button id="copyMarkdownBtn">Copy Markdown</button>
</div>
<textarea class="code-output" id="markdownOutput" readonly>${escapeHtml(markdown)}</textarea>
`;
resultsDiv.appendChild(markdownSection);
// Create HTML section
const htmlSection = document.createElement('div');
htmlSection.className = 'section';
htmlSection.innerHTML = `
<h2>HTML</h2>
<div class="button-group">
<button id="copyHtmlBtn">Copy HTML</button>
</div>
<textarea class="code-output" id="htmlOutput" readonly>${escapeHtml(html)}</textarea>
`;
resultsDiv.appendChild(htmlSection);
// Create preview section
const previewSection = document.createElement('div');
previewSection.className = 'section';
previewSection.innerHTML = `
<h2>Preview</h2>
<div class="preview-container" id="preview"></div>
`;
resultsDiv.appendChild(previewSection);
document.getElementById('preview').innerHTML = html;
// Create debug section (collapsed by default)
if (rtfData) {
const debugSection = document.createElement('div');
debugSection.className = 'section';
debugSection.innerHTML = `
<details>
<summary>Show raw RTF (debug)</summary>
<pre class="rtf-debug">${escapeHtml(rtfData)}</pre>
</details>
`;
resultsDiv.appendChild(debugSection);
}
// Set up copy buttons
const copyMarkdownBtn = document.getElementById('copyMarkdownBtn');
const copyHtmlBtn = document.getElementById('copyHtmlBtn');
const markdownOutput = document.getElementById('markdownOutput');
const htmlOutput = document.getElementById('htmlOutput');
copyMarkdownBtn.addEventListener('click', async () => {
try {
await navigator.clipboard.writeText(markdownOutput.value);
copyMarkdownBtn.textContent = 'Copied!';
copyMarkdownBtn.classList.add('copied');
setTimeout(() => {
copyMarkdownBtn.textContent = 'Copy Markdown';
copyMarkdownBtn.classList.remove('copied');
}, 2000);
} catch (err) {
console.error('Failed to copy:', err);
}
});
copyHtmlBtn.addEventListener('click', async () => {
try {
await navigator.clipboard.writeText(htmlOutput.value);
copyHtmlBtn.textContent = 'Copied!';
copyHtmlBtn.classList.add('copied');
setTimeout(() => {
copyHtmlBtn.textContent = 'Copy HTML';
copyHtmlBtn.classList.remove('copied');
}, 2000);
} catch (err) {
console.error('Failed to copy:', err);
}
});
});
// Clear the textarea on paste
pasteArea.addEventListener('paste', () => {
setTimeout(() => {
pasteArea.value = '';
}, 0);
});
</script>
</body>
</html>