From 3e423a3b376042591592512e7f91f932e44008da Mon Sep 17 00:00:00 2001 From: Chris Wanstrath Date: Fri, 20 Feb 2026 08:45:28 -0800 Subject: [PATCH] Strip code fences from markdown rendering in terminal output --- src/markdown.ts | 5 ++++- src/test-markdown.ts | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/markdown.ts b/src/markdown.ts index a0a73f2..f3e7114 100644 --- a/src/markdown.ts +++ b/src/markdown.ts @@ -1,7 +1,10 @@ export function renderMarkdown(text: string): string { + // Strip code fences (``` and ```language) — we're already in a terminal + let result = text.replace(/^```\w*\n?/gm, "") + // Extract code spans first so their contents aren't processed as bold/italic const codeSpans: string[] = [] - let result = text.replace(/`([^`]+)`/g, (_, code) => { + result = result.replace(/`([^`]+)`/g, (_, code) => { codeSpans.push(code) return `\x00CODE${codeSpans.length - 1}\x00` }) diff --git a/src/test-markdown.ts b/src/test-markdown.ts index b505ff6..c8ef41a 100644 --- a/src/test-markdown.ts +++ b/src/test-markdown.ts @@ -16,3 +16,19 @@ for (const line of lines) { console.log(` ${renderMarkdown(line)}`) console.log() } + +// Code fence tests +console.log("--- Code fence tests ---") +const fenceTests = [ + "Here is some code:\n```typescript\nconst x = 1\n```\nDone.", + "```\nhello\n```", + "```js\nconsole.log('hi')\n```", +] + +for (const test of fenceTests) { + console.log("INPUT:") + console.log(test) + console.log("OUTPUT:") + console.log(renderMarkdown(test)) + console.log() +}