Last active
June 4, 2020 11:12
-
-
Save Hultner/75a0308aa8dcdeb56ba1139acf3d18db to your computer and use it in GitHub Desktop.
Rich subtle markdown style
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import Any | |
from rich.console import Console, ConsoleOptions, RenderResult | |
from rich.markdown import Markdown, Heading, Text | |
class SubtleHeading(Heading): | |
def __init__(self, level): | |
# Defering all headings by one level to avoid the borderd box for H1, hence level 2 is max | |
super().__init__(level + 1) | |
def __rich_console__( | |
self, console: Console, options: ConsoleOptions | |
) -> RenderResult: | |
# Overwriting this to avoid the center-alginment | |
# h1 is unused since we defer all levels by one level | |
text = self.text | |
# Styled text for h2 and beyond | |
if self.level == 2: | |
yield Text("\n") | |
yield text | |
class SubtleMarkdown(Markdown): | |
def __init__(self, *args, **kwargs): | |
super(SubtleMarkdown, self).__init__(*args, **kwargs) | |
self.elements["heading"] = SubtleHeading | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment