-
-
Notifications
You must be signed in to change notification settings - Fork 672
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
Potential bug with new lines in the help output when markdown mode is used #447
Comments
For those who were running into the same issue, here is a quick monkey patch I've prepared:
import inspect
from typing import Union, Iterable
import click
from rich.console import group
from rich.markdown import Markdown
from rich.text import Text
from typer.core import MarkupMode
from typer.rich_utils import MARKUP_MODE_MARKDOWN, STYLE_HELPTEXT_FIRST_LINE, _make_rich_rext
@group()
def _get_custom_help_text(
*,
obj: Union[click.Command, click.Group],
markup_mode: MarkupMode,
) -> Iterable[Union[Markdown, Text]]:
# Fetch and dedent the help text
help_text = inspect.cleandoc(obj.help or "")
# Trim off anything that comes after \f on its own line
help_text = help_text.partition("\f")[0]
# Get the first paragraph
first_line = help_text.split("\n\n")[0]
# Remove single linebreaks
if markup_mode != MARKUP_MODE_MARKDOWN and not first_line.startswith("\b"):
first_line = first_line.replace("\n", " ")
yield _make_rich_rext(
text=first_line.strip(),
style=STYLE_HELPTEXT_FIRST_LINE,
markup_mode=markup_mode,
)
# Get remaining lines, remove single line breaks and format as dim
remaining_paragraphs = help_text.split("\n\n")[1:]
if remaining_paragraphs:
remaining_lines = inspect.cleandoc("\n\n".join(remaining_paragraphs).replace("<br/>", "\\"))
yield _make_rich_rext(
text=remaining_lines,
style="cyan",
markup_mode=markup_mode,
)
This allows you to use Some output limitations of the method above:
|
We've also just run into this issue. Markdown that is rendered correctly by Rich is not rendered correctly when fed through a Typer docstring. @renardeinside's monkeypatch may work but a proper fix would be much more palatable. |
One more bump, it makes the Markdown mode not really usable unfortunately. |
It's now over a year since this problem was reported and Markdown support remains completely broken. Is this library abandonware by now? It'd be a real pity. |
+1 for #671 to get merged. This is a real bummer when using rich and markdown in help docs. |
Yeah, would love to see a fix for this! |
It isn't 🙂. Open-source maintenance is quite a bit of work, and we're facing some backlog, but we're still committed to maintaining and improving Thanks all for your comments & contributions, they're very much appreciated! |
Includes fix to typo in function name. Ref: fastapi#447
Includes fix to typo in function name. Ref: fastapi#447
First Check
Commit to Help
Example Code
Description
--help
switchInput:
Output:
Expected output:
If I run the same command with:
app = typer.Typer(rich_markup_mode="rich", name="tester-app")
I get the expected result:
If I add more newlines in
markdown mode
, I get the following:Input:
Output:
It only works if I add 3 newlines:
Input:
Output:
But it's also not the desired output. Desired output would be something similar to
rich
formatting:Operating System
macOS
Operating System Details
Apple M1, macOs Monterey 12.2.1
Typer Version
0.6.1
Python Version
Python 3.9.12
Additional Context
No response
The text was updated successfully, but these errors were encountered: