miyuchina / mistletoe

A fast, extensible and spec-compliant Markdown parser in pure Python.
MIT License
841 stars 119 forks source link

IndexError when parsing unfinished-link-like text #98

Closed pelid closed 2 years ago

pelid commented 4 years ago

Got unexpected IndexError during rendering of preview for user input. Steps to reproduce are below:

import mistletoe

mistletoe.markdown('# Header with [link](')
Traceback (most recent call last):
  ....
  File ".../site-packages/mistletoe/core_tokens.py", line 206, in match_link_dest
    if string[offset] == '<':
IndexError: string index out of range

Looks like a bug.

Was used mistletoe of version 0.7.2.

pbodnar commented 4 years ago

Hi @pelid, the # Header with [link]( from your testing input is not a valid markdown - the link code is not finished, so the parsing logically fails. So is your requirement to return some nice error from mistletoe, or possibly to fallback to rendering a simple text instead? I would see this as a feature request then.

fr3aker commented 2 years ago

I don't know what the best behaviour would be, but a random exception is likely the worst possible solution. A better behaviour would be a sane, catchable mistletoe exception on invalid markdown. However, it could be argued that it is just text that, on first sight, looks like possibly markdown, while it is in fact just text. It could be a weird smiley: )[<>](. Would it be illegal to have this in a markdown text?

pbodnar commented 2 years ago

Fixed in the master. This turned out to be a very simple one. :) So I don't even add unit tests this time.