Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Render news items with slim headings #74

Merged
merged 2 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions ruyi/ruyipkg/news_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

from rich import box
from rich.table import Table
from rich.markdown import Markdown

from ..config import GlobalConfig
from ..config.news import NewsReadStatusStore
from ..utils.markdown import MarkdownWithSlimHeadings
from .. import log
from .news import NewsItem
from .repo import MetadataRepo
Expand Down Expand Up @@ -127,6 +127,6 @@ def filter_news_items_by_specs(


def print_news(ni: NewsItem) -> None:
md = Markdown(ni.content)
md = MarkdownWithSlimHeadings(ni.content)
log.stdout(md)
log.stdout("")
23 changes: 23 additions & 0 deletions ruyi/utils/markdown.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from rich.console import Console, ConsoleOptions, RenderResult
from rich.markdown import Heading, Markdown, MarkdownContext
from rich.text import Text


class SlimHeading(Heading):
def on_enter(self, context: MarkdownContext) -> None:
heading_level = int(self.tag[1:]) # e.g. self.tag == 'h1'

context.enter_style(self.style_name)
self.text = Text("#" * heading_level + " ", context.current_style)

def __rich_console__(
self,
console: Console,
options: ConsoleOptions,
) -> RenderResult:
yield self.text


class MarkdownWithSlimHeadings(Markdown):
elements = Markdown.elements
elements["heading_open"] = SlimHeading