${currentHTML}`); doc.close(); // Update stats updateStats(md); } function updateStats(md) { const chars = md.length; const words = md.trim() ? md.trim().split(/\s+/).length : 0; const lines = md.split('\n').length; const htmlBytes = new Blob([currentHTML]).size; document.getElementById('stat-chars').textContent = chars.toLocaleString(); document.getElementById('stat-words').textContent = words.toLocaleString(); document.getElementById('stat-lines').textContent = lines.toLocaleString(); document.getElementById('stat-html-size').textContent = htmlBytes < 1024 ? htmlBytes + ' B' : (htmlBytes / 1024).toFixed(1) + ' KB'; } // Default content const DEFAULT_MD = `# Welcome to Markdown to HTML Converter Convert **Markdown** to clean HTML instantly. No upload, no server — everything runs in your browser. ## Features - Live preview as you type - Copy or download the HTML output - Supports GitHub-Flavored Markdown (GFM) - Tables, code blocks, blockquotes, and more ## Example Code \`\`\`javascript function greet(name) { return \`Hello, \${name}!\`; } \`\`\` ## Table | Name | Type | Description | |--------|---------|----------------------| | input | string | Markdown source text | | output | string | HTML string | > **Tip:** Switch to **HTML** tab to copy the raw HTML, or **Preview** for full-page view. --- *Start typing to replace this example.* `; const mdInput = document.getElementById('md-input'); mdInput.value = DEFAULT_MD; convert(DEFAULT_MD); mdInput.addEventListener('input', () => { convert(mdInput.value); }); // File input document.getElementById('file-input').addEventListener('change', (e) => { const file = e.target.files[0]; if (!file) return; const reader = new FileReader(); reader.onload = (ev) => { mdInput.value = ev.target.result; convert(mdInput.value); incrementUsage(); trackEvent('file_process', 'md'); }; reader.readAsText(file); }); // Copy HTML function copyHTML() { if (!currentHTML) return; navigator.clipboard.writeText(currentHTML).then(() => { showNotif('HTML copied!'); trackEvent('copy_click', 'md-html-038'); }).catch(() => { const ta = document.createElement('textarea'); ta.value = currentHTML; document.body.appendChild(ta); ta.select(); document.execCommand('copy'); document.body.removeChild(ta); showNotif('HTML copied!'); trackEvent('copy_click', 'md-html-038'); }); incrementUsage(); } // Download HTML function downloadHTML() { if (!currentHTML) return; const full = `\n\n\n\n\nDocument\n\n\n${currentHTML}\n\n`; const blob = new Blob([full], { type: 'text/html' }); const a = document.createElement('a'); a.href = URL.createObjectURL(blob); a.download = 'output.html'; a.click(); URL.revokeObjectURL(a.href); incrementUsage(); trackEvent('file_process', 'html-download'); } // Clear function clearAll() { mdInput.value = ''; convert(''); } // Notification function showNotif(msg) { const el = document.getElementById('notif'); el.textContent = msg; el.classList.add('show'); setTimeout(() => el.classList.remove('show'), 2000); }