File size: 1,215 Bytes
d06fa57 8956871 d06fa57 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | # Copyright (c) 2024-present, Yumeow. Licensed under the MIT License.
import re
from rich.console import Console
from rich.markdown import Markdown
from rich.syntax import Syntax
def render_markdown(text: str, width=120, theme="staroffice") -> str:
"""Render Markdown text to an ANSI-styled terminal string using Rich.
Args:
text: Markdown source string.
width: Terminal wrapping width in columns.
theme: Syntax highlighting color theme for fenced code blocks.
Returns:
str: ANSI-formatted string ready for terminal display.
"""
console = Console(record=True, width=width)
with console.capture() as capture:
matches = list(re.finditer(r"```(\w+)?\n(.*?)\n```", text, re.DOTALL))
current = 0
for match in matches:
lang, code = match.groups()
start, end = match.span()
md = Markdown(text[current:start])
code = Syntax(code, lang or "text", word_wrap=True, line_numbers=True, theme=theme)
console.print(md)
console.print(code)
current = end
if current < len(text):
console.print(Markdown(text[current:]))
return capture.get()
|