Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

fix(md-view): render terminal output as readable output, not mangled markdown

reviewbae38f698def
diff --git a/src/renderer/lib/terminalOutputMarkdown.test.ts b/src/renderer/lib/terminalOutputMarkdown.test.ts
new file mode 100644
index 000000000..36701768a
--- /dev/null
+++ b/src/renderer/lib/terminalOutputMarkdown.test.ts
@@ -0,0 +1,72 @@
+// @vitest-environment jsdom
+import { describe, expect, it } from 'vitest'
+import { marked } from 'marked'
+import { renderTerminalOutput } from './terminalOutputMarkdown'
+import { renderMarkdown } from './markdown'
+
+/** Visible text of the rendered HTML, the way a user reads it. */
+function textOf(html: string): string {
+ const el = document.createElement('div')
+ el.innerHTML = html
+ return el.textContent ?? ''
+}
+
+describe('renderTerminalOutput', () => {
+ it('keeps consecutive output lines apart (ls -l must not become one line)', () => {
+ const out = 'total 8\n-rw-r--r-- 1 u u 10 a.txt\n-rw-r--r-- 1 u u 20 b.txt'
+ const html = renderTerminalOutput(out)
+ expect(html.match(/<br>/g)?.length).toBe(2)
+ expect(textOf(html)).toContain('a.txt')
+ })
+
+ it('shows raw-HTML-looking output as text instead of parsing and stripping it', () => {
+ const html = renderTerminalOutput('$ echo <stdin>\n<stdin>')
+ expect(textOf(html)).toMatch(/^\$ echo <stdin>\n?<stdin>\n?$/)
+ expect(html).toContain('&lt;stdin&gt;')
+ })
+
+ it('shows a block of raw HTML as text, lines kept', () => {
+ const html = renderTerminalOutput('<div class="x">\nhello\n</div>')
+ expect(html).not.toMatch(/<div class="x">/)
+ expect(textOf(html)).toContain('<div class="x">')
+ expect(textOf(html)).toContain('</div>')
+ })
+
+ it('never lets an escaped tag execute — script and handlers are text, not markup', () => {
+ const html = renderTerminalOutput('<img src=x onerror=alert(1)> <script>alert(1)</script>')
+ const el = document.createElement('div')
+ el.innerHTML = html
+ expect(el.querySelector('img, script')).toBeNull()
+ expect(el.textContent).toContain('<script>')
+ })
+
+ it('does not double-escape inside code spans and fenced blocks', () => {
+ const html = renderTerminalOutput('run `a <b> c`\n\n```\nx < y && <tag>\n```')
+ expect(textOf(html)).toContain('a <b> c')
+ expect(textOf(html)).toContain('x < y && <tag>')
+ expect(html).not.toContain('&amp;lt;')
+ })
+
+ it('trims the trailing blank lines and trailing spaces capture-pane pads with', () => {
+ const html = renderTerminalOutput('$ ls \nfile \n\n\n \n\n')
+ expect(html).toBe(renderTerminalOutput('$ ls\nfile'))
+ expect(html.trimEnd().endsWith('file</p>')).toBe(true)
+ })
+
+ it('still sanitizes (markdown links with javascript: urls lose their href)', () => {
+ const html = renderTerminalOutput('[x](javascript:alert(1))')
+ expect(html).not.toContain('javascript:')
+ })
+
+ it('renders empty input as empty', () => {
+ expect(renderTerminalOutput('')).toBe('')
+ expect(renderTerminalOutput('\n\n \n')).toBe('')
+ })
+
+ it('does not mutate the shared marked config the other callers use', () => {
+ renderTerminalOutput('a\nb <x>')
+ expect(marked.defaults.breaks).toBe(false)
+ expect(renderMarkdown('a\nb')).toBe('<p>a\nb</p>\n')
+ expect(renderMarkdown('<b>x</b>')).toBe('<p><b>x</b></p>\n')
+ })
+})
diff --git a/src/renderer/lib/terminalOutputMarkdown.ts b/src/renderer/lib/terminalOutputMarkdown.ts
new file mode 100644
index 000000000..571cda726
--- /dev/null
+++ b/src/renderer/lib/terminalOutputMarkdown.ts
@@ -0,0 +1,57 @@
+import { Marked } from 'marked'
+import DOMPurify from 'dompurify'
+
+/**
+ * Render a terminal's captured output (the ⌘M "markdown of output" view) to sanitized HTML.
+ *
+ * `renderMarkdown` is the wrong tool for this, for three measured reasons:
+ * - With `breaks: false`, consecutive output lines join into ONE paragraph — `ls -l` rendered as a
+ * single run-on line. Terminal output is line-oriented, so every newline is kept (`breaks: true`).
+ * - Anything that LOOKS like a tag is parsed as raw HTML, and DOMPurify then strips it:
+ * `echo <stdin>` showed as `echo `. Output is text a program printed, never markup the user
+ * meant, so raw-HTML tokens are rendered as escaped TEXT. This is done on the `html` token only —
+ * NOT by pre-escaping `<` in the source, which would double-escape inside code spans/fences
+ * (marked already escapes those, so `a < b` in backticks would print as `a &lt; b`).
+ * - `tmux capture-pane` pads the capture with trailing blank lines (and lines with trailing
+ * spaces), which rendered as a tall empty tail under the real output.
+ *
+ * A private `Marked` instance, so the global `marked` config that `renderMarkdown`'s callers
+ * (sticky notes, editor preview, ChatPanel) share is never touched. The result still goes through
+ * DOMPurify: escaping raw HTML is about readability, sanitizing is about safety — markdown links
+ * (`[x](javascript:…)`) still produce real elements that need it.
+ */
+
+function escapeHtml(s: string): string {
+ return s
+ .replace(/&/g, '&amp;')
+ .replace(/</g, '&lt;')
+ .replace(/>/g, '&gt;')
+ .replace(/"/g, '&quot;')
+ .replace(/'/g, '&#39;')
+}
+
+const terminalMarked = new Marked({
+ async: false,
+ breaks: true,
+ gfm: true,
+ renderer: {
+ html(html: string, block?: boolean): string {
+ if (!block) return escapeHtml(html)
+ // A block-level html token swallows every line up to the next blank line; show it as the
+ // paragraph of text it was printed as, its line structure kept like everything else.
+ return `<p>${escapeHtml(html.replace(/\n+$/, '')).replace(/\n/g, '<br>\n')}</p>\n`
+ }
+ }
+})
+
+/** Strip trailing whitespace from every line, then the trailing blank lines capture-pane pads. */
+function trimCapture(text: string): string {
+ return text.replace(/[ \t]+$/gm, '').replace(/\s+$/, '')
+}
+
+export function renderTerminalOutput(text: string): string {
+ const src = trimCapture(text || '')
+ if (!src) return ''
+ const html = terminalMarked.parse(src) as string
+ return DOMPurify.sanitize(html)
+}
diff --git a/src/renderer/nodes/TerminalNode.tsx b/src/renderer/nodes/TerminalNode.tsx
index c97c80be8..16a6611ba 100644
--- a/src/renderer/nodes/TerminalNode.tsx
+++ b/src/renderer/nodes/TerminalNode.tsx
@@ -5314,8 +5314,10 @@ export function TerminalNode({
// startup path (it is what the canvas is made of), the markdown renderer is not — it runs
// only after someone presses ⌘M. The capture is already a round trip to main, so the extra
// chunk fetch is not even on a path the user can perceive.
- void Promise.all([api.pty.capture(id, true), import('../lib/markdown')]).then(
- ([text, md]) => setMdHtml(md.renderMarkdown(text))
+ // `renderTerminalOutput`, not `renderMarkdown`: output keeps its line breaks, and tag-like
+ // text (`echo <stdin>`) shows as text instead of being parsed as HTML and stripped.
+ void Promise.all([api.pty.capture(id, true), import('../lib/terminalOutputMarkdown')]).then(
+ ([text, md]) => setMdHtml(md.renderTerminalOutput(text))
)
}
}, [data.mdMode, id, useChat])