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

Detect md image link #66958

Merged
merged 7 commits into from
Jan 26, 2019
Merged

Detect md image link #66958

merged 7 commits into from
Jan 26, 2019

Conversation

flurmbo
Copy link
Contributor

@flurmbo flurmbo commented Jan 23, 2019

Fixes #49238. I added an option to the regex to have an image instead of text for the link description. Then when iterating over regex matches providerInlineLinks will now check to see if an image resource was also matched, and if so add it to results. All the tests pass, as well as a new one I've added.

Feedback on the regex would be appreciated, also I think perhaps the code I copied starting here could be refactored into a separate function.

@mjbvz mjbvz added this to the December/January 2019 milestone Jan 23, 2019
Copy link
Collaborator

@mjbvz mjbvz left a comment

Choose a reason for hiding this comment

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

Thanks for taking a look! Changes look good, just a few tweaks.

Let me know if you have any questions about my comments or the code

@@ -51,7 +51,7 @@ function matchAll(
}

export default class LinkProvider implements vscode.DocumentLinkProvider {
private readonly linkPattern = /(\[[^\]]*\]\(\s*)((([^\s\(\)]|\(\S*?\))+))\s*(".*?")?\)/g;
private readonly linkPattern = /(\[((!\[(.+)\]\()(.+)\)\]|[^\]]*\])\(\s*)((([^\s\(\)]|\(\S*?\))+))\s*(".*?")?\)/g;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Regex looks good but I think it does need to be tweaked to be less greedy:

  • Instead of using . for matching the contents between the brackets, match on what that content cannot contain, such as [^\]] so that we match any character that is not a square bracket.
  • Use a non-greedy quanitfier: +? instead of just + so we consume the minimum number of characters that still meet the regexp

The reason is that for markdown files like:

[![alt text](image.jpg)](https://example.com) other text [![alt text](image.jpg)](https://example.com)

if the matching is greedy, the detected link could end up spanning both links.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Probably worth adding a test for that example md as well

@mjbvz mjbvz merged commit 4fe1cdc into microsoft:master Jan 26, 2019
@mjbvz
Copy link
Collaborator

mjbvz commented Jan 26, 2019

Thanks! This change will be in the next VS Code insiders build and is scheduled to be included in VS Code 1.31

@flurmbo
Copy link
Contributor Author

flurmbo commented Jan 26, 2019

Awesome, thanks for the fast response

@github-actions github-actions bot locked and limited conversation to collaborators Mar 27, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Markdown link provider misses link for image
2 participants