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

Kramdown freezes with specific input. #755

Open
EnotPoloskun opened this issue May 25, 2022 · 3 comments
Open

Kramdown freezes with specific input. #755

EnotPoloskun opened this issue May 25, 2022 · 3 comments
Assignees

Comments

@EnotPoloskun
Copy link

EnotPoloskun commented May 25, 2022

Hello. We have noticed that kramdown has hard time parsing some string.

Example:

text = "___QWE ___qwe ___qwe ___qwe ___qwe ___QWE ___qwe ___qwe ___qwe ___qwe ___QWE ___qwe ___qwe ___qwe ___qwe ___QWE ___qwe ___qwe ___qwe ___qwe ___QWE ___qwe ___qwe ___qwe ___qwe ___QWE ___qwe ___qwe ___qwe ___qwe ___QWE ___qwe ___qwe ___qwe ___qwe ___QWE ___qwe ___qwe ___qwe ___qwe ___QWE ___qwe ___qwe ___qwe ___qwe ___QWE ___qwe ___qwe ___qwe ___qwe ___QWE ___qwe ___qwe ___qwe ___qwe ___QWE ___qwe ___qwe ___qwe ___qwe ___QWE ___qwe ___qwe ___qwe ___qwe ___QWE ___qwe ___qwe ___qwe ___qwe ___QWE ___qwe ___qwe ___qwe ___qwe ___QWE ___qwe ___qwe ___qwe ___qwe ___QWE ___qwe ___qwe ___qwe ___qwe ___QWE ___qwe ___qwe ___qwe ___qwe ___QWE ___qwe ___qwe ___qwe ___qwe ___QWE ___qwe ___qwe ___qwe ___qwe"

Kramdown::Document.new(text)

takes around 30 second on my machine. And if I double string with same pattern - it never finishes. Is there a workaround or any option which would help processing this markdown?

Kramdown version - 2.4.0

@gettalong gettalong self-assigned this May 25, 2022
@gettalong
Copy link
Owner

The reason for this is backtracking. For the first three underscores kramdown tries to find the matching pair. When encountering the second three underscores, it sees that those are not the matching ones. So it starts with those three to find their matching counterpart. And on, and on, and on it goes... until the end of the text where we see that nothing matches. So all nested matching attempts are abandoned and other things tried. Until we come to the first three underscores again. Then we see that they should be handled as normal text. After that we find the second three underscores and everything starts again...

I will have a look at the code.

@EnotPoloskun
Copy link
Author

EnotPoloskun commented May 25, 2022

When encountering the second three underscores, it sees that those are not the matching ones

Why second three underscores are considered not "matching"? Is it intentional?

I have tried https://markdowntohtml.com/ for this text, and it considers them as matching if I understand correctly.

So If I understand correctly

___1 ___QWE ___2 ___

In this example 1 and 2 should be formatted.

@gettalong
Copy link
Owner

Underscores only match at word boundaries and the stopping delimiter must not be preceded by a space (see https://kramdown.gettalong.org/syntax.html#emphasis). So yes, that is intentional.

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

No branches or pull requests

2 participants