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

Fix brackets in links in markdown lexer #1445

Merged

Conversation

Ravlen
Copy link
Contributor

@Ravlen Ravlen commented Feb 20, 2020

When hiding brackets inside links with backticks, rendering
was breaking. This makes the lexer search for ][ or ](
to find the end delimiting the link text.

This fixes #1444.

@Ravlen Ravlen requested a review from pyrmont February 20, 2020 06:37
@Ravlen
Copy link
Contributor Author

Ravlen commented Feb 20, 2020

@pyrmont I see your lookahead suggestion, but it wasn't working when I tested. I'll give it another go and see if I can figure it out, and I'll update this PR if I can get it to work. It might just be the regex tester I'm using doesn't like the lookahead formatting. I understand the logic now.

Also, does this need to add an example to the spec, like you did in #1442?

When hiding brackets inside links (with backticks),
coloring was breaking. This makes the lexer search
for the open bracket or paren that starts the url
or reference link
@Ravlen Ravlen force-pushed the ravlen.bugfix.markdown-brackets-links branch from 1d146ae to a59a5f9 Compare February 20, 2020 07:37
@Ravlen
Copy link
Contributor Author

Ravlen commented Feb 20, 2020

@pyrmont OK, sorry about that, I think it's good to go now. 🙏

@pyrmont pyrmont self-assigned this Feb 20, 2020
@pyrmont pyrmont added the author-action The PR has been reviewed but action by the author is needed label Feb 20, 2020
@pyrmont pyrmont changed the title Fix brackets in links in markdown lexer (#1444) Fix brackets in links in markdown lexer Feb 20, 2020
@Ravlen
Copy link
Contributor Author

Ravlen commented Feb 20, 2020

@pyrmont Thanks a lot for the fixes! Feel free to point them out to me next time, and I'll fix them myself so you don't have to go through all the effort. It also means I can fix my commit, so I'm not pushing broken regex to the project, even if it is fixed right after. 🙇

I've also cloned locally and am able to test, (super simple, works great), so I'll test locally myself before submitting next time. 🙇

@Ravlen
Copy link
Contributor Author

Ravlen commented Feb 20, 2020

@pyrmont I don't think the regex you pushed was correct, as there shouldn't be any spaces between the link text and the URL/Reference:

[This is a link `with backticks`](example.com)
[This is not a link `with backticks`] (example.com)

By adding spaces/tabs to be allowed between the ] and ( or [, it broke:

Screen Shot 2020-02-20 at 22 16 57

I removed that in the latest commit, and now it seems correct:

Screen Shot 2020-02-20 at 22 16 15

I also added that example to the markdown sample spec. What do you think?

@pyrmont
Copy link
Contributor

pyrmont commented Feb 20, 2020

A couple of things:

  1. Don't worry about fixing broken commits. It's preferable to leave them in, to be honest. We follow a squash and merge strategy when merging so it won't pollute the eventual commit history and it means the true development process is easier to follow if/when someone is looking at the PR.

  2. Markdown can have spaces between the text and the reference (see here). Strictly speaking, space is only permitted for reference links but that complicates the logic and I'm not convinced the lazy approach would lead to false positives in real life. Syntax correctness is a non-goal of Rouge so being more permissive than is correct syntactically isn't itself a problem.

What do you think?

@Ravlen
Copy link
Contributor Author

Ravlen commented Feb 21, 2020

@pyrmont In this case, I'm trying to match the behavior I see most commonly.

Testing this markdown:

### Try links

- [This is a link](https://www.google.com)
- [This is not a link] (https://www.google.com)
- [This is a link][example]
- [This is a link?] [example]

[example]: https://www.google.com

Testing with CommonMark dingus

Screen Shot 2020-02-21 at 12 23 17

Testing with GitHub:

Try links

Testing with GitLab

Screen Shot 2020-02-21 at 12 20 07

Testing with VS Code

Screen Shot 2020-02-21 at 12 22 31

It seems like the majority of renderers do not accept this, including commonmark. It's not surprising because I can see how it would cause ambiguity or confusion when mixing references. WDYT?

Copy link
Contributor

@pyrmont pyrmont left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed that not putting a space between the link and the reference is the approach everyone else is taking. Let's do that.

@pyrmont pyrmont merged commit abe35e8 into rouge-ruby:master Feb 23, 2020
@pyrmont
Copy link
Contributor

pyrmont commented Feb 23, 2020

@Ravlen Thanks for everything with this PR! I hope this improves the link highlighting behaviour on your end :)

@pyrmont pyrmont removed the author-action The PR has been reviewed but action by the author is needed label Feb 23, 2020
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 this pull request may close these issues.

Markdown lexer edge case with links and square brackets (rare)
2 participants