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

Escaping of square brackets for reference-style links #91

Closed
spier opened this issue Dec 15, 2018 · 2 comments
Closed

Escaping of square brackets for reference-style links #91

spier opened this issue Dec 15, 2018 · 2 comments

Comments

@spier
Copy link

spier commented Dec 15, 2018

I am not sure if this is an issue on commonmarker, or rather https://github.com/github/cmark-gfm/issues. Or maybe no issue at all. So any input is appreciated.

I noticed that when I render markdown containing a link using the reference style, and then output that document as markdown, all square brackets get escaped.

Example with a regular link

(the square brackets don't get escaped)

doc = CommonMarker.render_doc("[foo](https://google.com)")
puts doc.to_commonmark

=>
[foo](https://google.com)

Example with a reference-style link

(the square brackets get escaped)

doc = CommonMarker.render_doc("[foo][bar]\n[bar]: https://google.com")
puts doc.to_commonmark

=>
\[foo\]\[bar\] \[bar\]: https://google.com

Is this the expected behavior?

@kivikakk
Copy link
Collaborator

Indeed, it is expected — try rendering to HTML and you'll see it spits out something very similar. Trying your document in the official CommonMark tester shows the same result: https://spec.commonmark.org/dingus/?text=%5Bfoo%5D%5Bbar%5D%0A%5Bbar%5D%3A%20https%3A%2F%2Fgoogle.com

The solution is to add an extra newline between the lines: references need to be in their own paragraph to be treated as such, otherwise it's read as part of the paragraph. Note that #to_commonmark will "normalise" the reference into a normal inline link.

@spier
Copy link
Author

spier commented Jan 19, 2019

Oh interesting.

So I added one more newline as you said:

doc = CommonMarker.render_doc("[foo][bar]\n\n[bar]: https://google.com")
puts doc.to_commonmark

And the that returns:

[foo](https://google.com)

Also thanks for the commonmark tester at https://spec.commonmark.org/dingus/, super handy!

@spier spier closed this as completed Jan 19, 2019
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

No branches or pull requests

2 participants