-
Notifications
You must be signed in to change notification settings - Fork 788
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
Allow customizing the markdown parser #2075
Allow customizing the markdown parser #2075
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Like it. Just a request re the API.
src/textual/widgets/_markdown.py
Outdated
@@ -509,6 +514,7 @@ def __init__( | |||
name: str | None = None, | |||
id: str | None = None, | |||
classes: str | None = None, | |||
parser_factory: Callable[[], MarkdownIt] = default_parser_factory, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we make this None-able. When you need to construct the parser, check for None and create MarkdownIt("gfm-like")
.
This means we could drop the default_parser_factory
function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can rewrite it this way, sure.
Sorry for prematurely requesting your re-review.
src/textual/widgets/_markdown.py
Outdated
@@ -799,6 +807,7 @@ def __init__( | |||
name: str | None = None, | |||
id: str | None = None, | |||
classes: str | None = None, | |||
parser_factory: Callable[[], MarkdownIt] = default_parser_factory, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some comment as above. Let's make this None able.
For instance, code using Markdown might wish to create a markdown parser that does not parse embedded HTML: ```py def parser_factory(): parser = MarkdownIt("gfm-like") parser.options["html"] = False return parser ```
0e5e92c
to
4c7e54f
Compare
OK -- rebased and now actually ready for re-review. Thank you for your time! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just some pedantic requests.
src/textual/widgets/_markdown.py
Outdated
@@ -509,6 +508,7 @@ def __init__( | |||
name: str | None = None, | |||
id: str | None = None, | |||
classes: str | None = None, | |||
parser_factory: Optional[Callable[[], MarkdownIt]] = None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you use | None
here, just for consistency sake...
src/textual/widgets/_markdown.py
Outdated
@@ -574,7 +576,7 @@ async def update(self, markdown: str) -> None: | |||
""" | |||
output: list[MarkdownBlock] = [] | |||
stack: list[MarkdownBlock] = [] | |||
parser = MarkdownIt("gfm-like") | |||
parser = self._parser_factory() if self._parser_factory else MarkdownIt("gfm-like") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you make this an explicit is None
check please.
Thanks again for the feedback. I've updated the PR once more. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good. Thanks!
Test fail is unrelated to this change. Merging. |
Thank you! |
For instance, code using Markdown might wish to create a markdown parser that does not parse embedded HTML:
This is needed to properly render chatgpt's markdown; there are many "knobs" of MarkdownIt and users of the textual library may need to tune them to their needs.