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

ext/codehilite.py: Putting "```text like this" causes ValueError in _parse_extras #147

Closed
llllvvuu opened this issue Jun 9, 2023 · 0 comments · Fixed by #148
Closed

Comments

@llllvvuu
Copy link
Contributor

llllvvuu commented Jun 9, 2023

Hi, I noticed that I was getting this stack trace:

  File "/Users/llwu/git/@llllvvuu/contributor-tools/github_issues/test.py", line 6, in <module>
    markdown.convert("```python my_script.py\nprint('hello world')\n```")
  File "/Users/llwu/.pyenv/versions/scripts/lib/python3.11/site-packages/marko/__init__.py", line 111, in convert
    return self.render(self.parse(text))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/llwu/.pyenv/versions/scripts/lib/python3.11/site-packages/marko/__init__.py", line 131, in render
    return r.render(parsed)
           ^^^^^^^^^^^^^^^^
  File "/Users/llwu/.pyenv/versions/scripts/lib/python3.11/site-packages/marko/renderer.py", line 71, in render
    return self.render_children(element)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/llwu/.pyenv/versions/scripts/lib/python3.11/site-packages/marko/renderer.py", line 85, in render_children
    rendered = [self.render(child) for child in element.children]  # type: ignore
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/llwu/.pyenv/versions/scripts/lib/python3.11/site-packages/marko/renderer.py", line 85, in <listcomp>
    rendered = [self.render(child) for child in element.children]  # type: ignore
                ^^^^^^^^^^^^^^^^^^
  File "/Users/llwu/.pyenv/versions/scripts/lib/python3.11/site-packages/marko/renderer.py", line 70, in render
    return render_func(element)
           ^^^^^^^^^^^^^^^^^^^^
  File "/Users/llwu/.pyenv/versions/scripts/lib/python3.11/site-packages/marko/ext/codehilite.py", line 40, in render_fenced_code
    options.update(_parse_extras(getattr(element, "extra", None)))
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/llwu/.pyenv/versions/scripts/lib/python3.11/site-packages/marko/ext/codehilite.py", line 30, in _parse_extras
    return {k: json.loads(v) for part in line.split(",") for k, v in [part.split("=")]}
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/llwu/.pyenv/versions/scripts/lib/python3.11/site-packages/marko/ext/codehilite.py", line 30, in <dictcomp>
    return {k: json.loads(v) for part in line.split(",") for k, v in [part.split("=")]}
                                                             ^^^^
ValueError: not enough values to unpack (expected 2, got 1)

while parsing the markdown of this GitHub issue. From this, I was able to create a minimal repro:

from marko import Markdown

markdown = Markdown(extensions=['codehilite'])
markdown.convert("```Error: Unable\n```")

What's happening here is that Marko thinks we are highlighting language Error: with options Unable.

In fact, I noticed something else; the example given in the docstring will also error by this same logic!

    from marko import Markdown

    markdown = Markdown(extensions=['codehilite'])
    markdown.convert(```python my_script.py\nprint('hello world')\n```)

Indeed it does throw a ValueError. It should be python filename="my_script.py".

I have attached a PR which fixes this issue. It simply ignores tokens which are not of the form key=JSON

@llllvvuu llllvvuu changed the title ext/codehilite.py: Putting "``text like this" causes ValueError in _parse_extras` ext/codehilite.py: Putting "\\\text like this" causes ValueError in _parse_extras` Jun 9, 2023
@llllvvuu llllvvuu changed the title ext/codehilite.py: Putting "\\\text like this" causes ValueError in _parse_extras` ext/codehilite.py: Putting "``text like this" causes ValueError in _parse_extras` Jun 9, 2023
@llllvvuu llllvvuu changed the title ext/codehilite.py: Putting "``text like this" causes ValueError in _parse_extras` ext/codehilite.py: Putting "```text like this" causes ValueError in _parse_extras` Jun 9, 2023
@llllvvuu llllvvuu changed the title ext/codehilite.py: Putting "```text like this" causes ValueError in _parse_extras` ext/codehilite.py: Putting "```text like this" causes ValueError in _parse_extras Jun 9, 2023
llllvvuu added a commit to llllvvuu/marko that referenced this issue Jun 9, 2023
frostming added a commit that referenced this issue Jun 9, 2023
* fix(ext/codehilite): `ValueError` in `parse_extras` (closes #147)

* apply improvement

---------

Co-authored-by: Frost Ming <[email protected]>
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 a pull request may close this issue.

1 participant