-
Notifications
You must be signed in to change notification settings - Fork 29.9k
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
Detect md image link #66958
Conversation
There was a problem hiding this 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; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
extensions/markdown-language-features/src/features/documentLinkProvider.ts
Outdated
Show resolved
Hide resolved
Thanks! This change will be in the next VS Code insiders build and is scheduled to be included in VS Code 1.31 |
Awesome, thanks for the fast response |
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.