Skip to content
8 changes: 6 additions & 2 deletions typer/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,9 @@ def get_help_record(self, ctx: click.Context) -> Optional[Tuple[str, str]]:
extra_str = f"[{extra_str}]"
if rich is not None:
# This is needed for when we want to export to HTML
extra_str = rich.markup.escape(extra_str).strip()
from . import rich_utils

extra_str = rich_utils.escape_before_html_export(extra_str)

help = f"{help} {extra_str}" if help else f"{extra_str}"
return name, help
Expand Down Expand Up @@ -583,7 +585,9 @@ def _write_opts(opts: Sequence[str]) -> str:
extra_str = f"[{extra_str}]"
if rich is not None:
# This is needed for when we want to export to HTML
extra_str = rich.markup.escape(extra_str).strip()
from . import rich_utils

extra_str = rich_utils.escape_before_html_export(extra_str)

help = f"{help} {extra_str}" if help else f"{extra_str}"

Expand Down
6 changes: 6 additions & 0 deletions typer/rich_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from rich.emoji import Emoji
from rich.highlighter import RegexHighlighter
from rich.markdown import Markdown
from rich.markup import escape
from rich.padding import Padding
from rich.panel import Panel
from rich.table import Table
Expand Down Expand Up @@ -727,6 +728,11 @@ def rich_abort_error() -> None:
console.print(ABORTED_TEXT, style=STYLE_ABORTED)


def escape_before_html_export(input_text: str) -> str:
"""Ensure that the input string can be used for HTML export."""
return escape(input_text).strip()


def rich_to_html(input_text: str) -> str:
"""Print the HTML version of a rich-formatted input string.

Expand Down
Loading