feat: Remove padding from markdown code blocks using NoInsetMarkdown

This commit is contained in:
Paul Gauthier (aider) 2025-03-17 21:10:12 -07:00
parent feb277049f
commit 7316635aba

View file

@ -5,7 +5,8 @@ import time
from rich.console import Console
from rich.live import Live
from rich.markdown import Markdown
from rich.markdown import Markdown, CodeBlock
from rich.syntax import Syntax
from rich.text import Text
from aider.dump import dump # noqa: F401
@ -46,6 +47,27 @@ The end.
""" # noqa: E501
class NoInsetCodeBlock(CodeBlock):
"""A code block with syntax highlighting and no padding."""
def __rich_console__(self, console, options):
code = str(self.text).rstrip()
syntax = Syntax(
code, self.lexer_name, theme=self.theme, word_wrap=True, padding=0
)
yield syntax
class NoInsetMarkdown(Markdown):
"""Markdown with code blocks that have no padding."""
@classmethod
def make_elements(cls):
elements = super().make_elements()
elements["code_block"] = NoInsetCodeBlock
return elements
class MarkdownStream:
"""Streaming markdown renderer that progressively displays content with a live updating window.
@ -88,7 +110,7 @@ class MarkdownStream:
# Render the markdown to a string buffer
string_io = io.StringIO()
console = Console(file=string_io, force_terminal=True)
markdown = Markdown(text, **self.mdargs)
markdown = NoInsetMarkdown(text, **self.mdargs)
console.print(markdown)
output = string_io.getvalue()
@ -186,6 +208,7 @@ if __name__ == "__main__":
_text = _text * 10
pm = MarkdownStream()
print("Using NoInsetMarkdown for code blocks with padding=0")
for i in range(6, len(_text), 5):
pm.update(_text[:i])
time.sleep(0.01)