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

New markdown parser #2439

Merged
merged 52 commits into from
Jan 18, 2023
Merged

New markdown parser #2439

merged 52 commits into from
Jan 18, 2023

Conversation

darrenburns
Copy link
Member

@darrenburns darrenburns commented Aug 2, 2022

Type of changes

  • Bug fix
  • New feature
  • Documentation / docstrings
  • Tests
  • Other

Checklist

  • I've run the latest black with default args on new code.
  • I've updated CHANGELOG.md and CONTRIBUTORS.md where appropriate.
  • I've added tests for new code.
  • I accept that @willmcgugan may be pedantic in the code review.

Description

Work in progress.

Switches over to markdown-it-py instead of commonmark for Markdown parsing.

new_markdown

Copy link
Collaborator

@willmcgugan willmcgugan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work. It's quite a small footprint for such a significant change. Love that the public interface doesn't change.

Approving, but let's not merge just yet. Would like to do one last Python3.6 release.

rich/text.py Outdated Show resolved Hide resolved
rich/markdown.py Outdated Show resolved Hide resolved
rich/markdown.py Outdated Show resolved Hide resolved
rich/markdown.py Outdated Show resolved Hide resolved
@synrg
Copy link

synrg commented Dec 26, 2022

I realize this might be slightly out of scope of this PR, as it does not enable strikethrough extension, but I thought I'd mention it here in case it indicates an issue that is better to address now.

I found that merely enabling the extension was not enough to make ~~strike~~ work. That resulted in an empty string when rendered. What I found in my debugging is that strikethrough markup is being tokenized as tag s, not strike. In order to make it work, I had to both enable the strikethrough extension and modify the tag to process, as per:

diff --git a/rich/markdown.py b/rich/markdown.py
index 36d6491e..3b75c579 100644
--- a/rich/markdown.py
+++ b/rich/markdown.py
@@ -427,7 +427,7 @@ class Markdown(JupyterMixin):
         "image": ImageItem,
     }

-    inlines = {"em", "strong", "code", "strike"}
+    inlines = {"em", "strong", "code", "s"}

     def __init__(
         self,
@@ -439,7 +439,7 @@ class Markdown(JupyterMixin):
         inline_code_lexer: Optional[str] = None,
         inline_code_theme: Optional[str] = None,
     ) -> None:
-        parser = MarkdownIt().enable("table")
+        parser = MarkdownIt().enable(["table", "strikethrough"])
         self.markup = markup
         self.parsed = parser.parse(markup)
         self.code_theme = code_theme

Furthermore, since the default theme has "markdown.strike": Style(strike=True), I had to use a custom theme to set the strikethrough style for the s tag:

from rich.console import Console
from rich.theme import Theme
from rich.style import Style
our_theme = Theme({
    "markdown.s": Style(strike=True),
})
console = Console(theme=our_theme)

@daniloegea
Copy link

Hi all,

considering commonmark is not being maintained anymore, are there plans to merge this PR in the near future?

@darrenburns darrenburns marked this pull request as ready for review January 18, 2023 10:38
@darrenburns
Copy link
Member Author

@synrg That's great, thanks! I've added that to this PR so that we support strikethrough (cc @willmcgugan).

@daniloegea Just polishing this up now, so should be getting merged relatively soon.

@@ -8,7 +8,7 @@ jobs:
strategy:
matrix:
os: [windows-latest, ubuntu-latest, macos-latest]
python-version: ["3.7", "3.8", "3.9", "3.10"]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We were missing 3.11.

@willmcgugan
Copy link
Collaborator

Nice. This will make some folks happy.

@willmcgugan willmcgugan merged commit 9d4a1f3 into master Jan 18, 2023
@willmcgugan willmcgugan deleted the new-markdown-parser branch January 18, 2023 11:47
@daniloegea
Copy link

That's brilliant. Thanks so much! :)

@synrg
Copy link

synrg commented Jan 19, 2023

superb! many thanks

@slyon
Copy link

slyon commented Jan 19, 2023

Awesome, thank you! Will there be a new release including this change, soon?

Oh, nevermind :) https://github.com/Textualize/rich/releases/tag/v13.2.0

toofar added a commit to qutebrowser/qutebrowser that referenced this pull request Jan 23, 2023
@chrisjsewell
Copy link

FYI as the maintainer of markdown-it-py, I approve 😄 was wondering why there was a sudden spike in pypi downloads 😆

Love rich, keep up the good work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants