-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
398 lines (360 loc) · 16.9 KB
/
index.html
File metadata and controls
398 lines (360 loc) · 16.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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Markdown Viewer</title>
<style>
* { box-sizing: border-box; }
body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; margin: 0; padding: 20px; background: #f5f5f5; }
.container { max-width: 1200px; margin: 0 auto; background: white; border-radius: 8px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); overflow: hidden; }
.toolbar { background: #2c3e50; color: white; padding: 15px 20px; display: flex; align-items: center; gap: 15px; min-height: 60px; }
.file-input { display: none; }
.btn {
background: #f8f9fa;
color: #212529;
border: 1px solid #dee2e6;
padding: 10px 16px;
border-radius: 6px;
cursor: pointer;
font-size: 14px;
font-weight: 500;
display: inline-block;
text-decoration: none;
transition: all 0.2s;
}
.btn:hover {
background: #e9ecef;
border-color: #adb5bd;
}
.theme-toggle {
background: #6c757d;
color: white;
border-color: #6c757d;
margin-left: auto;
min-width: 40px;
display: flex;
align-items: center;
justify-content: center;
}
.theme-toggle:hover {
background: #5c636a;
border-color: #5c636a;
}
.drop-zone { border: 2px dashed #bdc3c7; margin: 20px; padding: 40px; text-align: center; color: #7f8c8d; border-radius: 8px; transition: all 0.3s; }
.drop-zone.drag-over { border-color: #3498db; background: #ecf0f1; }
.content { padding: 20px; line-height: 1.6; color: #2c3e50; }
/* Markdown styles */
.content h1, .content h2, .content h3, .content h4, .content h5, .content h6 { margin: 1.5em 0 0.5em 0; font-weight: 600; line-height: 1.2; }
.content h1 { font-size: 2em; border-bottom: 1px solid #eaecef; padding-bottom: 0.3em; }
.content h2 { font-size: 1.5em; border-bottom: 1px solid #eaecef; padding-bottom: 0.3em; }
.content h3 { font-size: 1.25em; }
.content h4 { font-size: 1em; }
.content h5 { font-size: 0.875em; }
.content h6 { font-size: 0.85em; color: #6a737d; }
.content p { margin: 0 0 1em 0; }
.content blockquote { margin: 0 0 1em 0; padding: 0 1em; color: #6a737d; border-left: 0.25em solid #dfe2e5; }
.content ul, .content ol { margin: 0 0 1em 0; padding-left: 2em; }
.content li { margin: 0.25em 0; }
.content pre { background: #f6f8fa; padding: 16px; border-radius: 6px; overflow-x: auto; margin: 0 0 1em 0; }
.content code { background: #f6f8fa; padding: 0.2em 0.4em; border-radius: 3px; font-size: 85%; font-family: 'SFMono-Regular', Consolas, monospace; }
.content pre code { background: none; padding: 0; }
.content table { border-collapse: collapse; width: 100%; margin: 0 0 1em 0; }
.content th, .content td { border: 1px solid #dfe2e5; padding: 6px 13px; text-align: left; }
.content th { background: #f6f8fa; font-weight: 600; }
.content hr { border: none; height: 0.25em; background: #e1e4e8; margin: 1.5em 0; }
.content a { color: #0366d6; text-decoration: none; }
.content a:hover { text-decoration: underline; }
.content img { max-width: 100%; height: auto; }
.content em { font-style: italic; }
.content strong { font-weight: 600; }
.content del { text-decoration: line-through; }
/* Dark mode styles */
body.dark { background: #0d1117; }
body.dark .container { background: #161b22; }
body.dark .toolbar { background: #21262d; }
body.dark .btn {
background: #343a40;
color: #f8f9fa;
border-color: #495057;
}
body.dark .btn:hover {
background: #495057;
border-color: #6c757d;
}
body.dark .theme-toggle {
background: #6c757d;
color: #f8f9fa;
border-color: #6c757d;
}
body.dark .theme-toggle:hover {
background: #868e96;
border-color: #868e96;
}
body.dark .drop-zone { border-color: #30363d; color: #7d8590; }
body.dark .drop-zone.drag-over { border-color: #58a6ff; background: #21262d; }
body.dark .content { color: #e6edf3; }
body.dark .content h1, body.dark .content h2, body.dark .content h3,
body.dark .content h4, body.dark .content h5, body.dark .content h6 { color: #f0f6fc; }
body.dark .content h1, body.dark .content h2 { border-bottom-color: #21262d; }
body.dark .content h6 { color: #7d8590; }
body.dark .content blockquote { color: #7d8590; border-left-color: #30363d; }
body.dark .content pre { background: #161b22; border: 1px solid #30363d; }
body.dark .content code { background: #6e768166; color: #e6edf3; }
body.dark .content pre code { background: none; }
body.dark .content table { border-color: #30363d; }
body.dark .content th, body.dark .content td { border-color: #30363d; }
body.dark .content th { background: #21262d; color: #f0f6fc; }
body.dark .content hr { background: #30363d; }
body.dark .content a { color: #58a6ff; }
body.dark .content a:hover { color: #79c0ff; }
</style>
</head>
<body>
<div class="container">
<div class="toolbar">
<button class="btn" onclick="document.getElementById('file-input').click()">Choose File</button>
<input type="file" id="file-input" class="file-input" accept=".md,.markdown,.txt" onchange="loadFile(event)">
<span>Drag & drop a markdown file anywhere</span>
<button class="btn theme-toggle" onclick="toggleTheme()" id="theme-toggle">🌙</button>
</div>
<div class="drop-zone" id="drop-zone">
<p>Drop a markdown file here or click "Choose File" to get started</p>
</div>
<div class="content" id="content" style="display: none;"></div>
</div>
<script>
// CommonMark-compliant markdown parser
class MarkdownParser {
constructor() {
this.inlinePatterns = [
{ pattern: /`([^`]+)`/g, replacement: '<code>$1</code>' },
{ pattern: /\*\*\*(.+?)\*\*\*/g, replacement: '<strong><em>$1</em></strong>' },
{ pattern: /\*\*(.+?)\*\*/g, replacement: '<strong>$1</strong>' },
{ pattern: /\*(.+?)\*/g, replacement: '<em>$1</em>' },
{ pattern: /___(.+?)___/g, replacement: '<strong><em>$1</em></strong>' },
{ pattern: /__(.+?)__/g, replacement: '<strong>$1</strong>' },
{ pattern: /_(.+?)_/g, replacement: '<em>$1</em>' },
{ pattern: /~~(.+?)~~/g, replacement: '<del>$1</del>' },
{ pattern: /!\[([^\]]*)\]\(([^)]+)\)/g, replacement: '<img src="$2" alt="$1">' },
{ pattern: /\[([^\]]+)\]\(([^)]+)\)/g, replacement: '<a href="$2">$1</a>' }
];
}
parse(markdown) {
const lines = markdown.split('\n');
let html = '';
let inCodeBlock = false;
let codeBlockLang = '';
let inTable = false;
let tableHeaders = [];
let inList = false;
let listStack = [];
let inBlockquote = false;
for (let i = 0; i < lines.length; i++) {
let line = lines[i];
// Code blocks
if (line.startsWith('```')) {
if (inCodeBlock) {
html += '</code></pre>\n';
inCodeBlock = false;
codeBlockLang = '';
} else {
codeBlockLang = line.slice(3).trim();
html += `<pre><code${codeBlockLang ? ` class="language-${codeBlockLang}"` : ''}>`;
inCodeBlock = true;
}
continue;
}
if (inCodeBlock) {
html += this.escapeHtml(line) + '\n';
continue;
}
// Headers
const headerMatch = line.match(/^(#{1,6})\s+(.+)$/);
if (headerMatch) {
const level = headerMatch[1].length;
const text = this.parseInline(headerMatch[2]);
html += `<h${level}>${text}</h${level}>\n`;
continue;
}
// Horizontal rule
if (/^(\*{3,}|-{3,}|_{3,})$/.test(line.trim())) {
html += '<hr>\n';
continue;
}
// Tables
if (line.includes('|') && line.trim() !== '') {
const cells = line.split('|').map(cell => cell.trim()).filter(cell => cell !== '');
if (!inTable) {
// Check if next line is table separator
if (i + 1 < lines.length && /^[\|\s\-:]+$/.test(lines[i + 1])) {
html += '<table>\n<thead>\n<tr>\n';
cells.forEach(cell => {
html += `<th>${this.parseInline(cell)}</th>\n`;
});
html += '</tr>\n</thead>\n<tbody>\n';
tableHeaders = cells;
inTable = true;
i++; // Skip separator line
continue;
}
} else {
html += '<tr>\n';
cells.forEach(cell => {
html += `<td>${this.parseInline(cell)}</td>\n`;
});
html += '</tr>\n';
continue;
}
} else if (inTable) {
html += '</tbody>\n</table>\n';
inTable = false;
tableHeaders = [];
}
// Lists
const listMatch = line.match(/^(\s*)([*+-]|\d+\.)\s+(.+)$/);
if (listMatch) {
const indent = listMatch[1].length;
const marker = listMatch[2];
const content = this.parseInline(listMatch[3]);
const isOrdered = /^\d+\./.test(marker);
if (!inList) {
html += isOrdered ? '<ol>\n' : '<ul>\n';
listStack.push({ type: isOrdered ? 'ol' : 'ul', indent: indent });
inList = true;
}
html += `<li>${content}</li>\n`;
continue;
} else if (inList && line.trim() === '') {
// Continue list context on empty lines
continue;
} else if (inList) {
// Close all open lists
while (listStack.length > 0) {
const list = listStack.pop();
html += `</${list.type}>\n`;
}
inList = false;
}
// Blockquotes
if (line.startsWith('>')) {
if (!inBlockquote) {
html += '<blockquote>\n';
inBlockquote = true;
}
const content = line.slice(1).trim();
html += `<p>${this.parseInline(content)}</p>\n`;
continue;
} else if (inBlockquote) {
html += '</blockquote>\n';
inBlockquote = false;
}
// Empty lines
if (line.trim() === '') {
if (inList) continue;
html += '\n';
continue;
}
// Regular paragraphs
if (line.trim() !== '') {
html += `<p>${this.parseInline(line)}</p>\n`;
}
}
// Close any remaining open tags
if (inCodeBlock) {
html += '</code></pre>\n';
}
if (inTable) {
html += '</tbody>\n</table>\n';
}
if (inList) {
while (listStack.length > 0) {
const list = listStack.pop();
html += `</${list.type}>\n`;
}
}
if (inBlockquote) {
html += '</blockquote>\n';
}
return html;
}
parseInline(text) {
let result = this.escapeHtml(text);
// Apply inline patterns in order of precedence
this.inlinePatterns.forEach(({ pattern, replacement }) => {
result = result.replace(pattern, replacement);
});
return result;
}
escapeHtml(text) {
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}
}
const parser = new MarkdownParser();
const dropZone = document.getElementById('drop-zone');
const content = document.getElementById('content');
const themeToggle = document.getElementById('theme-toggle');
// Theme management
function toggleTheme() {
const isDark = document.body.classList.toggle('dark');
themeToggle.textContent = isDark ? '☀️' : '🌙';
localStorage.setItem('theme', isDark ? 'dark' : 'light');
}
// Load saved theme on startup
const savedTheme = localStorage.getItem('theme');
if (savedTheme === 'dark' || (!savedTheme && window.matchMedia('(prefers-color-scheme: dark)').matches)) {
document.body.classList.add('dark');
themeToggle.textContent = '☀️';
}
// Drag and drop functionality
['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
document.addEventListener(eventName, preventDefaults, false);
});
function preventDefaults(e) {
e.preventDefault();
e.stopPropagation();
}
['dragenter', 'dragover'].forEach(eventName => {
document.addEventListener(eventName, highlight, false);
});
['dragleave', 'drop'].forEach(eventName => {
document.addEventListener(eventName, unhighlight, false);
});
function highlight(e) {
dropZone.classList.add('drag-over');
}
function unhighlight(e) {
dropZone.classList.remove('drag-over');
}
document.addEventListener('drop', handleDrop, false);
function handleDrop(e) {
const dt = e.dataTransfer;
const files = dt.files;
handleFiles(files);
}
function loadFile(event) {
const files = event.target.files;
handleFiles(files);
}
function handleFiles(files) {
if (files.length === 0) return;
const file = files[0];
if (!file.name.match(/\.(md|markdown|txt)$/i)) {
alert('Please select a markdown file (.md, .markdown, or .txt)');
return;
}
const reader = new FileReader();
reader.onload = function(e) {
const markdown = e.target.result;
const html = parser.parse(markdown);
content.innerHTML = html;
content.style.display = 'block';
dropZone.style.display = 'none';
};
reader.readAsText(file);
}
</script>
</body>
</html>