Skip to content

ui: use a live-preview markdown editor for the chat form input - #31955

Merged
allozaur merged 14 commits into
ggml-org:masterfrom
allozaur:allozaur/feat/live-preview-editor
Nov 10, 2026
Merged

allozaur merged 14 commits into
ggml-org:masterfrom
allozaur:allozaur/feat/live-preview-editor

Conversation

@allozaur

@allozaur allozaur commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

Overview

The chat form's input is a live-preview markdown editor: what you write renders as you write it, and the line you are on shows its markdown.

Demo

Demo

Recorded at a446fa0; click the preview for the full video.

Additional information

Stacked on #31949: until that merges, this diff also shows its commits. This PR's own commits are the last six, starting at "ui: use a live-preview markdown editor for the chat form input".

Supersedes #31363, the same change on an older base.

What changed
  • The chat form input is a CodeMirror editor with a live preview: every block the cursor is not in shows the HTML that MarkdownContent renders for it (same pipeline and stylesheet, code blocks included), and the block being edited shows its raw text. The text itself is never rewritten, so what is sent is exactly what was typed.
  • One MarkdownEditor component replaces both the plain textarea and the rich contenteditable input, along with the rich input's tokenizer and its own undo history. File mentions are atomic inline badges in the block being edited.
  • Shift+Enter is a line break that continues lists and quotes; Enter keeps its send behaviour, except inside a code fence, where it adds a line.
  • The live preview keeps its rendered blocks while only the cursor moves, so moving through a long prompt does not render it again.
  • The editor has an accessible name, its placeholder by default, so the chat form input is announced as "Type a message..." again.
  • The production build gets a 6 GB heap, as CodeMirror and the single-bundle build ran out of the default one. A note in svelte.config.js says what mdsvex stays for: content pages such as release notes.
Testing
  • Checked in the browser on this branch: a heading and emphasis render as soon as the cursor leaves their line, Shift+Enter continues a list, a code fence renders as a highlighted block with its copy button, and clicking a rendered block shows its markdown again for editing.
  • The chat form input is found by its name, "Type a message...", as the demo harness and screen readers look for it.
  • npm run check and prettier --check pass on this branch; the unit and client suites run in CI.
Demo script
// types where the editor's cursor already is: a click would land inside a rendered block
async function keys(page, text) {
	for (const char of text) {
		await page.keyboard.type(char);
		await page.waitForTimeout(/[\s.,:]/.test(char) ? 70 : 35);
	}
}

// a code fence, spelled out: the PR body ends this script at the first literal one
const FENCE = '`'.repeat(3);

// an audio model gets a microphone button in place of Send while the input is empty
test.use({ settings: { autoMicOnEmpty: false } });

test('demo', async ({ demo, page }) => {
	const editor = page.getByRole('textbox', { name: 'Type a message...' });
	const send = page.getByRole('button', { name: 'Send' });

	// a small model may answer with a tool call that waits for a permission: send no tools
	await page.route('**/v1/chat/completions', async (route) => {
		const body = JSON.parse(route.request().postData() ?? '{}');

		delete body.tools;
		await route.continue({ postData: JSON.stringify(body) });
	});

	await demo.caption('The chat form is a live-preview markdown editor');
	await demo.type(editor, '## Plan for the release');
	await demo.press('Shift+Enter');

	await demo.caption('A line renders once the cursor leaves it; the one being typed stays raw');
	await keys(page, 'Keep it **short** and _friendly_:');
	await demo.press('Shift+Enter');

	await demo.caption('Lists carry on by themselves');
	await keys(page, '- list the fixes');
	await demo.press('Shift+Enter');
	await keys(page, 'thank the testers');
	await demo.press('Shift+Enter');
	await demo.press('Shift+Enter');

	await demo.caption('Code blocks render as they do in a reply');
	await keys(page, `${FENCE}js`);
	await demo.press('Shift+Enter', 300);
	await keys(page, "const version = '1.2.0';");
	await demo.press('Shift+Enter', 300);
	await keys(page, FENCE);
	await demo.press('Shift+Enter');
	await keys(page, 'Does this read well?');
	await demo.pause(1200);

	await demo.caption('Click a block to edit its markdown again');
	await demo.click(editor.getByText('Plan for the release'));
	await demo.pause(2000);

	await demo.caption('What is sent is the markdown, exactly as typed');
	await page.keyboard.press('ControlOrMeta+End');
	await demo.press('Enter', 1500);
	await expect(send).toBeVisible({ timeout: 60000 });
	await demo.pause(2500);
});

Requirements

@github-actions github-actions Bot added video demo PR features a video demo recorded with the UI demo pipeline server/ui labels Nov 9, 2026
Replace the contenteditable rich input with a CodeMirror editor that shows
the markdown source of the block being edited and renders every other block
through the MarkdownContent pipeline and stylesheet, so they match chat
messages. Plain text types exactly as before, the value stays the exact
markdown source, and mention links show as atomic badges.

