Skip to content

Commit dfe2e00

Browse files
committed
custom markdown
1 parent 6c9b33a commit dfe2e00

2 files changed

Lines changed: 50 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Changelog
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7+
8+
## [Unreleased]
9+
10+
### Added
11+
12+
- Added space key to page down to pager
13+
14+
### Changed
15+
16+
- Change how code blocks in markdown are rendered (remove border, adding padding)
17+
18+
[unreleased]: https://github.com/olivierlacan/keep-a-changelog/compare/v1.5.1...HEAD

src/rich_cli/markdown.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from typing import Any
2+
3+
from rich.console import Console, ConsoleOptions, RenderResult
4+
from rich.markdown import Markdown, TextElement
5+
from rich.padding import Padding
6+
from rich.syntax import Syntax
7+
8+
9+
class CodeBlock(TextElement):
10+
"""A code block with syntax highlighting."""
11+
12+
style_name = "markdown.code_block"
13+
14+
@classmethod
15+
def create(cls, markdown: "Markdown", node: Any) -> "CodeBlock":
16+
node_info = node.info or ""
17+
lexer_name = node_info.partition(" ")[0]
18+
return cls(lexer_name or "default", markdown.code_theme)
19+
20+
def __init__(self, lexer_name: str, theme: str) -> None:
21+
self.lexer_name = lexer_name
22+
self.theme = theme
23+
24+
def __rich_console__(
25+
self, console: Console, options: ConsoleOptions
26+
) -> RenderResult:
27+
code = str(self.text).rstrip()
28+
syntax = Syntax(code, self.lexer_name, theme=self.theme, word_wrap=True)
29+
yield Padding(syntax, (0, 4))
30+
31+
32+
Markdown.elements["code_block"] = CodeBlock

0 commit comments

Comments
 (0)