Live Dual-Pane Markdown Editor
Warm-paper split Markdown editor with live parsed preview, focus mode, reading metrics, and Edit/Preview tabs on mobile.
Inspired by HackMD ↗
WHY THIS AESTHETIC IS UNIQUE
Pairs distraction-free monospace authoring with instant typographic preview, interactive checklists, and a Focus mode that dims chrome while transforming into clean full-width tabs on 390px screens.
BEST FOR
TAGS
COLOR PALETTE (CLICK TO COPY HEX)
TYPOGRAPHY
- Display
- Newsreader
- Body
- Inter
- Mono
- IBM Plex Mono
- Scale
- 15px/1.7 monospace authoring pane paired with 16px/1.75 formatted editorial preview and 12px mono status bar.
ARCHITECTURE & MOBILE NOTES
LAYOUT
50/50 split panes with a hairline vertical divider on desktop; segmented Edit / Preview tab switcher on mobile viewports.
INTERACTION
Real-time Markdown parser renders headings, bold, italic, inline/fenced code, blockquotes, lists, and checkboxes on every keystroke, plus formatting toolbar insertions and Focus mode.
MOBILE
Replaces cramped dual columns with a full-width segmented Edit / Preview toggle and thumb-friendly formatting pills so writing stays comfortable on 390px screens.
AI AGENT GUARDRAILS
DO
- Switch split editor/preview panes to a segmented Edit / Preview tab control below 768px.
- Use a monospace font in the raw Markdown pane and editorial typography in the preview pane.
- Show live word count and estimated reading time in a subtle footer bar.
DON'T
- Never render a 50/50 side-by-side editor and preview on a 390px phone.
- Avoid injecting unsanitized raw HTML from user Markdown without escaping.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Folio — Dual-Pane Markdown Editor</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:ital,wght@0,400;0,500;0,600;1,400&family=Newsreader:ital,opsz,wght@0,6..72,400;0,6..72,500;0,6..72,600;1,6..72,400&family=Inter:wght@400;500;600&display=swap" rel="stylesheet">
<style>
:root {
--paper: #fbf9f5;
--paper-muted: #f3efe6;
--paper-elevated: #ffffff;
--ink: #1c1b18;
--ink-secondary: #5c574f;
--ink-faint: #8e887d;
--hairline: #e6e0d4;
--accent: #0066cc;
--accent-soft: rgba(0, 102, 204, 0.1);
--code-bg: #f1ece2;
--font-mono: 'IBM Plex Mono', ui-monospace, SFMono-Regular, Menlo, monospace;
--font-serif: 'Newsreader', Georgia, 'Times New Roman', serif;
--font-sans: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
}
body.dark-paper {
--paper: #171614;
--paper-muted: #201e1b;
--paper-elevated: #25231f;
--ink: #ece8df;
--ink-secondary: #a7a093;
--ink-faint: #736d63;
--hairline: #2f2c27;
--accent: #4da3ff;
--accent-soft: rgba(77, 163, 255, 0.15);
--code-bg: #26231e;
}
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
html, body {
height: 100%;
background: var(--paper);
color: var(--ink);
font-family: var(--font-sans);
-webkit-font-smoothing: antialiased;
overflow-x: hidden;
}
.app {
display: flex;
flex-direction: column;
height: 100dvh;
max-width: 100vw;
overflow: hidden;
}
/* Top Chrome */
.topbar {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
padding: 0 20px;
height: 54px;
border-bottom: 1px solid var(--hairline);
background: var(--paper);
flex-shrink: 0;
transition: opacity 0.25s ease;
}
body.focus-mode .topbar {
opacity: 0.35;
}
body.focus-mode .topbar:hover,
body.focus-mode .topbar:focus-within {
opacity: 1;
}
.doc-meta {
display: flex;
align-items: center;
gap: 10px;
min-width: 0;
}
.brand-mark {
width: 24px;
height: 24px;
border-radius: 5px;
background: var(--ink);
color: var(--paper);
display: grid;
place-items: center;
font-family: var(--font-mono);
font-size: 12px;
font-weight: 600;
flex-shrink: 0;
}
.doc-title-input {
background: transparent;
border: none;
color: var(--ink);
font-family: var(--font-sans);
font-size: 14px;
font-weight: 500;
width: 210px;
max-width: 100%;
padding: 4px 6px;
border-radius: 4px;
text-overflow: ellipsis;
}
.doc-title-input:focus {
outline: none;
background: var(--paper-muted);
}
.saved-dot {
font-size: 12px;
color: var(--ink-faint);
white-space: nowrap;
}
.toolbar-actions {
display: flex;
align-items: center;
gap: 6px;
}
.btn {
appearance: none;
border: 1px solid transparent;
background: transparent;
color: var(--ink-secondary);
font-family: var(--font-sans);
font-size: 13px;
font-weight: 500;
padding: 0 10px;
height: 36px;
border-radius: 6px;
cursor: pointer;
display: inline-flex;
align-items: center;
gap: 6px;
transition: background 0.15s ease, color 0.15s ease, border-color 0.15s ease;
white-space: nowrap;
}
.btn:hover {
background: var(--paper-muted);
color: var(--ink);
}
.btn.active {
background: var(--accent-soft);
color: var(--accent);
border-color: rgba(0, 102, 204, 0.22);
}
.btn-primary {
background: var(--ink);
color: var(--paper);
}
.btn-primary:hover {
background: var(--ink-secondary);
color: var(--paper);
}
/* Formatting Ribbon */
.format-bar {
display: flex;
align-items: center;
justify-content: space-between;
gap: 8px;
padding: 6px 20px;
border-bottom: 1px solid var(--hairline);
background: var(--paper-muted);
flex-shrink: 0;
overflow-x: auto;
scrollbar-width: none;
transition: opacity 0.25s ease;
}
.format-bar::-webkit-scrollbar { display: none; }
body.focus-mode .format-bar {
opacity: 0.25;
}
body.focus-mode .format-bar:hover {
opacity: 1;
}
.format-group {
display: flex;
align-items: center;
gap: 2px;
}
.fmt-btn {
appearance: none;
border: none;
background: transparent;
color: var(--ink-secondary);
font-family: var(--font-mono);
font-size: 12px;
font-weight: 500;
min-width: 34px;
height: 34px;
padding: 0 8px;
border-radius: 5px;
cursor: pointer;
display: inline-flex;
align-items: center;
justify-content: center;
transition: background 0.12s, color 0.12s;
}
.fmt-btn:hover {
background: var(--paper-elevated);
color: var(--ink);
}
.divider-v {
width: 1px;
height: 18px;
background: var(--hairline);
margin: 0 6px;
flex-shrink: 0;
}
/* Mobile Segmented Switcher */
.mobile-tabs {
display: none;
padding: 8px 16px;
background: var(--paper);
border-bottom: 1px solid var(--hairline);
flex-shrink: 0;
}
.segmented {
display: grid;
grid-template-columns: 1fr 1fr;
background: var(--paper-muted);
padding: 3px;
border-radius: 9px;
gap: 4px;
}
.seg-btn {
appearance: none;
border: none;
background: transparent;
color: var(--ink-secondary);
font-family: var(--font-sans);
font-size: 14px;
font-weight: 500;
min-height: 44px;
border-radius: 7px;
cursor: pointer;
transition: background 0.15s, color 0.15s, box-shadow 0.15s;
}
.seg-btn.active {
background: var(--paper-elevated);
color: var(--ink);
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
}
/* Workspace Split */
.workspace {
flex: 1;
display: grid;
grid-template-columns: 1fr 1px 1fr;
min-height: 0;
overflow: hidden;
}
.pane {
height: 100%;
overflow-y: auto;
min-width: 0;
}
.pane-divider {
background: var(--hairline);
width: 1px;
}
.editor-pane {
background: var(--paper);
display: flex;
flex-direction: column;
}
.editor-inner {
flex: 1;
width: 100%;
max-width: 68ch;
margin: 0 auto;
padding: 36px 32px 120px;
display: flex;
flex-direction: column;
}
.editor-textarea {
flex: 1;
width: 100%;
min-height: 100%;
border: none;
outline: none;
resize: none;
background: transparent;
color: var(--ink);
font-family: var(--font-mono);
font-size: 14.5px;
line-height: 1.85;
caret-color: var(--accent);
tab-size: 2;
}
body.focus-mode .editor-textarea {
color: var(--ink);
}
/* Preview Pane */
.preview-pane {
background: var(--paper-elevated);
scroll-behavior: smooth;
}
.preview-inner {
max-width: 66ch;
margin: 0 auto;
padding: 40px 40px 120px;
font-family: var(--font-serif);
font-size: 18px;
line-height: 1.72;
color: var(--ink);
}
.preview-inner .md-block {
transition: opacity 0.25s ease;
}
body.focus-mode .preview-inner .md-block {
opacity: 0.24;
}
body.focus-mode .preview-inner .md-block.is-focused {
opacity: 1;
}
.preview-inner h1 {
font-size: 2.05em;
line-height: 1.18;
font-weight: 600;
letter-spacing: -0.02em;
margin: 0 0 0.65em;
}
.preview-inner h2 {
font-size: 1.42em;
line-height: 1.28;
font-weight: 600;
letter-spacing: -0.015em;
margin: 1.6em 0 0.5em;
padding-bottom: 0.25em;
border-bottom: 1px solid var(--hairline);
}
.preview-inner h3 {
font-size: 1.15em;
font-weight: 600;
margin: 1.3em 0 0.4em;
}
.preview-inner p {
margin: 0 0 1.05em;
}
.preview-inner blockquote {
margin: 1.25em 0;
padding: 4px 0 4px 18px;
border-left: 3px solid var(--accent);
color: var(--ink-secondary);
font-style: italic;
}
.preview-inner ul, .preview-inner ol {
margin: 0 0 1.1em 1.35em;
}
.preview-inner li {
margin-bottom: 0.35em;
}
.preview-inner .task-list {
list-style: none;
margin-left: 0;
}
.preview-inner .task-item {
display: flex;
align-items: baseline;
gap: 10px;
margin-bottom: 0.4em;
}
.preview-inner .task-item input[type="checkbox"] {
accent-color: var(--accent);
width: 15px;
height: 15px;
cursor: pointer;
transform: translateY(2px);
}
.preview-inner .task-item.checked span {
text-decoration: line-through;
color: var(--ink-faint);
}
.preview-inner code {
font-family: var(--font-mono);
font-size: 0.82em;
background: var(--code-bg);
padding: 2px 6px;
border-radius: 4px;
}
.preview-inner pre {
background: var(--code-bg);
border: 1px solid var(--hairline);
border-radius: 8px;
padding: 14px 16px;
margin: 1.2em 0;
overflow-x: auto;
}
.preview-inner pre code {
background: transparent;
padding: 0;
font-size: 13px;
line-height: 1.6;
display: block;
}
.preview-inner hr {
border: none;
border-top: 1px solid var(--hairline);
margin: 1.8em 0;
}
.preview-inner a {
color: var(--accent);
text-underline-offset: 3px;
}
.preview-inner table {
width: 100%;
border-collapse: collapse;
margin: 1.2em 0;
font-family: var(--font-sans);
font-size: 14px;
}
.preview-inner th, .preview-inner td {
border-bottom: 1px solid var(--hairline);
padding: 8px 10px;
text-align: left;
}
.preview-inner th {
font-weight: 600;
color: var(--ink-secondary);
font-size: 12px;
text-transform: uppercase;
letter-spacing: 0.04em;
}
/* Status Footer */
.statusbar {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
padding: 0 20px;
height: 36px;
border-top: 1px solid var(--hairline);
background: var(--paper);
font-family: var(--font-mono);
font-size: 11.5px;
color: var(--ink-faint);
flex-shrink: 0;
}
.stats-group {
display: flex;
align-items: center;
gap: 16px;
flex-wrap: nowrap;
overflow: hidden;
}
.stat-pill strong {
color: var(--ink-secondary);
font-weight: 600;
}
.toast {
position: fixed;
bottom: 48px;
right: 20px;
background: var(--ink);
color: var(--paper);
font-size: 13px;
font-weight: 500;
padding: 9px 14px;
border-radius: 8px;
box-shadow: 0 8px 24px rgba(0, 0, 0, 0.16);
opacity: 0;
transform: translateY(8px);
pointer-events: none;
transition: opacity 0.2s, transform 0.2s;
z-index: 50;
}
.toast.show {
opacity: 1;
transform: translateY(0);
}
/* Responsive Mobile Transformation */
@media (max-width: 820px) {
.topbar {
padding: 0 12px;
height: 56px;
}
.doc-title-input {
width: 135px;
font-size: 14px;
}
.saved-dot, .hide-mobile {
display: none;
}
.btn {
min-height: 44px;
padding: 0 10px;
}
.format-bar {
padding: 4px 12px;
}
.fmt-btn {
min-width: 44px;
min-height: 44px;
}
.mobile-tabs {
display: block;
}
.workspace {
grid-template-columns: 1fr;
}
.pane-divider {
display: none;
}
.workspace[data-mobile-tab="edit"] .preview-pane {
display: none;
}
.workspace[data-mobile-tab="preview"] .editor-pane {
display: none;
}
.editor-inner {
padding: 20px 16px 80px;
}
.preview-inner {
padding: 22px 18px 80px;
font-size: 16.5px;
}
.statusbar {
padding: 0 14px;
font-size: 11px;
}
}
</style>
</head>
<body>
<div class="app">
<header class="topbar">
<div class="doc-meta">
<div class="brand-mark" aria-hidden="true">¶</div>
<input id="docTitle" class="doc-title-input" type="text" value="quiet-software-notes.md" aria-label="Document filename">
<span id="saveStatus" class="saved-dot">Saved locally</span>
</div>
<div class="toolbar-actions">
<button id="focusBtn" class="btn" type="button" title="Toggle Focus Mode (Alt+F)">
<svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="3"/><path d="M3 12h3m12 0h3M12 3v3m0 12v3"/></svg>
<span>Focus</span>
</button>
<button id="themeBtn" class="btn" type="button" aria-label="Toggle dark paper theme">
<svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"/></svg>
<span class="hide-mobile">Ink</span>
</button>
<button id="copyHtmlBtn" class="btn btn-primary" type="button">Copy HTML</button>
</div>
</header>
<div class="format-bar" role="toolbar" aria-label="Markdown formatting">
<div class="format-group">
<button class="fmt-btn" type="button" data-action="h2" title="Heading 2">H2</button>
<button class="fmt-btn" type="button" data-action="bold" title="Bold"><strong>B</strong></button>
<button class="fmt-btn" type="button" data-action="italic" title="Italic"><em>I</em></button>
<button class="fmt-btn" type="button" data-action="code" title="Inline Code"></></button>
<div class="divider-v"></div>
<button class="fmt-btn" type="button" data-action="quote" title="Blockquote">“”</button>
<button class="fmt-btn" type="button" data-action="task" title="Task item">☑</button>
<button class="fmt-btn" type="button" data-action="hr" title="Divider">―</button>
</div>
<span class="hide-mobile" style="font-size:12px;color:var(--ink-faint);font-family:var(--font-mono);">
Markdown · Live Sync
</span>
</div>
<div class="mobile-tabs" role="tablist" aria-label="Editor view">
<div class="segmented">
<button id="tabEdit" class="seg-btn active" role="tab" aria-selected="true" type="button">Edit</button>
<button id="tabPreview" class="seg-btn" role="tab" aria-selected="false" type="button">Preview</button>
</div>
</div>
<main id="workspace" class="workspace" data-mobile-tab="edit">
<section class="pane editor-pane" aria-label="Markdown editor">
<div class="editor-inner">
<textarea id="editor" class="editor-textarea" spellcheck="true" aria-label="Markdown source"></textarea>
</div>
</section>
<div class="pane-divider" aria-hidden="true"></div>
<section id="previewPane" class="pane preview-pane" aria-label="Rendered preview">
<article id="preview" class="preview-inner"></article>
</section>
</main>
<footer class="statusbar">
<div class="stats-group">
<span class="stat-pill"><strong id="wordCount">0</strong> words</span>
<span class="stat-pill"><strong id="charCount">0</strong> chars</span>
<span class="stat-pill hide-mobile"><strong id="paraCount">0</strong> paragraphs</span>
<span class="stat-pill"><strong id="readTime">1 min</strong> read</span>
</div>
<span id="cursorPos">Ln 1, Col 1</span>
</footer>
</div>
<div id="toast" class="toast" role="status" aria-live="polite"></div>
<script>
const INITIAL_MD = `# Field Notes on Quiet Software
Writing tools fail most often by asking for attention before the writer has finished a sentence. When chrome recedes, structure becomes legible again.
> Good typography is invisible until it breaks—then it is all the reader can see.
## Principles for Calm Editing
A dual-pane draft works best when **raw syntax** stays crisp in monospace on the left while the rendered cadence breathes in serif on the right.
- Keep line measure between \`60\` and \`72\` characters
- Reserve high-contrast accents strictly for cursor and links
- Dim surrounding paragraphs in *Focus Mode* when drafting
## Revision Checklist
- [x] Verify heading rhythm and vertical spacing
- [x] Add inline code and table support to parser
- [ ] Trim opening paragraph by fifteen words
- [ ] Export clean HTML for the weekend essay
## Reading Cadence Benchmarks
| Mode | Measure | Font Family | Purpose |
| :--- | :--- | :--- | :--- |
| Draft | 66 ch | IBM Plex Mono | Structural clarity |
| Proof | 64 ch | Newsreader | Typographic rhythm |
\`\`\`js
function estimateReadingTime(text) {
const words = text.trim().split(/\\s+/).filter(Boolean).length;
return Math.max(1, Math.ceil(words / 225));
}
\`\`\`
---
Try editing any paragraph on the left, toggling **Focus** mode to spotlight the active block, or clicking a checkbox in the preview.`;
const editor = document.getElementById('editor');
const preview = document.getElementById('preview');
const workspace = document.getElementById('workspace');
const focusBtn = document.getElementById('focusBtn');
const themeBtn = document.getElementById('themeBtn');
const copyHtmlBtn = document.getElementById('copyHtmlBtn');
const tabEdit = document.getElementById('tabEdit');
const tabPreview = document.getElementById('tabPreview');
const saveStatus = document.getElementById('saveStatus');
const toast = document.getElementById('toast');
let toastTimer = null;
function showToast(msg) {
toast.textContent = msg;
toast.classList.add('show');
clearTimeout(toastTimer);
toastTimer = setTimeout(() => toast.classList.remove('show'), 2200);
}
function escapeHtml(str) {
return str
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"');
}
function parseInline(text) {
let s = escapeHtml(text);
s = s.replace(/`([^`]+)`/g, '<code>$1</code>');
s = s.replace(/\*\*([^*]+)\*\*/g, '<strong>$1</strong>');
s = s.replace(/\*([^*]+)\*/g, '<em>$1</em>');
s = s.replace(/\[([^\]]+)\]\((https?:\/\/[^\s)]+)\)/g, '<a href="$2" target="_blank" rel="noopener">$1</a>');
return s;
}
function parseMarkdownBlocks(md) {
const lines = md.replace(/\r\n/g, '\n').split('\n');
const blocks = [];
let i = 0;
while (i < lines.length) {
const line = lines[i];
if (!line.trim()) {
i++;
continue;
}
const startLine = i;
// Fenced code block
if (/^```/.test(line.trim())) {
const lang = line.trim().slice(3).trim();
const codeLines = [];
i++;
while (i < lines.length && !/^```/.test(lines[i].trim())) {
codeLines.push(lines[i]);
i++;
}
const endLine = i;
i++;
blocks.push({
startLine,
endLine,
html: `<pre><code data-lang="${escapeHtml(lang)}">${escapeHtml(codeLines.join('\n'))}</code></pre>`
});
continue;
}
// Heading
const hMatch = line.match(/^(#{1,3})\s+(.*)$/);
if (hMatch) {
const level = hMatch[1].length;
blocks.push({
startLine,
endLine: i,
html: `<h${level}>${parseInline(hMatch[2])}</h${level}>`
});
i++;
continue;
}
// Horizontal rule
if (/^(-{3,}|\*{3,})$/.test(line.trim())) {
blocks.push({ startLine, endLine: i, html: '<hr>' });
i++;
continue;
}
// Blockquote
if (/^>\s?/.test(line)) {
const qLines = [];
while (i < lines.length && /^>\s?/.test(lines[i])) {
qLines.push(lines[i].replace(/^>\s?/, ''));
i++;
}
blocks.push({
startLine,
endLine: i - 1,
html: `<blockquote><p>${parseInline(qLines.join(' '))}</p></blockquote>`
});
continue;
}
// Table
if (line.includes('|') && i + 1 < lines.length && /^\s*\|?[\s:-]+\|[\s|:-]+$/.test(lines[i + 1])) {
const headerCells = line.trim().replace(/^\||\|$/g, '').split('|').map(c => c.trim());
i += 2;
const rows = [];
while (i < lines.length && lines[i].includes('|') && lines[i].trim()) {
rows.push(lines[i].trim().replace(/^\||\|$/g, '').split('|').map(c => c.trim()));
i++;
}
const thead = `<thead><tr>${headerCells.map(c => `<th>${parseInline(c)}</th>`).join('')}</tr></thead>`;
const tbody = `<tbody>${rows.map(r => `<tr>${r.map(c => `<td>${parseInline(c)}</td>`).join('')}</tr>`).join('')}</tbody>`;
blocks.push({
startLine,
endLine: i - 1,
html: `<table>${thead}${tbody}</table>`
});
continue;
}
// Task list
if (/^\s*-\s+\[( |x|X)\]\s+/.test(line)) {
const items = [];
while (i < lines.length && /^\s*-\s+\[( |x|X)\]\s+/.test(lines[i])) {
const m = lines[i].match(/^\s*-\s+\[( |x|X)\]\s+(.*)$/);
const checked = m[1].toLowerCase() === 'x';
items.push(`<li class="task-item ${checked ? 'checked' : ''}"><input type="checkbox" data-line="${i}" ${checked ? 'checked' : ''}><span>${parseInline(m[2])}</span></li>`);
i++;
}
blocks.push({
startLine,
endLine: i - 1,
html: `<ul class="task-list">${items.join('')}</ul>`
});
continue;
}
// Unordered list
if (/^\s*[-*]\s+/.test(line)) {
const items = [];
while (i < lines.length && /^\s*[-*]\s+/.test(lines[i])) {
items.push(`<li>${parseInline(lines[i].replace(/^\s*[-*]\s+/, ''))}</li>`);
i++;
}
blocks.push({
startLine,
endLine: i - 1,
html: `<ul>${items.join('')}</ul>`
});
continue;
}
// Paragraph
const pLines = [];
while (
i < lines.length &&
lines[i].trim() &&
!/^(#{1,3}\s|```|>\s?|[-*]\s+|-{3,}$)/.test(lines[i])
) {
pLines.push(lines[i]);
i++;
}
blocks.push({
startLine,
endLine: i - 1,
html: `<p>${parseInline(pLines.join(' '))}</p>`
});
}
return blocks;
}
function getActiveLineIndex() {
const pos = editor.selectionStart || 0;
return editor.value.slice(0, pos).split('\n').length - 1;
}
function updateCursorAndFocus() {
const pos = editor.selectionStart || 0;
const before = editor.value.slice(0, pos).split('\n');
const ln = before.length;
const col = before[before.length - 1].length + 1;
document.getElementById('cursorPos').textContent = `Ln ${ln}, Col ${col}`;
const activeLine = ln - 1;
const blockEls = preview.querySelectorAll('.md-block');
blockEls.forEach(el => {
const start = Number(el.dataset.start);
const end = Number(el.dataset.end);
el.classList.toggle('is-focused', activeLine >= start && activeLine <= end);
});
}
function render() {
const text = editor.value;
const blocks = parseMarkdownBlocks(text);
preview.innerHTML = blocks
.map(b => `<div class="md-block" data-start="${b.startLine}" data-end="${b.endLine}">${b.html}</div>`)
.join('');
const words = text.trim() ? text.trim().split(/\s+/).filter(Boolean).length : 0;
const chars = text.length;
const paras = text.split(/\n\s*\n/).filter(p => p.trim()).length;
const mins = Math.max(1, Math.ceil(words / 225));
document.getElementById('wordCount').textContent = words.toLocaleString();
document.getElementById('charCount').textContent = chars.toLocaleString();
document.getElementById('paraCount').textContent = paras;
document.getElementById('readTime').textContent = `${mins} min`;
updateCursorAndFocus();
}
// Interactive checkboxes in preview sync back to markdown source
preview.addEventListener('change', (e) => {
if (e.target.matches('input[type="checkbox"][data-line]')) {
const lineIdx = Number(e.target.dataset.line);
const lines = editor.value.split('\n');
if (lines[lineIdx] !== undefined) {
lines[lineIdx] = e.target.checked
? lines[lineIdx].replace(/\[ \]/, '[x]')
: lines[lineIdx].replace(/\[[xX]\]/, '[ ]');
editor.value = lines.join('\n');
render();
showToast(e.target.checked ? 'Marked task complete' : 'Reopened task');
}
}
});
// Formatting toolbar actions
document.querySelectorAll('.fmt-btn[data-action]').forEach(btn => {
btn.addEventListener('click', () => {
const action = btn.dataset.action;
const start = editor.selectionStart;
const end = editor.selectionEnd;
const val = editor.value;
const sel = val.slice(start, end);
let replacement = sel;
let cursorOffset = 0;
if (action === 'bold') {
replacement = `**${sel || 'bold text'}**`;
cursorOffset = sel ? replacement.length : 2;
} else if (action === 'italic') {
replacement = `*${sel || 'italic text'}*`;
cursorOffset = sel ? replacement.length : 1;
} else if (action === 'code') {
replacement = `\`${sel || 'code'}\``;
cursorOffset = sel ? replacement.length : 1;
} else if (action === 'h2') {
replacement = `\n## ${sel || 'Section Heading'}\n`;
cursorOffset = replacement.length - 1;
} else if (action === 'quote') {
replacement = `\n> ${sel || 'Pull quote or reflection.'}\n`;
cursorOffset = replacement.length - 1;
} else if (action === 'task') {
replacement = `\n- [ ] ${sel || 'Follow-up item'}`;
cursorOffset = replacement.length;
} else if (action === 'hr') {
replacement = `\n\n---\n\n`;
cursorOffset = replacement.length;
}
editor.value = val.slice(0, start) + replacement + val.slice(end);
editor.focus();
editor.setSelectionRange(start + cursorOffset, start + cursorOffset);
render();
});
});
// Focus mode toggle
function toggleFocus() {
const on = document.body.classList.toggle('focus-mode');
focusBtn.classList.toggle('active', on);
updateCursorAndFocus();
showToast(on ? 'Focus Mode on — active block spotlighted' : 'Focus Mode off');
}
focusBtn.addEventListener('click', toggleFocus);
// Theme toggle
themeBtn.addEventListener('click', () => {
const dark = document.body.classList.toggle('dark-paper');
themeBtn.classList.toggle('active', dark);
});
// Copy HTML
copyHtmlBtn.addEventListener('click', async () => {
const html = preview.innerHTML;
try {
await navigator.clipboard.writeText(html);
showToast('Rendered HTML copied to clipboard');
} catch {
showToast('Preview HTML ready (' + html.length + ' chars)');
}
});
// Mobile tab switcher
function setMobileTab(tab) {
workspace.dataset.mobileTab = tab;
tabEdit.classList.toggle('active', tab === 'edit');
tabPreview.classList.toggle('active', tab === 'preview');
tabEdit.setAttribute('aria-selected', String(tab === 'edit'));
tabPreview.setAttribute('aria-selected', String(tab === 'preview'));
}
tabEdit.addEventListener('click', () => setMobileTab('edit'));
tabPreview.addEventListener('click', () => setMobileTab('preview'));
// Input & cursor listeners
let saveTimer = null;
editor.addEventListener('input', () => {
render();
saveStatus.textContent = 'Editing…';
clearTimeout(saveTimer);
saveTimer = setTimeout(() => { saveStatus.textContent = 'Saved locally'; }, 550);
});
editor.addEventListener('keyup', updateCursorAndFocus);
editor.addEventListener('click', updateCursorAndFocus);
document.addEventListener('keydown', (e) => {
if (e.altKey && e.key.toLowerCase() === 'f') {
e.preventDefault();
toggleFocus();
}
});
// Init
editor.value = INITIAL_MD;
render();
</script>
</body>
</html>