diff --git a/src/markdown.ts b/src/markdown.ts new file mode 100644 index 0000000..c9eb259 --- /dev/null +++ b/src/markdown.ts @@ -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 +}