Add markdown rendering utility for bold, italic, and inline code
This commit is contained in:
parent
f9f87862b0
commit
fa888a9e38
21
src/markdown.ts
Normal file
21
src/markdown.ts
Normal 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
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user