rich icon indicating copy to clipboard operation
rich copied to clipboard

New markdown parser

Open darrenburns opened this issue 2 years ago • 1 comments

Type of changes

  • [ ] Bug fix
  • [ ] New feature
  • [ ] Documentation / docstrings
  • [ ] Tests
  • [x] Other

Checklist

  • [x] 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.
  • [x] 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.

darrenburns avatar Aug 02 '22 14:08 darrenburns

Codecov Report

Base: 98.51% // Head: 98.50% // Decreases project coverage by -0.01% :warning:

Coverage data is based on head (8e87818) compared to base (8baf90b). Patch coverage: 100.00% of modified lines in pull request are covered.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2439      +/-   ##
==========================================
- Coverage   98.51%   98.50%   -0.01%     
==========================================
  Files          74       74              
  Lines        7897     7915      +18     
==========================================
+ Hits         7780     7797      +17     
- Misses        117      118       +1     
Flag Coverage Δ
unittests 98.50% <100.00%> (-0.01%) :arrow_down:

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
rich/default_styles.py 100.00% <ø> (ø)
rich/markdown.py 99.66% <100.00%> (-0.34%) :arrow_down:

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

:umbrella: View full report at Codecov.
:loudspeaker: Do you have feedback about the report comment? Let us know in this issue.

codecov-commenter avatar Aug 02 '22 14:08 codecov-commenter

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)

synrg avatar Dec 26 '22 11:12 synrg

Hi all,

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

daniloegea avatar Jan 09 '23 11:01 daniloegea

@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.

darrenburns avatar Jan 18 '23 10:01 darrenburns

Nice. This will make some folks happy.

willmcgugan avatar Jan 18 '23 11:01 willmcgugan

That's brilliant. Thanks so much! :)

daniloegea avatar Jan 18 '23 15:01 daniloegea

superb! many thanks

synrg avatar Jan 19 '23 08:01 synrg

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

slyon avatar Jan 19 '23 16:01 slyon

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!

chrisjsewell avatar Feb 22 '23 04:02 chrisjsewell