The editor lives in a shared MarkdownEditor component, and the highlight.js
theme loader is shared between MarkdownContent and SyntaxHighlightedCode.
The old rich input, its tokenizer, source history and their tests go away.

Assisted-by: Claude (Opus 5.5 Medium)
The single-bundle build now includes CodeMirror and runs past Node's default
heap limit on CI runners. Raise it in the build script, so local and CI builds
behave the same on every platform.

Assisted-by: Claude (Opus 5.5 Medium)
Moving the cursor re-walked the syntax tree and rebuilt every block's
decoration. Keep the blocks with the decorations and rebuild only when the
text changes or the cursor enters another block.

Assisted-by: Claude (Opus 5.5 Medium)
…ceholder until the design system lands

Assisted-by: Claude (Opus 5.5 Medium)
CodeMirror's content is a textbox with no name of its own, so the chat form
input was announced as an unnamed text box, and could not be found by its name
as the old input could. It takes an aria-label, its placeholder by default,
which makes the chat form's "Type a message..." again.

Assisted-by: Claude (Opus 5.5 Medium)
Assisted-by: Claude (Opus 5.5 Medium)
…d the editor

The editor and MarkdownContent each @imported markdown-content.css into their own
<style>, so the bundle carried two scoped copies of it. Both now render into a
MarkdownSurface, the one element that carries the stylesheet; the few rules for
MarkdownContent's own template elements are global under .markdown-content.

Drawing mermaid and svg blocks and the actions of their header buttons move to
markdown-diagrams.ts, so the editor's rendered blocks can draw diagrams the same
way messages do.

Assisted-by: Claude (Opus 5.5 Medium)
- onChange skips only the editor's own syncs from `value`, marked by an
  annotation, instead of reporting only edits that carry a user event
- Enter and the mention badges ask the syntax tree whether they are in code, so
  ~~~ fences and indented code count, and a closing fence ends a block
- a picked mention is one undoable edit, through replaceText
- rendered blocks draw mermaid and svg diagrams with their header buttons, a $$
  math block with blank lines renders as one block, and a link reference
  definition stays raw instead of rendering to nothing
- rendered blocks render again when the mention path settings change
- a drag in a rendered block selects its text and copy takes it, across blocks;
  a click still opens the block
- the block margins are the ones a chat message renders (each heading is a first
  child there), checked by a test; the blank lines after a raw block are its gap
- the tree comes from syntaxTree, without forcing a full parse on every edit
- placeholder, label, extensions, mention widgets and render key follow prop
  changes after mount, through compartments
- the editor uses the markdown language alone, which drops the html, css and
  javascript parsers markdown() pulls in for html tag completion (-70 KB gzip)

Assisted-by: Claude (Opus 5.5 Medium)
The new dependencies are pinned like the rest of package.json. The build heap
moves from a node call on vite's bin path to npm's node-options, so the build
script is plain `vite build` again and the setting reaches it on every platform.

Assisted-by: Claude (Opus 5.5 Medium)
An edit used to build every block's decorations and widgets again, which the
editor then compared one by one. The previous decorations, and the mention badges
of raw blocks, now move through the edit, and only the blocks whose range, text,
raw or rendered state, or gap changed get new ones; the texts of the blocks an
edit left alone are reused instead of sliced again. A test drives random edits,
cursor moves and focus changes and checks each result against a full build.

Rendered blocks report the height they were last measured at, or a guess from
their text, so CodeMirror's height map for blocks off screen in a long document
stays close and scrolling stays steady.

Assisted-by: Claude (Opus 5.5 Medium)
- syncing `value` compares it with the text the editor last reported or synced, so
  a keystroke coming back through onChange no longer serializes the document again
- the prop effects reconfigure the editor only when a prop changed, not once more
  right after it is created (which rebuilt the whole preview)
- loadHighlightTheme leaves the theme's style element alone while the mode stays,
  so a message or an editor mounting no longer restyles the whole page

Tests check that typing in one block leaves the other rendered blocks without a
single DOM mutation, and that mounting reconfigures nothing.

Assisted-by: Claude (Opus 5.5 Medium)
Rebuilding the decorations of every block on an edit measures the same as
patching only the changed ones (0.09 ms against 0.11 ms per keystroke at
200 blocks, 0.9 ms against 1.1 ms at 2000), and equal widgets keep their
DOM either way, so the incremental update and its test go.

- markdown-blocks.ts collects the document's blocks, markdown-block-widget.ts
  renders one, markdown-live-preview.ts decides which ones show raw text
- one bounded cache holds a block's HTML and its measured height
- the render key is a plain option: a new one reconfigures the preview,
  which keeps the editor's focus
- MarkdownEditor follows its props with one helper instead of five effects

Assisted-by: Claude (Opus 5.5 Medium)
@allozaur
allozaur marked this pull request as ready for review November 10, 2026 07:12
@allozaur
allozaur requested a review from a team as a code owner November 10, 2026 07:12
Copilot AI balanced review requested due to automatic review settings November 10, 2026 07:12

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@ServeurpersoCom ServeurpersoCom left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

opt-in (OK to enable by default) and LGTM

`test` over `---` is a setext heading, so the preview rendered it as an h2
and the underline vanished once the cursor left the block. The editor now
shows a setext heading's text as typed at all times, like a link
reference definition.

Assisted-by: Claude (Opus 5.5 Medium)
"Live markdown preview in the message input" (General) is on by default.
Off, the input shows the plain markdown text, with file mention badges
still in place; switching it takes effect without a reload.

Assisted-by: Claude (Opus 5.5 Medium)
@allozaur
allozaur merged commit ac00705 into ggml-org:master Nov 10, 2026
10 checks passed
allozaur added a commit to allozaur/llama.cpp that referenced this pull request Nov 12, 2026
Replayed onto the ui/ layout; its first commit (the live-preview editor) is upstream as ggml-org#31955.
allozaur added a commit to allozaur/llama.cpp that referenced this pull request Nov 16, 2026
Replayed onto the ui/ layout; its first commit (the live-preview editor) is upstream as ggml-org#31955.

(cherry picked from commit 8552e13)
@erusev

erusev commented Nov 16, 2026

Copy link
Copy Markdown
Member

I think this is premature. A live preview is nice, but the input is now a code editor, and it brings code-editor behavior we have to find and undo one piece at a time. Two I found today:

  • with a prompt that has a few blocks (a heading, a list, a code block), the second down arrow from the first line jumps to the last line
  • spellcheck and autocorrect are off (CodeMirror's defaults)

A textarea gets both right for free. Is the editor worth that at this stage? I'd go back to a textarea for now and revisit once there's a clear need -- happy to open the PR.

@allozaur

Copy link
Copy Markdown
Contributor Author

I think this is premature. A live preview is nice, but the input is now a code editor, and it brings code-editor behavior we have to find and undo one piece at a time. Two I found today:

* with a prompt that has a few blocks (a heading, a list, a code block), the second down arrow from the first line jumps to the last line

* spellcheck and autocorrect are off (CodeMirror's defaults)

A textarea gets both right for free. Is the editor worth that at this stage? I'd go back to a textarea for now and revisit once there's a clear need -- happy to open the PR.

let's make this an opt-in setting which is disabled by default then

@erusev

erusev commented Nov 17, 2026

Copy link
Copy Markdown
Member

Here it is: #32469. It turns the live preview off by default, and turns spellcheck and autocorrect back on in the input.

allozaur pushed a commit that referenced this pull request Nov 17, 2026
As discussed in #31955, the live preview is now opt-in. With it on, the editor renders blocks as widgets, and moving the cursor down from the first line of a prompt with a few blocks (a heading, a list, a code block) jumps to the last line; with it off, the cursor moves one line at a time. Users with a saved config keep their current value, since the full config is stored in localStorage.

The message input also gets spellcheck, autocorrect and autocapitalize back. CodeMirror turns them off by default, so they were off with the preview on or off; the former textarea and contenteditable inputs had the browser's defaults.
Annieren pushed a commit to Annieren/llama.cpp-upstream that referenced this pull request Nov 23, 2026
As discussed in ggml-org#31955, the live preview is now opt-in. With it on, the editor renders blocks as widgets, and moving the cursor down from the first line of a prompt with a few blocks (a heading, a list, a code block) jumps to the last line; with it off, the cursor moves one line at a time. Users with a saved config keep their current value, since the full config is stored in localStorage.

The message input also gets spellcheck, autocorrect and autocapitalize back. CodeMirror turns them off by default, so they were off with the preview on or off; the former textarea and contenteditable inputs had the browser's defaults.
Nindaleth pushed a commit to Nindaleth/llama.cpp that referenced this pull request Nov 26, 2026
As discussed in ggml-org#31955, the live preview is now opt-in. With it on, the editor renders blocks as widgets, and moving the cursor down from the first line of a prompt with a few blocks (a heading, a list, a code block) jumps to the last line; with it off, the cursor moves one line at a time. Users with a saved config keep their current value, since the full config is stored in localStorage.

The message input also gets spellcheck, autocorrect and autocapitalize back. CodeMirror turns them off by default, so they were off with the preview on or off; the former textarea and contenteditable inputs had the browser's defaults.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

server/ui video demo PR features a video demo recorded with the UI demo pipeline

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants