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

Parsing issue with multiple links #1

Closed
matt-oakes opened this issue Sep 30, 2014 · 3 comments · Fixed by #2
Closed

Parsing issue with multiple links #1

matt-oakes opened this issue Sep 30, 2014 · 3 comments · Fixed by #2

Comments

@matt-oakes
Copy link
Contributor

I'm using your library and I came across some markdown that crashed the parser:

Markdown with a [link](http://google.com) and [phone number](tel:+441234567890).\n\nNew paragraph!

What was happening was the first link was being matched but the regex was too greedy and also matched all the text in between the start of the first link and the end of the last link. When the URL was parsed it was obviously invalid and hance url was nil. Once we attempted to add the attribute with the nil url value it crashed the application.

The regex I came up with to fix this was:

static NSString *CNMarkdownLinkRegex = @"(?<!\\!)\\[.*?\\]\\(\\S*\\)";

The change is the addition of the ? inside the \\[.*?\\] part. This means it will lazily match, rather than greedily consuming as much as it can. I also changed the link part to use \S because URL's should not contain whitespace. Ideally we'd only match for valid URL characters.

@laptobbe
Copy link
Owner

Great work! We should also add some tests to cover this. Could you create a pull request with the tests and this fix? Otherwise I can have a look at it.

@matt-oakes
Copy link
Contributor Author

Sure I'll submit a pull request soon. I'm not using your library directly as a Pod because I need to customise the attributes I'm applying for each element, so it's a little more work. I'll do it soon though!

@laptobbe
Copy link
Owner

Thanks!

Maybe we could add a formatting block to each method that adds a specific parsing rule? That way you could use the regexes defined in the project but set your own formatting if needed.

laptobbe pushed a commit that referenced this issue Jun 3, 2015
hjon pushed a commit to hjon/TSMarkdownParser that referenced this issue Mar 21, 2017
Fix for a crash when attempting to create a URL from a string that contains emoji (including test)
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.

2 participants