-
Notifications
You must be signed in to change notification settings - Fork 183
Markdown spaces #678
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
Closed
Closed
Markdown spaces #678
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0994730
test(links): add robust link resolution tests
sfgartland cec23ba
test(lsp): add panic protection unit test
sfgartland 0916f62
fix(links): unify resolution for Markdown and WikiLinks and fix LSP p…
sfgartland 1fbe0fd
test(markdown): add tests for spaces in Markdown links
sfgartland f9325d9
feat(markdown): support literal spaces in Markdown links
sfgartland File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| package extensions | ||
|
|
||
| import ( | ||
| "regexp" | ||
|
|
||
| "github.com/yuin/goldmark" | ||
| "github.com/yuin/goldmark/ast" | ||
| "github.com/yuin/goldmark/parser" | ||
| "github.com/yuin/goldmark/text" | ||
| "github.com/yuin/goldmark/util" | ||
| ) | ||
|
|
||
| // MarkdownLinkWithSpacesExt is an extension parsing Markdown links with spaces in the destination. | ||
| // For example, [label](file name.md). | ||
| var MarkdownLinkWithSpacesExt = &markdownLinkWithSpaces{} | ||
|
|
||
| type markdownLinkWithSpaces struct{} | ||
|
|
||
| func (e *markdownLinkWithSpaces) Extend(m goldmark.Markdown) { | ||
| m.Parser().AddOptions( | ||
| parser.WithInlineParsers( | ||
| util.Prioritized(&mlwsParser{}, 199), | ||
| ), | ||
| ) | ||
| } | ||
|
|
||
| type mlwsParser struct{} | ||
|
|
||
| func (p *mlwsParser) Trigger() []byte { | ||
| return []byte{'['} | ||
| } | ||
|
|
||
| var mlwsRegex = regexp.MustCompile(`^\[([^\]]+)\]\(\s*([^)"]*?)\s*(?:\s+"([^"]+)")?\s*\)`) | ||
|
|
||
| func (p *mlwsParser) Parse(parent ast.Node, block text.Reader, pc parser.Context) ast.Node { | ||
| line, segment := block.PeekLine() | ||
| match := mlwsRegex.FindSubmatch(line) | ||
| if match == nil { | ||
| return nil | ||
| } | ||
|
|
||
| label := match[1] | ||
| destination := match[2] | ||
| var title []byte | ||
| if len(match) > 3 { | ||
| title = match[3] | ||
| } | ||
|
|
||
| block.Advance(len(match[0])) | ||
|
|
||
| link := ast.NewLink() | ||
| link.Destination = destination | ||
| if len(title) > 0 { | ||
| link.Title = title | ||
| } | ||
|
|
||
| // Use ast.Text so that LinkPosition can find the text segments | ||
| txt := ast.NewText() | ||
| start := segment.Start + 1 | ||
| txt.Segment = text.NewSegment(start, start+len(label)) | ||
| link.AppendChild(link, txt) | ||
|
|
||
| return link | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
We only just got rid of our own custom markdown parser: it had way too many edge cases which were broken, and each time we fixed one, another showed up.
I'm very hesitant to re-introduce a custom parser for markdown 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.
That is very understandable! Actually, I did not know this, but the commonmark standard allows for spaces in markdown links if wrapped with <>. E.g.
[Link name](<projects/Research Paper/review.md>)So there is no need for this PR. But maybe this would be something worth adding to the documentation on 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.
In the spec explicitly allows for it, and the parser is rejecting it, we should report this to https://github.com/yuin/goldmark. The author is quite responsive.