Add markdown rendering utility for bold, italic, and inline code

This commit is contained in:
Chris Wanstrath 2026-02-19 20:09:08 -08:00
parent f9f87862b0
commit fa888a9e38

21
src/markdown.ts Normal file
View File

@ -0,0 +1,21 @@
export function renderMarkdown(text: string): string {
// Extract code spans first so their contents aren't processed as bold/italic
const codeSpans: string[] = []
let result = text.replace(/`([^`]+)`/g, (_, code) => {
codeSpans.push(code)
return `\x00CODE${codeSpans.length - 1}\x00`
})
// Bold: **text**
result = result.replace(/\*\*(.+?)\*\*/g, "\x1b[1m$1\x1b[22m")
// Italic: *text*
result = result.replace(/\*(.+?)\*/g, "\x1b[3m$1\x1b[23m")
// Restore code spans as light blue
result = result.replace(/\x00CODE(\d+)\x00/g, (_, i) => {
return `\x1b[94m${codeSpans[parseInt(i)]}\x1b[39m`
})
return result
}