Skip to content

Commit e9b0e19

Browse files
committed
Update to markdown styles
1 parent c595fa9 commit e9b0e19

File tree

6 files changed

+55
-36
lines changed

6 files changed

+55
-36
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2727

2828
- `cells.cell_len` now has a `unicode_version` parameter (that you probably should never change) https://github.com/Textualize/rich/pull/3930
2929
- Live will not write a new line if there was nothing rendered https://github.com/Textualize/rich/pull/3934
30+
- Changed style of Markdown headers
31+
- Changed style of Markdown tables, added `markdown.table.header` and `markdown.table.border` styles
32+
- Changed style of Markdown rules
3033

3134
## [14.2.0] - 2025-10-09
3235

rich/default_styles.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -149,20 +149,22 @@
149149
"markdown.block_quote": Style(color="magenta"),
150150
"markdown.list": Style(color="cyan"),
151151
"markdown.item": Style(),
152-
"markdown.item.bullet": Style(color="yellow", bold=True),
153-
"markdown.item.number": Style(color="yellow", bold=True),
154-
"markdown.hr": Style(color="yellow"),
152+
"markdown.item.bullet": Style(bold=True),
153+
"markdown.item.number": Style(color="cyan"),
154+
"markdown.hr": Style(dim=True),
155155
"markdown.h1.border": Style(),
156-
"markdown.h1": Style(bold=True),
157-
"markdown.h2": Style(bold=True, underline=True),
158-
"markdown.h3": Style(bold=True),
159-
"markdown.h4": Style(bold=True, dim=True),
160-
"markdown.h5": Style(underline=True),
161-
"markdown.h6": Style(italic=True),
156+
"markdown.h1": Style(bold=True, underline=True),
157+
"markdown.h2": Style(color="magenta", underline=True),
158+
"markdown.h3": Style(color="magenta", bold=True),
159+
"markdown.h4": Style(color="magenta", italic=True),
160+
"markdown.h5": Style(italic=True),
161+
"markdown.h6": Style(dim=True),
162162
"markdown.h7": Style(italic=True, dim=True),
163163
"markdown.link": Style(color="bright_blue"),
164164
"markdown.link_url": Style(color="blue", underline=True),
165165
"markdown.s": Style(strike=True),
166+
"markdown.table.border": Style(color="cyan"),
167+
"markdown.table.header": Style(color="cyan", bold=False),
166168
"iso8601.date": Style(color="blue"),
167169
"iso8601.time": Style(color="magenta"),
168170
"iso8601.timezone": Style(color="yellow"),

rich/markdown.py

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import sys
4+
from dataclasses import dataclass
45
from typing import ClassVar, Iterable, get_args
56

67
from markdown_it import MarkdownIt
@@ -14,7 +15,6 @@
1415
from .console import Console, ConsoleOptions, JustifyMethod, RenderResult
1516
from .containers import Renderables
1617
from .jupyter import JupyterMixin
17-
from .panel import Panel
1818
from .rule import Rule
1919
from .segment import Segment
2020
from .style import Style, StyleStack
@@ -124,9 +124,24 @@ def __rich_console__(
124124
yield self.text
125125

126126

127+
@dataclass
128+
class HeadingFormat:
129+
justify: JustifyMethod = "left"
130+
style: str = ""
131+
132+
127133
class Heading(TextElement):
128134
"""A heading."""
129135

136+
LEVEL_ALIGN: ClassVar[dict[str, JustifyMethod]] = {
137+
"h1": "center",
138+
"h2": "left",
139+
"h3": "left",
140+
"h4": "left",
141+
"h5": "left",
142+
"h6": "left",
143+
}
144+
130145
@classmethod
131146
def create(cls, markdown: Markdown, token: Token) -> Heading:
132147
return cls(token.tag)
@@ -143,20 +158,10 @@ def __init__(self, tag: str) -> None:
143158
def __rich_console__(
144159
self, console: Console, options: ConsoleOptions
145160
) -> RenderResult:
146-
text = self.text
147-
text.justify = "center"
148-
if self.tag == "h1":
149-
# Draw a border around h1s
150-
yield Panel(
151-
text,
152-
box=box.HEAVY,
153-
style="markdown.h1.border",
154-
)
155-
else:
156-
# Styled text for h2 and beyond
157-
if self.tag == "h2":
158-
yield Text("")
159-
yield text
161+
text = self.text.copy()
162+
heading_justify = self.LEVEL_ALIGN.get(self.tag, "left")
163+
text.justify = heading_justify
164+
yield text
160165

161166

162167
class CodeBlock(TextElement):
@@ -219,7 +224,8 @@ def __rich_console__(
219224
self, console: Console, options: ConsoleOptions
220225
) -> RenderResult:
221226
style = console.get_style("markdown.hr", default="none")
222-
yield Rule(style=style)
227+
yield Rule(style=style, characters="-")
228+
yield Text()
223229

224230

225231
class TableElement(MarkdownElement):
@@ -241,11 +247,19 @@ def on_child_close(self, context: MarkdownContext, child: MarkdownElement) -> bo
241247
def __rich_console__(
242248
self, console: Console, options: ConsoleOptions
243249
) -> RenderResult:
244-
table = Table(box=box.SIMPLE_HEAVY)
250+
table = Table(
251+
box=box.SIMPLE,
252+
pad_edge=False,
253+
style="markdown.table.border",
254+
show_edge=True,
255+
collapse_padding=True,
256+
)
245257

246258
if self.header is not None and self.header.row is not None:
247259
for column in self.header.row.cells:
248-
table.add_column(column.content)
260+
heading = column.content.copy()
261+
heading.stylize("markdown.table.header")
262+
table.add_column(heading)
249263

250264
if self.body is not None:
251265
for row in self.body.rows:

tests/_card_render.py

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)