elements
Blog Demos UI Docs Pricing
Elements Manual
  • Getting Started
  • CLI
  • Build
  • Browser
  • Packages
  • TypeScript
  • Elements HTML
  • Assets
  • RPC
  • Router
  • Session
  • Database
  • Migrations
  • LiveTable
  • Channel
  • Jobs
  • Email
  • Payments
  • JSON
  • Async
  • Style
  • UI
  • JSOC
  • Config
  • Tests
  • Logs
  • Deploy
  • Editors
  • Recipes
    • Todo App
    • Todo App: Markup and Styles
    • Authentication
    • Form Validation
    • Signup with Email Confirmation
    • Signup Wizard
    • Admin Roles
    • File Upload
    • Serving Assets From the Database
    • Live Comments
    • Likes / Votes Toggle
    • Shared Lists
    • Team-Partitioned Table
    • Chat Rooms
    • Typing Indicator
    • Grouped Feed Without Flicker
    • Unread Counts
    • Room Presence
    • Collaborative Canvas
    • Infinite Scroll Feed
    • Bidirectional Partition
    • Search and Filter
    • Admin Table
    • Markdown Blog
    • Public and Admin Views
    • Cron Cache
    • Live Dashboard
    • Live Updates From Any Write
    • Marketing Page
    • Status Indicators
    • Markdown Editor
    • Time Ago
  • Licenses
Manual / Recipes/ Markdown Editor

Markdown Editor

elements man recipes/render-markdown

An in-page markdown editor with a live preview. As the user types in a Textarea, the preview re-renders on every keystroke through the template's reactive binding. The parser is marked, installed via elements install marked; the recipe is the wiring around it, not the parser itself.

Install

Terminal
elements install marked

elements install adds the dependency to package.json and the project server rebuilds against it on the next save.

Page setup

Terminal
elements create page markdown

The page is a single-template editor: Textarea on the left, rendered preview on the right. No RPC, no database. marked.parse runs in the browser on each keystroke.

app/pages/markdown/index.ts
import { Request, Response } from "@elements/app"; import markdown from "./template"; export default function route(req: Request, res: Response) { return new markdown(); }

The route does nothing but render. All the interactivity is in the template.

app/pages/markdown/template.ehtml
import "./style.css"; import { raw } from "@elements/app"; import { marked } from "marked"; import { Textarea } from "@elements/ui/textarea"; const INITIAL = `# hello, markdown write *some* **bold** text. add a [link](https://elements.dev) or two. - bullet one - bullet two - bullet three \`\`\` let x = 42; console.log(x); \`\`\` `; <html class="page-markdown" ( private text: { value: string } = { value: INITIAL }, )> <main class="page-shell"> <h1>markdown editor</h1> <div class="split"> <Textarea class="source" value={text.value} rows={20} autosize={false} resize="none" attrs={{ "aria-label": "markdown source" }} /> <div class="preview">{raw(marked.parse(text.value) as string)}</div> </div> </main> </html>

text.value is the Textarea's bound value. autosize={false} holds it at twenty rows so the two panes stay the same height. Each keystroke updates text.value, the binding re-runs, marked.parse(...) renders the new markdown, and {raw(...)} writes the resulting HTML into the preview pane. marked.parse is sync by default and returns string; the cast pins the return type for TypeScript.

{raw(html)} is the elements helper for rendering an HTML string without escaping. The trust boundary is marked: by default it escapes HTML in the input, so its output is safe to pass to raw(). If you configure marked to allow raw HTML pass-through in the source, sanitize the output with DOMPurify (or equivalent) before raw().

app/pages/markdown/style.css
@import "#app/shared/styles/page.css"; .page-markdown .split { display: grid; grid-template-columns: 1fr 1fr; gap: var(--space-6); align-items: stretch; } .page-markdown .source { font-family: var(--font-mono); font-size: var(--text-sm); } .page-markdown .preview { padding: var(--space-4); border: 1px solid var(--rule); border-radius: var(--radius-md); background: var(--bg); overflow-y: auto; } .page-markdown .preview h1, .page-markdown .preview h2, .page-markdown .preview h3 { margin-top: var(--space-6); } .page-markdown .preview h1:first-child, .page-markdown .preview h2:first-child { margin-top: 0; } .page-markdown .preview pre { background: var(--bg-soft); padding: var(--space-3); border-radius: var(--radius-md); overflow-x: auto; } .page-markdown .preview code { font-family: var(--font-mono); font-size: var(--text-sm); }

The preview is a side-by-side panel matching the Textarea's height. The Textarea brings its own chrome; .source only switches it to the monospace face. The preview's rules pick up the design-system tokens (--bg, --bg-soft, --rule, --space-*) so it matches the rest of the app's look.

Routes

Register the page in index.ts:

index.ts
import markdown from "#app/pages/markdown"; // ... app.route("/markdown", markdown);

Notes

  • Browser-side rendering. marked.parse runs on every keystroke. For typical document sizes (under ~50 KB) it finishes inside a frame; the preview keeps up with typing. For very large documents, debounce the binding by piping text.value through a setTimeout-driven preview.value and reading preview.value in the raw(...) call.
  • Server-side rendering. For static content where the user doesn't edit (a blog post, a docs page), render in the route handler and pass the HTML string to the template: let html = marked.parse(body) as string; return new page({ html });. The first paint shows the rendered output and the parser never runs on the browser. See elements man recipes markdown-blog for that shape.
  • Sanitization. Default marked escapes HTML in the input, so a user typing <script> into the textarea ends up as literal text in the preview, not as executed JavaScript. If you turn that off (rare, but marked supports raw HTML), run the output through DOMPurify before raw(...).
  • Saving the document. The recipe is a pure editor; the markdown is held in template state and lost on navigation. Persist by adding a save @rpc that writes text.value to a database row, and call it from a "save" button or on blur.
  • Extending the parser. marked supports custom renderers, code highlighting integration, and extensions for non-standard syntax (callouts, math, mermaid). All configured through marked.use(...) once on app startup; the recipe's binding stays the same.
Prev Status IndicatorsNext Time Ago
Product
Manual Pricing Compare
Company
History Contact
Legal
Privacy Policy Acceptable Use Terms License Agreement
© Elements Dev Corporation 2026