Live Markdown to HTML Preview
A live Markdown editor that renders Comark content to HTML inside a sandboxed iframe preview, with Shiki syntax highlighting.
src/main.ts
import { createHtmlRenderer } from '@comark/html'
import shiki from '@comark/html/plugins/shiki'
const renderHtml = createHtmlRenderer({
plugins: [shiki()],
})
const PREVIEW_STYLES = `
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
font-size: 15px;
line-height: 1.7;
color: #1a1a2e;
max-width: 720px;
margin: 0 auto;
padding: 32px 24px;
}
/* Additional preview styles omitted */
`
async function updatePreview(markdown: string) {
const html = await renderHtml(markdown)
const frame = document.getElementById('preview') as HTMLIFrameElement
frame.srcdoc = `<!doctype html><html><head><style>${PREVIEW_STYLES}</style></head><body>${html}</body></html>`
}
const input = document.getElementById('input') as HTMLTextAreaElement
input.value = SAMPLE
updatePreview(SAMPLE)
let debounceTimer: ReturnType<typeof setTimeout>
input.addEventListener('input', () => {
clearTimeout(debounceTimer)
debounceTimer = setTimeout(() => updatePreview(input.value), 150)
})This example builds a split-pane live preview with @comark/html. Typing Comark Markdown on the left updates a sandboxed <iframe> on the right after a 150 ms debounce. The shiki plugin highlights fenced code blocks, and srcdoc isolates the preview styles from the editor shell.