Skip to content

Commit 487ba88

Browse files
committed
fix centering of headers in Markdown
1 parent 765e95e commit 487ba88

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

cycode/cli/console.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import os
2-
from typing import Optional
2+
from typing import TYPE_CHECKING, Optional
33

4-
from rich.console import Console
4+
from rich.console import Console, RenderResult
5+
from rich.markdown import Heading, Markdown
6+
from rich.text import Text
7+
8+
if TYPE_CHECKING:
9+
from rich.console import ConsoleOptions
510

611
console_out = Console()
712
console_err = Console(stderr=True)
@@ -45,3 +50,20 @@ def is_dark_console() -> Optional[bool]:
4550

4651
# when we could not detect it, use dark theme as most terminals are dark
4752
_SYNTAX_HIGHLIGHT_THEME = _SYNTAX_HIGHLIGHT_LIGHT_THEME if is_dark_console() is False else _SYNTAX_HIGHLIGHT_DARK_THEME
53+
54+
55+
class CycodeHeading(Heading):
56+
"""Custom Rich Heading for Markdown.
57+
58+
Changes:
59+
- remove justify to 'center'
60+
- remove the box for h1
61+
"""
62+
63+
def __rich_console__(self, console: 'Console', options: 'ConsoleOptions') -> RenderResult:
64+
if self.tag == 'h2':
65+
yield Text('')
66+
yield self.text
67+
68+
69+
Markdown.elements['heading_open'] = CycodeHeading

0 commit comments

Comments
 (0)